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

Support multiple JSON mime types #55

Merged
merged 1 commit into from
Aug 20, 2020

Conversation

thestrabusiness
Copy link
Contributor

🧰 What's being changed?

This commit expands the content type check in the
Har::ResponseSerializer to accept multiple JSON mime types.

🧪 Testing

Using the following sample application:

$LOAD_PATH << File.expand_path("../../metrics-sdks/packages/ruby/lib", __FILE__)
require "readme/metrics"

class ApiApp
  def call(env)
    [
      200,
      {"Content-Length" => "20", "Content-Type" => "application/x-json"},
      [{response: "value"}.to_json]
    ]
  end
end

map "/api" do
  options = {
    api_key: "api_key",
    buffer_length: 1
  }
  use Readme::Metrics, options do |env|
    {id: "anthonym", label: "Anthony M.", email: "[email protected]"}
  end
  run ApiApp.new
end

And the following curl command:

curl 'http://username:password@localhost:9292/api/foo' -X POST

A request was successfully added to the metrics dashboard with a newly added json mime type.

Screen Shot 2020-08-20 at 11 10 38 AM

Automated test coverage was also expanded

Comment on lines +75 to +77
def content_type_is_json?
JSON_MIME_TYPES.include? @response.content_type
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you feel about moving this to something like HttpRequest#has_json_body? It would be easier to unit-test over there and it fits with the generic request responsibility of the object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh huh. This doesn't fit with the HttpRequest object. @response here is a Rack::Response. Does it make sense to wrap that in a new class to add this has_json_body? functionality and test it there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'm getting confused between request and response 🤦‍♂️

Would it make sense to extract a HttpResponse object similar to what was done with HttpRequest? Or maybe leave everything in here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this again I think that it makes sense to just leave everything here. It doesn't seem like we gain much by wrapping the response.

Comment on lines +131 to +132
expect(json[:content][:text]).to eq body.first
expect(json[:content][:mimeType]).to eq mime_type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this test red before you implemented the change? My guess is that this would be true no matter the mime type as long as there is some kind of body

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was red. The JSON content type was hardcoded as "application/json"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, so it failed for application/json but passed for the others.

This commit expands the content type check in the
Har::ResponseSerializer to accept multiple JSON mime types.
@thestrabusiness thestrabusiness force-pushed the am-multiple-json-mime-types branch from c366f09 to 135975e Compare August 20, 2020 21:13
@thestrabusiness thestrabusiness merged commit 135975e into master Aug 20, 2020
@thestrabusiness thestrabusiness deleted the am-multiple-json-mime-types branch August 20, 2020 21:20
djmango pushed a commit to djmango/metrics-sdks that referenced this pull request Sep 19, 2023
…meio#55)

Bumps [@readme/eslint-config](https://github.com/readmeio/eslint-config) from 2.0.0 to 2.0.2.
- [Release notes](https://github.com/readmeio/eslint-config/releases)
- [Changelog](https://github.com/readmeio/eslint-config/blob/master/CHANGELOG.md)
- [Commits](readmeio/standards@2.0.0...2.0.2)

Signed-off-by: dependabot-preview[bot] <[email protected]>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
@erunion erunion added ruby Issues related to our Ruby SDK and removed area:ruby labels Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ruby Issues related to our Ruby SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants