Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle HTTP 204 response bodies #1098

Merged
merged 7 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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