added content-type filtering to decoders

This commit is contained in:
HoneyryderChuck 2021-08-10 12:58:29 +01:00
parent c7f177adbb
commit f520785572
4 changed files with 24 additions and 5 deletions

View File

@ -58,9 +58,16 @@ module HTTPX
end
def decode(response)
return Transcoder::Form.decode(response) if response.headers["content-type"] == "application/x-www-form-urlencoded"
content_type = response.content_type.mime_type
Decoder.new(response)
case content_type
when "application/x-www-form-urlencoded"
Transcoder::Form.decode(response)
when "multipart/form-data"
Decoder.new(response)
else
raise Error, "invalid form mime type (#{content_type})"
end
end
def multipart?(data)

View File

@ -47,7 +47,11 @@ module HTTPX::Transcoder
Encoder.new(form)
end
def decode(_response)
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"
Decoder
end
end

View File

@ -5,6 +5,10 @@ require "json"
module HTTPX::Transcoder
module JSON
JSON_REGEX = %r{\bapplication/(?:vnd\.api\+)?json\b}i.freeze
using HTTPX::RegexpExtensions unless Regexp.method_defined?(:match?)
module_function
class Encoder
@ -28,7 +32,11 @@ module HTTPX::Transcoder
Encoder.new(json)
end
def decode(_response)
def decode(response)
content_type = response.content_type.mime_type
raise Error, "invalid json mime type (#{content_type})" unless JSON_REGEX.match?(content_type)
::JSON.method(:parse)
end
end

View File

@ -228,7 +228,7 @@ module Requests
HTTPX::Request.new(:get, "http://example.com"),
200,
"2.0",
{ "content-type" => "Content-Type: multipart/form-data; boundary=90" }
{ "content-type" => "multipart/form-data; boundary=90" }
)
form_response << [
"--90\r\n",