Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 274 lines (234 sloc) 9.705 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
require 'spec_helper'
require 'ostruct'
require 'acceptance/webmock_shared'
require 'acceptance/net_http/net_http_spec_helper'
require 'acceptance/net_http/net_http_shared'

include NetHTTPSpecHelper

describe "Net:HTTP" do
  include_examples "with WebMock"

  let(:port){ WebMockServer.instance.port }

  describe "marshalling" do
    class TestMarshalingInWebMockNetHTTP
      attr_accessor :r
    end
    before(:each) do
      @b = TestMarshalingInWebMockNetHTTP.new
    end
    after(:each) do
      WebMock.enable!
    end
    it "should be possible to load object marshalled when webmock was disabled" do
      WebMock.disable!
      original_constants = [
        Net::HTTP::Get,
        Net::HTTP::Post,
        Net::HTTP::Put,
        Net::HTTP::Delete,
        Net::HTTP::Head,
        Net::HTTP::Options
      ]
      @b.r = original_constants
      original_serialized = Marshal.dump(@b)
      Marshal.load(original_serialized)
      WebMock.enable!
      Marshal.load(original_serialized)
    end

    it "should be possible to load object marshalled when webmock was enabled" do
      WebMock.enable!
      new_constants = [
        Net::HTTP::Get,
        Net::HTTP::Post,
        Net::HTTP::Put,
        Net::HTTP::Delete,
        Net::HTTP::Head,
        Net::HTTP::Options
      ]
      @b.r = new_constants
      new_serialized = Marshal.dump(@b)
      Marshal.load(new_serialized)
      WebMock.disable!
      Marshal.load(new_serialized)
    end
  end

  describe "constants" do
    it "should still have const Get defined on replaced Net::HTTP" do
      Object.const_get("Net").const_get("HTTP").const_defined?("Get").should be_true
    end

    it "should still have const Get within constants on replaced Net::HTTP" do
      Object.const_get("Net").const_get("HTTP").constants.map(&:to_s).should include("Get")
    end

    it "should still have const Get within constants on replaced Net::HTTP" do
      Object.const_get("Net").const_get("HTTP").const_get("Get").should_not be_nil
    end

    if Module.method(:const_defined?).arity != 1
      it "should still have const Get defined (and not inherited) on replaced Net::HTTP" do
        Object.const_get("Net").const_get("HTTP").const_defined?("Get", false).should be_true
      end
    end

    if Module.method(:const_get).arity != 1
      it "should still be able to get non inherited constant Get on replaced Net::HTTP" do
        Object.const_get("Net").const_get("HTTP").const_get("Get", false).should_not be_nil
      end
    end

    if Module.method(:constants).arity != 0
      it "should still Get within non inherited constants on replaced Net::HTTP" do
        Object.const_get("Net").const_get("HTTP").constants(false).map(&:to_s).should include("Get")
      end
    end

    describe "after WebMock is disabled" do
      after(:each) do
        WebMock.enable!
      end
      it "Net::HTTP should have the same constants" do
        orig_consts_number = WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP.constants.size
        Net::HTTP.send(:const_set, "TEST_CONST", 10)
        Net::HTTP.constants.size.should == orig_consts_number + 1
        WebMock.disable!
        Net::HTTP.constants.size.should == orig_consts_number + 1
      end
    end
  end

  it "should work with block provided" do
    stub_http_request(:get, "www.example.com").to_return(:body => "abc"*100000)
    Net::HTTP.start("www.example.com") { |query| query.get("/") }.body.should == "abc"*100000
  end

  it "should handle multiple values for the same response header" do
    stub_http_request(:get, "www.example.com").to_return(:headers => { 'Set-Cookie' => ['foo=bar', 'bar=bazz'] })
    response = Net::HTTP.get_response(URI.parse("http://www.example.com/"))
    response.get_fields('Set-Cookie').should == ['bar=bazz', 'foo=bar']
  end

  it "should yield block on response" do
    stub_http_request(:get, "www.example.com").to_return(:body => "abc")
    response_body = ""
    http_request(:get, "http://www.example.com/") do |response|
      response_body = response.body
    end
    response_body.should == "abc"
  end

  it "should handle Net::HTTP::Post#body" do
    stub_http_request(:post, "www.example.com").with(:body => "my_params").to_return(:body => "abc")
    req = Net::HTTP::Post.new("/")
    req.body = "my_params"
    Net::HTTP.start("www.example.com") { |http| http.request(req)}.body.should == "abc"
  end

  it "should handle Net::HTTP::Post#body_stream" do
    stub_http_request(:post, "www.example.com").with(:body => "my_params").to_return(:body => "abc")
    req = Net::HTTP::Post.new("/")
    req.body_stream = StringIO.new("my_params")
    Net::HTTP.start("www.example.com") { |http| http.request(req)}.body.should == "abc"
  end

  it "should behave like Net::HTTP and raise error if both request body and body argument are set" do
    stub_http_request(:post, "www.example.com").with(:body => "my_params").to_return(:body => "abc")
    req = Net::HTTP::Post.new("/")
    req.body = "my_params"
    lambda {
      Net::HTTP.start("www.example.com") { |http| http.request(req, "my_params")}
    }.should raise_error("both of body argument and HTTPRequest#body set")
  end

  it "should return a Net::ReadAdapter from response.body when a stubbed request is made with a block and #read_body" do
    WebMock.stub_request(:get, 'http://example.com/').to_return(:body => "the body")
    response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
    response.body.should be_a(Net::ReadAdapter)
  end

  it "should have request 1 time executed in registry after 1 real request", :net_connect => true do
    WebMock.allow_net_connect!
    http = Net::HTTP.new('localhost', port)
    http.get('/') {}
    WebMock::RequestRegistry.instance.requested_signatures.hash.size.should == 1
    WebMock::RequestRegistry.instance.requested_signatures.hash.values.first.should == 1
  end

  describe "connecting on Net::HTTP.start" do
    before(:each) do
      @http = Net::HTTP.new('www.google.com', 443)
      @http.use_ssl = true
      @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    describe "when net http is allowed" do
      it "should not connect to the server until the request", :net_connect => true do
        WebMock.allow_net_connect!
        @http.start {|conn|
          conn.peer_cert.should be_nil
        }
      end

      it "should connect to the server on start", :net_connect => true do
        WebMock.allow_net_connect!(:net_http_connect_on_start => true)
        @http.start {|conn|
          cert = OpenSSL::X509::Certificate.new conn.peer_cert
          cert.should be_a(OpenSSL::X509::Certificate)
        }
      end

    end

    describe "when net http is disabled and allowed only for some hosts" do
      it "should not connect to the server until the request", :net_connect => true do
        WebMock.disable_net_connect!(:allow => "www.google.com")
        @http.start {|conn|
          conn.peer_cert.should be_nil
        }
      end

      it "should connect to the server on start", :net_connect => true do
        WebMock.disable_net_connect!(:allow => "www.google.com", :net_http_connect_on_start => true)
        @http.start {|conn|
          cert = OpenSSL::X509::Certificate.new conn.peer_cert
          cert.should be_a(OpenSSL::X509::Certificate)
        }
      end

      it "should connect to the server if the URI matches an regex", :net_connect => true do
        WebMock.disable_net_connect!(:allow => /google.com/)
        response = Net::HTTP.get('www.google.com','/')
      end

      it "should connect to the server if the URI matches any regex the array", :net_connect => true do
        WebMock.disable_net_connect!(:allow => [/google.com/, /yahoo.com/])
        response = Net::HTTP.get('www.google.com','/')
      end

    end

  end

  describe "when net_http_connect_on_start is true" do
    before(:each) do
      WebMock.allow_net_connect!(:net_http_connect_on_start => true)
    end
    it_should_behave_like "Net::HTTP"
  end

  describe "when net_http_connect_on_start is false" do
    before(:each) do
      WebMock.allow_net_connect!(:net_http_connect_on_start => false)
    end
    it_should_behave_like "Net::HTTP"
  end

  describe 'after_request callback support', :net_connect => true do
    let(:expected_body_regex) { /hello world/ }

    before(:each) do
      WebMock.allow_net_connect!
      @callback_invocation_count = 0
      WebMock.after_request do |_, response|
        @callback_invocation_count += 1
        @callback_response = response
      end
    end

    after(:each) do
      WebMock.reset_callbacks
    end

    def perform_get_with_returning_block
      http_request(:get, "http://localhost:#{port}/") do |response|
        return response.body
      end
    end

    it "should support the after_request callback on an request with block and read_body" do
      response_body = ''
      http_request(:get, "http://localhost:#{port}/") do |response|
        response.read_body { |fragment| response_body << fragment }
      end
      response_body.should =~ expected_body_regex

      @callback_response.body.should == response_body
    end

    it "should support the after_request callback on a request with a returning block" do
      response_body = perform_get_with_returning_block
      response_body.should =~ expected_body_regex
      @callback_response.should be_instance_of(WebMock::Response)
      @callback_response.body.should == response_body
    end

    it "should only invoke the after_request callback once, even for a recursive post request" do
      Net::HTTP.new('localhost', port).post('/', nil)
      @callback_invocation_count.should == 1
    end
  end
end
Something went wrong with that request. Please try again.