Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to set the Content-Type of the response #4

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading