File: net_http_shared.rb

package info (click to toggle)
ruby-webmock 3.26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: ruby: 12,859; makefile: 6
file content (196 lines) | stat: -rw-r--r-- 6,273 bytes parent folder | download | duplicates (2)
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
require 'set'

shared_examples_for "Net::HTTP" do
  describe "when making real requests", net_connect: true do
    let(:port){ WebMockServer.instance.port }

    before(:each) do
      @http = Net::HTTP.new("localhost", port)
    end

    it "should return a Net::ReadAdapter from response.body when a real request is made with a block and #read_body", net_connect: true do
      response = Net::HTTP.new("localhost", port).request_get('/') { |r| r.read_body { } }
      expect(response.body).to be_a(Net::ReadAdapter)
    end

    it "should return response that returns request uri" do
      uri = URI.parse("http://localhost:#{port}/")
      response = Net::HTTP.get_response(uri)
      expect(response.uri).to eql(uri)
    end

    it "should handle requests with block passed to read_body", net_connect: true do
      body = "".dup
      req = Net::HTTP::Get.new("/")
      Net::HTTP.start("localhost", port) do |http|
        http.request(req) do |res|
          res.read_body do |str|
            body << str
          end
        end
      end
      expect(body).to match(/hello world/)
    end

    it "should connect only once when connected on start", net_connect: true do
      @http = Net::HTTP.new('localhost', port)
      socket_before_request = socket_after_request = nil
      @http.start {|conn|
        socket_before_request = conn.instance_variable_get(:@socket)
        conn.request(Net::HTTP::Get.new("/"))
        socket_after_request = conn.instance_variable_get(:@socket)
      }

      if !defined?(WebMock::NetHTTPUtility) || WebMock::Config.instance.net_http_connect_on_start
        expect(socket_before_request).to be_a(Net::BufferedIO)
        expect(socket_after_request).to be_a(Net::BufferedIO)
        expect(socket_after_request).to be(socket_before_request)
      else
        expect(socket_before_request).to be_a(StubSocket)
        expect(socket_after_request).to be_a(Net::BufferedIO)
      end
    end

    xit "should allow sending multiple requests when persisted", net_connect: true do
      @http = Net::HTTP.new('example.org')
      @http.start
      expect(@http.get("/")).to be_a(Net::HTTPSuccess)
      expect(@http.get("/")).to be_a(Net::HTTPSuccess)
      expect(@http.get("/")).to be_a(Net::HTTPSuccess)
      @http.finish
    end

    xit "should not leak file descriptors", net_connect: true do
      sockets = Set.new

      @http = Net::HTTP.new('example.org')
      @http.start
      sockets << @http.instance_variable_get(:@socket)
      @http.get("/")
      sockets << @http.instance_variable_get(:@socket)
      @http.get("/")
      sockets << @http.instance_variable_get(:@socket)
      @http.get("/")
      sockets << @http.instance_variable_get(:@socket)
      @http.finish

      if !defined?(WebMock::NetHTTPUtility) || WebMock.net_http_connect_on_start?(Addressable::URI.parse("http://example.com/"))
        expect(sockets.length).to eq(1)
        expect(sockets.to_a[0]).to be_a(Net::BufferedIO)
      else
        expect(sockets.length).to eq(2)
        expect(sockets.to_a[0]).to be_a(StubSocket)
        expect(sockets.to_a[1]).to be_a(Net::BufferedIO)
      end

      expect(sockets.all?(&:closed?)).to be(true)
    end

    it "should pass the read_timeout value on", net_connect: true do
      @http = Net::HTTP.new('localhost', port)
      read_timeout = @http.read_timeout + 1
      @http.read_timeout = read_timeout
      @http.start {|conn|
        conn.request(Net::HTTP::Get.new("/"))
        socket = conn.instance_variable_get(:@socket)
        expect(socket.read_timeout).to eq(read_timeout)
      }
    end

    describe "without start" do
      it "should close connection after a real request" do
        @http.get('/') { }
        expect(@http).not_to be_started
      end

      it "should execute block exactly once" do
        times = 0
        @http.get('/') { times += 1 }
        expect(times).to eq(1)
      end

      it "should have socket open during a real request" do
        socket_id = nil
        @http.get('/') {
          socket_id = @http.instance_variable_get(:@socket).object_id
        }
        expect(socket_id).not_to be_nil
      end

      it "should be started during a real request" do
        started = nil
        @http.get('/') {
          started = @http.started?
        }
        expect(started).to eq(true)
        expect(@http.started?).to eq(false)
      end
    end

    describe "with start" do
      it "should close connection after a real request" do
        @http.start {|conn| conn.get('/') { } }
        expect(@http).not_to be_started
      end

      it "should execute block exactly once" do
        times = 0
        @http.start {|conn| conn.get('/') { times += 1 }}
        expect(times).to eq(1)
      end

      it "should have socket open during a real request" do
        socket_id = nil
        @http.start {|conn| conn.get('/') {
            socket_id = conn.instance_variable_get(:@socket).object_id
          }
        }
        expect(socket_id).not_to be_nil
      end

      it "should be started during a real request" do
        started = nil
        @http.start {|conn| conn.get('/') {
            started = conn.started?
          }
        }
        expect(started).to eq(true)
        expect(@http.started?).to eq(false)
      end
    end

    describe "with start without request block" do
      it "should close connection after a real request" do
        @http.start {|conn| conn.get('/') }
        expect(@http).not_to be_started
      end

      it "should have socket open during a real request" do
        socket_id = nil
        @http.start {|conn|
          socket_id = conn.instance_variable_get(:@socket).object_id
        }
        expect(socket_id).not_to be_nil
      end

      it "should be started during a real request" do
        started = nil
        @http.start {|conn|
          started = conn.started?
        }
        expect(started).to eq(true)
        expect(@http.started?).to eq(false)
      end
    end

    describe "with start without a block and finish" do
      it "should gracefully start and close connection" do
        @http.start
        @http.get("/")
        expect(@http).to be_started
        @http.finish
        expect(@http).not_to be_started
      end
    end
  end
end