Skip to content

Commit

Permalink
Gracefully handle HTTP 204 response bodies (#1098)
Browse files Browse the repository at this point in the history
* Gracefully handle HTTP 204 response bodies

* Add changelog entry for #1098

* Fix up changelog formatting

* Rubocop fixup
  • Loading branch information
ClaytonPassmore committed Mar 21, 2023
1 parent 2b20d9b commit 4e74cca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Note: For changes to the API, see https://shopify.dev/changelog?filter=api

## Unreleased

- [#1098](https://github.com/Shopify/shopify-api-ruby/pull/1098) Gracefully handle HTTP 204 repsonse bodies
- [#1104](https://github.com/Shopify/shopify-api-ruby/pull/1104) Allow api version overrides.

## Version 12.4.0
Expand Down
2 changes: 1 addition & 1 deletion lib/shopify_api/clients/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def request(request)
body: request.body.class == Hash ? T.unsafe(request.body).to_json : request.body,
), HTTParty::Response)

body = res.body.empty? ? {} : JSON.parse(res.body)
body = res.body.nil? || res.body.empty? ? {} : JSON.parse(res.body)
response = HttpResponse.new(code: res.code.to_i, headers: res.headers.to_h, body: body)

if response.headers["x-shopify-api-deprecated-reason"]
Expand Down
14 changes: 14 additions & 0 deletions test/clients/http_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ def test_request_with_empty_response_body
verify_http_request
end

# It doesn't really make sense for an HTTP body to ever be `nil`, but
# the underlying HTTParty library seems to use `nil` for the response
# body in situations where it doesn't need to parse it, e.g. when the
# response status is 204.
def test_request_with_nil_response_body
@success_body = {}

stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: "", headers: @response_headers, status: 204)

verify_http_request
end

def test_request_with_no_access_token
@session = ShopifyAPI::Auth::Session.new(shop: @shop)
@client = ShopifyAPI::Clients::HttpClient.new(session: @session, base_path: @base_path)
Expand Down

0 comments on commit 4e74cca

Please sign in to comment.