Merge branch 'fixup_decoders_mime_error' into 'master'

Fixup decoders content type checks

See merge request honeyryderchuck/httpx!166
This commit is contained in:
HoneyryderChuck 2021-09-10 16:05:38 +00:00
commit 24c762a50d
3 changed files with 10 additions and 2 deletions

View File

@ -50,7 +50,7 @@ module HTTPX::Transcoder
def decode(response)
content_type = response.content_type.mime_type
raise Error, "invalid form mime type (#{content_type})" unless content_type == "application/x-www-form-urlencoded"
raise HTTPX::Error, "invalid form mime type (#{content_type})" unless content_type == "application/x-www-form-urlencoded"
Decoder
end

View File

@ -35,7 +35,7 @@ module HTTPX::Transcoder
def decode(response)
content_type = response.content_type.mime_type
raise Error, "invalid json mime type (#{content_type})" unless JSON_REGEX.match?(content_type)
raise HTTPX::Error, "invalid json mime type (#{content_type})" unless JSON_REGEX.match?(content_type)
::JSON.method(:parse)
end

View File

@ -130,10 +130,18 @@ class ResponseTest < Minitest::Test
json_response << %({"a": "b"})
assert json_response.json == { "a" => "b" }
assert json_response.json(symbolize_names: true) == { :a => "b" }
json_response << "bogus"
assert_raises(JSON::ParserError) { json_response.json }
err = assert_raises(HTTPX::Error) { json_response.form }
assert err.message == "invalid form mime type (application/json)"
form_response = Response.new(request, 200, "2.0", { "content-type" => "application/x-www-form-urlencoded" })
form_response << "a=b&c=d"
assert form_response.form == { "a" => "b", "c" => "d" }
err = assert_raises(HTTPX::Error) { form_response.json }
assert err.message == "invalid json mime type (application/x-www-form-urlencoded)"
form_response << "богус"
assert_raises(ArgumentError) { form_response.form }
form2_response = Response.new(request, 200, "2.0", { "content-type" => "application/x-www-form-urlencoded" })
form2_response << "a[]=b&a[]=c&d[e]=f&g[h][i][j]=k&l[m][][n]=o&l[m][][p]=q&l[m][][n]=r&s[=t"