Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 150 lines (130 sloc) 5.007 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
shared_context "callbacks" do
  describe "when after_request callback is declared" do
    before(:each) do
      WebMock.reset_callbacks
      stub_request(:get, "http://www.example.com")
    end

    it "should not invoke callback unless request is made" do
      WebMock.after_request {
        @called = true
      }
      @called.should == nil
    end

    it "should invoke a callback after request is made" do
      WebMock.after_request {
        @called = true
      }
      http_request(:get, "http://www.example.com/")
      @called.should == true
    end

    it "should not invoke a callback if this http library should be ignored" do
      WebMock.after_request(:except => [http_library()]) {
        @called = true
      }
      http_request(:get, "http://www.example.com/")
      @called.should == nil
    end

    it "should invoke a callback even if other http libraries should be ignored" do
      WebMock.after_request(:except => [:other_lib]) {
        @called = true
      }
      http_request(:get, "http://www.example.com/")
      @called.should == true
    end

    it "should pass request signature to the callback" do
      WebMock.after_request(:except => [:other_lib]) do |request_signature, _|
        @request_signature = request_signature
      end
      http_request(:get, "http://www.example.com/")
      @request_signature.uri.to_s.should == "http://www.example.com:80/"
    end

    after(:each) do
      WebMock::StubRegistry.instance.global_stubs.clear
    end

    it 'passes the same request signature instance to the callback that was passed to the global stub callback' do
      global_stub_request_sig = after_request_request_sig = nil
      WebMock.globally_stub_request do |request_sig|
        global_stub_request_sig = request_sig
        nil
      end

      WebMock.after_request do |request_sig, _|
        after_request_request_sig = request_sig
      end

      http_request(:get, "http://www.example.com/")
      global_stub_request_sig.should be(after_request_request_sig)
    end

    context "passing response to callback" do
      context "when request is stubbed" do
        before(:each) do
          stub_request(:get, "http://www.example.com").
          to_return(
            :status => ["200", "hello"],
            :headers => {'Content-Length' => '666', 'Hello' => 'World'},
            :body => "foo bar"
          )
          WebMock.after_request(:except => [:other_lib]) do |_, response|
            @response = response
          end
          http_request(:get, "http://www.example.com/")
        end

        it "should pass response to callback with the status and message" do
          @response.status.should == ["200", "hello"]
        end

        it "should pass response to callback with headers" do
          @response.headers.should == {
            'Content-Length' => '666',
            'Hello' => 'World'
          }
        end

        it "should pass response to callback with body" do
          @response.body.should == "foo bar"
        end
      end

      describe "when request is not stubbed", :net_connect => true do
        before(:each) do
          WebMock.reset!
          WebMock.allow_net_connect!
          WebMock.after_request(:except => [:other_lib]) do |_, response|
            @response = response
          end
          http_request(:get, "http://www.example.com/")
        end

        it "should pass real response to callback with status and message" do
          # not supported by em-http-request, it always returns "unknown" for http_reason
          # not supported by excon, it only returns a status code
          unless [:em_http_request, :excon].include?(http_library)
            @response.status[0].should == 302
            @response.status[1].should == "Found"
          end
        end

        it "should pass real response to callback with headers" do
          @response.headers["Content-Length"].should == "0"
        end

        it "should pass response to callback with body" do
          @response.body.size.should == 0
        end
      end
    end

    it "should invoke multiple callbacks in order of their declarations" do
      WebMock.after_request { @called = 1 }
      WebMock.after_request { @called += 1 }
      http_request(:get, "http://www.example.com/")
      @called.should == 2
    end

    it "should invoke callbacks only for real requests if requested", :net_connect => true do
      WebMock.after_request(:real_requests_only => true) { @called = true }
      http_request(:get, "http://www.example.com/")
      @called.should == nil
      WebMock.allow_net_connect!
      http_request(:get, "http://www.example.net/")
      @called.should == true
    end

    it "should not invoke any callbacks after callbacks were reset" do
      WebMock.after_request { @called = 1 }
      WebMock.reset_callbacks
      stub_request(:get, "http://www.example.com/")
      http_request(:get, "http://www.example.com/")
      @called.should == nil
    end
  end
end
Something went wrong with that request. Please try again.