Skip to content
This repository

Fix Net::HTTP adapter so that it returns `nil` for an empty body response. #190

Merged
merged 1 commit into from over 1 year ago

2 participants

Myron Marston Bartosz Blimke
Myron Marston
Collaborator

This mirrors the real behavior of Net::HTTP and is the source of myronmarston/vcr#173.

A couple things to note:

  • Rather than hitting an external URL (httpstat.us/204), this should probably hit the local webmock server; however, I can't figure out how to make the webmock server return a different response for different requests since it's writing directly to the socket w/o any request context available. Maybe it should be refactored to use rack or sinatra?
  • I have no idea why, but Curb is returning a 400 Bad Request response for the request. Weird. Not sure why or how to fix it.
Myron Marston Fix Net::HTTP adapter so that it returns `nil` for an empty body resp…
…onse.

This mirrors the real behavior of Net::HTTP and is the source of myronmarston/vcr#173.

A couple things to note:

- Rather than hitting an external URL (httpstat.us/204), this should probably
  hit the local webmock server; however, I can't figure out how to make the
  webmock server return a different response for different requests since it's
  writing directly to the socket w/o any request context available. Maybe it
  should be refactored to use rack or sinatra?
- I have no idea why, but Curb is returning a 400 Bad Request response for
  the request. Weird. Not sure why or how to fix it.
f7b3230
Bartosz Blimke
Owner

Thanks Myron. Clever approach to test that.

I'll create a patch release once I figure out what's going on with curb.

Myron Marston
Collaborator

What are your thoughts on the idea of refactoring the webmock server? This spec is currently hitting an external URL, as is my spec in #185. It'd be nice to make them hit the webmock server but they need a particular response and I couldn't figure out how to conditionally return a response based on the request in your current setup.

Bartosz Blimke bblimke merged commit e015405 into from July 23, 2012
Bartosz Blimke bblimke closed this July 23, 2012
Mike Piccolo mfpiccolo referenced this pull request from a commit January 11, 2014
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 11, 2012
Myron Marston Fix Net::HTTP adapter so that it returns `nil` for an empty body resp…
…onse.

This mirrors the real behavior of Net::HTTP and is the source of myronmarston/vcr#173.

A couple things to note:

- Rather than hitting an external URL (httpstat.us/204), this should probably
  hit the local webmock server; however, I can't figure out how to make the
  webmock server return a different response for different requests since it's
  writing directly to the socket w/o any request context available. Maybe it
  should be refactored to use rack or sinatra?
- I have no idea why, but Curb is returning a 400 Bad Request response for
  the request. Weird. Not sure why or how to fix it.
f7b3230
This page is out of date. Refresh to see the latest.
5  lib/webmock/http_lib_adapters/net_http.rb
@@ -128,7 +128,10 @@ def start_with_conditional_connect(&block)
128 128
 
129 129
         def build_net_http_response(webmock_response, &block)
130 130
           response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1])
131  
-          response.instance_variable_set(:@body, webmock_response.body)
  131
+          body = webmock_response.body
  132
+          body = nil if body.to_s == ''
  133
+
  134
+          response.instance_variable_set(:@body, body)
132 135
           webmock_response.headers.to_a.each do |name, values|
133 136
             values = [values] unless values.is_a?(Array)
134 137
             values.each do |value|
13  spec/acceptance/shared/complex_cross_concern_behaviors.rb
@@ -17,5 +17,18 @@
17 17
     played_back_response.headers.keys.should include('Set-Cookie')
18 18
     played_back_response.should == real_response
19 19
   end
  20
+
  21
+  let(:no_content_url) { 'http://httpstat.us/204' }
  22
+  [nil, ''].each do |stub_val|
  23
+    it "returns the same value (nil or "") for a request stubbed as #{stub_val.inspect} that a real empty response has" do
  24
+      WebMock.allow_net_connect!
  25
+
  26
+      real_response = http_request(:get, no_content_url)
  27
+      stub_request(:get, no_content_url).to_return(:status => 204, :body => stub_val)
  28
+      stubbed_response = http_request(:get, no_content_url)
  29
+
  30
+      stubbed_response.body.should eq(real_response.body)
  31
+    end
  32
+  end
20 33
 end
21 34
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.