Skip to content

Commit

Permalink
API-7806: fix a problem with fetching counts with the http_method_ove…
Browse files Browse the repository at this point in the history
…rride: true option supplied. (#175)

* API-7806: fix bug with http_method_override requests that request pagination

* API-7806: version bump
  • Loading branch information
eeggers authored Nov 17, 2021
1 parent 73c890b commit ec60d86
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v1.5.4
- Fix a problem with fetching counts with the http_method_override: true option supplied.

v1.5.3
- Add support for http_method_override (true/false) option to client, which when supplied results in a POST with appropriate X-HTTP-Method-Override header.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.3
1.5.4
14 changes: 12 additions & 2 deletions lib/spark_api/faraday_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def on_complete(env)
if paging.nil?
results = response
else
q = CGI.parse(env[:url].query)
q = if http_method_override_request?(env)
CGI.parse(env[:request_body])
else
CGI.parse(env[:url].query)
end
if q.key?("_pagination") && q["_pagination"].first == "count"
results = paging['TotalRows']
else
Expand Down Expand Up @@ -87,7 +91,13 @@ def decompress_body(env)

env[:body]
end


private

def http_method_override_request?(env)
env[:request_headers]["X-HTTP-Method-Override"] == "GET"
end

end

Faraday::Response.register_middleware :spark_api => FaradayMiddleware
Expand Down
9 changes: 6 additions & 3 deletions spec/support/stub_api_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ def stub_api_request(meth, service_path, stub_fixture, body, opts, &block)

def with_args(service_path, opts, body=nil)
if body.is_a?(Hash)
body = { :D => body } unless body.empty?
elsif !body.nil?
body = { :D => body } unless body.empty?
body_str = body.nil? ? body : MultiJson.dump(body)
elsif body =~ /\.json\z/
body = MultiJson.load(fixture(body).read)
body_str = body.nil? ? body : MultiJson.dump(body)
else
body_str = body
end
body_str = body.nil? ? body : MultiJson.dump(body)

params = {:ApiUser => "foobar", :AuthToken => "c401736bf3d3f754f07c04e460e09573"}.merge(opts)
query = {:ApiSig => get_signature(service_path, params, body_str) }
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/spark_api/models/listing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@
count = Listing.count()
expect(count).to eq(2001)
end

on_get_it "should return the count even if http_method_override: true is passed" do
stub_api_post('/listings', '_pagination=count', 'count.json')
count = Listing.count(http_method_override: true)
expect(count).to eq(2001)
end
end

context "/listings/<listing_id>", :support do
Expand Down

0 comments on commit ec60d86

Please sign in to comment.