Skip to content

Commit

Permalink
Merge branch 'json-suffix' into 'master'
Browse files Browse the repository at this point in the history
Accept more MIME types with json suffix

Closes #326

See merge request os85/httpx!357
  • Loading branch information
HoneyryderChuck committed Dec 3, 2024
2 parents 3b52ef3 + 4bf07e7 commit da11cb3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/httpx/transcoder/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ module HTTPX::Transcoder
module JSON
module_function

JSON_REGEX = %r{\bapplication/(?:vnd\.api\+|hal\+)?json\b}i.freeze
JSON_REGEX = %r{
\b
application/
# optional vendor specific type
(?:
# token as per https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.6
[!#$%&'*+\-.^_`|~0-9a-z]+
# literal plus sign
\+
)?
json
\b
}ix.freeze

class Encoder
extend Forwardable
Expand Down
22 changes: 21 additions & 1 deletion test/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ResponseTest < Minitest::Test
require_relative "extensions/response_pattern_match"
include ResponsePatternMatchTests
rescue SyntaxError
# truffleruby advertises ruby 3 support, but still hasn't implemented pattern matching
# for ruby < 3.0 and truffleruby < 24.0
end
end

Expand Down Expand Up @@ -225,6 +225,26 @@ def test_response_decoders
form4_response = Response.new(request, 200, "2.0", { "content-type" => "application/x-www-form-urlencoded" })
form4_response << "[]"
assert form4_response.form == {}

json2_response = Response.new(request, 200, "2.0", { "content-type" => "application/hal+json" })
json2_response << %({"_links": {"self": {"href": "http://example.com/api/abc" } }, "id": "abc", "name": "ABC" })
assert json2_response.json == {
"_links" => {
"self" => {
"href" => "http://example.com/api/abc",
},
},
"id" => "abc",
"name" => "ABC",
}

json3_response = Response.new(request, 200, "2.0", { "content-type" => "application/vnd.com.acme.customtype+json;charset=UTF-8" })
json3_response << %({"custom": "data"})
assert json3_response.json == { "custom" => "data" }

json4_response = Response.new(request, 200, "2.0", { "content-type" => "application/invalidjson" })
err = assert_raises(HTTPX::Error) { json4_response.json }
assert err.message == "invalid json mime type (application/invalidjson)"
end

private
Expand Down

0 comments on commit da11cb3

Please sign in to comment.