Skip to content

Commit

Permalink
Add an option to set the Content-Type of the response (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaiu authored Mar 27, 2024
1 parent b441efe commit 8299af9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,26 @@ It is also possible to set a range for randomizing the delay time by supplying `
"min_response_millis": 150,
"max_response_millis": 500
}
```
```

### Binary Response
It is possible to return binary data as a response of an `endpoint` by setting the `return_value` to a base64 encoded string and by setting the `return_value_binary` to true. This is useful for returning binary files, such as images or PDFs. For example:
```json
{
"verb": "GET",
"path": "/my_binary_path",
"return_value_binary": true,
"return_value": "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABaElEQVR42mL8//8/AyUYIIgBk"
}
```

### Content Type
Supply the optional `content_type` field to set a custom `Content-Type` for an `endpoint` (default is `application/json`):
```json
{
"verb": "GET",
"path": "/my_custom_content_type",
"content_type": "text/plain",
"return_value": "plaintext"
}
```
2 changes: 1 addition & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def self.register_endpoint(endpoint:, sleep_time:)
end
method = METHODS[endpoint.verb]
self.send(method, endpoint.path) do
content_type :json
content_type endpoint.content_type.empty? ? :json : endpoint.content_type
body = JSON.parse(request.body.read) rescue nil
sleep(sleep_time)

Expand Down
1 change: 1 addition & 0 deletions models/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Endpoint < Dry::Struct

attribute :id, Types::Integer
attribute :verb, Types::String
attribute :content_type, Types::String.default('json')
attribute :path, Types::String
attribute :return_value, Types::Any
attribute :return_value_binary, Types::Bool.default(false)
Expand Down
1 change: 1 addition & 0 deletions models/endpoint_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class EndpointRequest < Dry::Struct

attribute :verb, Types::String
attribute :path, Types::String
attribute :content_type, Types::String.default('json')
attribute :return_value, Types::Any
attribute :return_value_binary, Types::Bool.default(false)
attribute :min_response_millis?, Types::Integer
Expand Down

0 comments on commit 8299af9

Please sign in to comment.