Skip to content

Commit

Permalink
Fix error when content type is not available on response.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Nov 20, 2023
1 parent 4710727 commit be435f4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# Changelog

- **unreleased**
- Fix error when content type is not available on response.
- **1.2.2**
- Transform header key before assigning values.
- **1.2.1**
Expand Down
2 changes: 1 addition & 1 deletion lib/aitch/request.rb
Expand Up @@ -102,7 +102,7 @@ def http_method_class
all_headers = options.fetch(:default_headers, {}).merge(headers)

all_headers.each do |name, value|
value = value.respond_to?(:call) ? value.call : value
value = value.call if value.respond_to?(:call)
name = name.to_s.split(HEADER_SEPARATOR_RE).map(&:capitalize).join("-")
request[name] = value.to_s
end
Expand Down
6 changes: 2 additions & 4 deletions lib/aitch/response.rb
Expand Up @@ -2,8 +2,6 @@

module Aitch
class Response
extend Forwardable

JSON_STR = "json"
XML_STR = "xml"
HTML_STR = "html"
Expand All @@ -13,8 +11,7 @@ class Response
ERROR_SUFFIX = "_error"
X_RE = /^x-/.freeze

def_delegators :@http_response, :content_type
attr_accessor :redirected_from, :url
attr_accessor :redirected_from, :url, :content_type

def self.description_for_code(code)
[code, DESCRIPTION[code]].compact.join(SPACE_STR)
Expand All @@ -24,6 +21,7 @@ def initialize(options, http_response)
@options = options
@http_response = http_response
@redirected_from = options.fetch(:redirected_from, [])
@content_type = http_response.content_type.to_s
end

ERRORS.each do |status_code, exception|
Expand Down
2 changes: 1 addition & 1 deletion test/aitch/request_test.rb
Expand Up @@ -150,7 +150,7 @@ class RequestTest < Minitest::Test

test "sets basic auth credentials" do
request = build_request(options: {user: "USER", password: "PASS"}).request
credentials = Base64.decode64(request["Authorization"].gsub(/Basic /, ""))
credentials = Base64.decode64(request["Authorization"].gsub("Basic ", ""))

assert_equal "USER:PASS", credentials
end
Expand Down
2 changes: 1 addition & 1 deletion test/aitch/response/errors_test.rb
Expand Up @@ -8,7 +8,7 @@ class ErrorsTest < Minitest::Test

test "detects response as #{name}" do
config = {}
http_response = stub(code: code)
http_response = stub(code: code, content_type: "text/html")
response = Aitch::Response.new(config, http_response)

assert response.public_send("#{name}?")
Expand Down
13 changes: 11 additions & 2 deletions test/aitch/response/status_3xx_test.rb
Expand Up @@ -6,11 +6,20 @@ class Status3xxTest < Minitest::Test
setup { Aitch.configuration.follow_redirect = false }

test "sets default redirected from" do
assert_empty Aitch::Response.new({}, stub("response")).redirected_from
response =
Aitch::Response.new({}, stub("response", content_type: "text/html"))

assert_empty response.redirected_from
end

test "uses provided redirected from" do
assert_equal ["URL"], Aitch::Response.new({redirected_from: ["URL"]}, stub("response")).redirected_from
response =
Aitch::Response.new(
{redirected_from: ["URL"]},
stub("response", content_type: "text/html")
)

assert_equal ["URL"], response.redirected_from
end

test "has body" do
Expand Down

0 comments on commit be435f4

Please sign in to comment.