File: connection_spec.rb

package info (click to toggle)
ruby-protocol-http1 0.14.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 264 kB
  • sloc: ruby: 1,146; makefile: 4
file content (308 lines) | stat: -rw-r--r-- 8,755 bytes parent folder | download
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2022, by Samuel Williams.
# Copyright, 2020, by Bruno Sutic.

require 'protocol/http1/connection'
require 'protocol/http/body/buffered'

require_relative 'connection_context'

RSpec.describe Protocol::HTTP1::Connection do
	include_context Protocol::HTTP1::Connection
	
	describe '#read_request' do
		it "reads request without body" do
			client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n"
			client.stream.close
			
			authority, method, target, version, headers, body = server.read_request
			
			expect(authority).to be == 'localhost'
			expect(method).to be == 'GET'
			expect(target).to be == '/'
			expect(version).to be == 'HTTP/1.1'
			expect(headers).to be == {}
			expect(body).to be_nil
		end
		
		it "reads request without body after closing connection" do
			client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nAccept: */*\r\nHeader-0: value 1\r\n\r\n"
			client.stream.close
			
			authority, method, target, version, headers, body = server.read_request
			
			expect(authority).to be == 'localhost'
			expect(method).to be == 'GET'
			expect(target).to be == '/'
			expect(version).to be == 'HTTP/1.1'
			expect(headers).to be == {'accept' => ['*/*'], 'header-0' => ["value 1"]}
			expect(body).to be_nil
		end
		
		it "reads request with fixed body" do
			client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 11\r\n\r\nHello World"
			client.stream.close
			
			authority, method, target, version, headers, body = server.read_request
			
			expect(authority).to be == 'localhost'
			expect(method).to be == 'GET'
			expect(target).to be == '/'
			expect(version).to be == 'HTTP/1.1'
			expect(headers).to be == {}
			expect(body.join).to be == "Hello World"
		end
		
		it "reads request with chunked body" do
			client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n"
			client.stream.close
			
			authority, method, target, version, headers, body = server.read_request
			
			expect(authority).to be == 'localhost'
			expect(method).to be == 'GET'
			expect(target).to be == '/'
			expect(version).to be == 'HTTP/1.1'
			expect(headers).to be == {}
			expect(body.join).to be == "Hello World"
			expect(server).to be_persistent(version, method, headers)
		end
		
		it "fails with broken request" do
			client.stream.write "Accept: */*\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n"
			client.stream.close
			
			expect do
				server.read_request
			end.to raise_error(Protocol::HTTP1::InvalidRequest)
		end
		
		it "fails with missing version" do
			client.stream.write "GET foo\r\n"
			client.stream.close
			
			expect do
				server.read_request
			end.to raise_error(Protocol::HTTP1::InvalidRequest)
		end
	end

	describe '#persistent?' do
		describe "HTTP 1.0" do
			it "should not be persistent by default" do
				expect(server).not_to be_persistent("HTTP/1.0", "GET", {})
			end

			it "should be persistent if connection: keep-alive is set" do
				headers = Protocol::HTTP::Headers[
					"connection" => "keep-alive"
				]
				
				expect(server).to be_persistent("HTTP/1.0", "GET", headers)
			end

			it "should allow case-insensitive 'connection' value" do
				headers = Protocol::HTTP::Headers[
					"connection" => "Keep-Alive"
				]
				
				expect(server).to be_persistent("HTTP/1.0", "GET", headers)
			end
		end

		describe "HTTP 1.1" do
			it "should be persistent by default" do
				expect(server).to be_persistent("HTTP/1.1", "GET", {})
			end

			it "should not be persistent if connection: close is set" do
				headers = Protocol::HTTP::Headers[
					"connection" => "close"
				]
				
				expect(server).not_to be_persistent("HTTP/1.1", "GET", headers)
			end

			it "should allow case-insensitive 'connection' value" do
				headers = Protocol::HTTP::Headers[
					"connection" => "Close"
				]
				
				expect(server).not_to be_persistent("HTTP/1.1", "GET", headers)
			end
		end
	end
	
	describe '#read_response' do
		it "should read successful response" do
			server.stream.write("HTTP/1.1 200 Hello\r\nContent-Length: 0\r\n\r\n")
			server.stream.close
			
			version, status, reason, headers, body = client.read_response("GET")
			
			expect(version).to be == 'HTTP/1.1'
			expect(status).to be == 200
			expect(reason).to be == "Hello"
			expect(headers).to be == {}
			expect(body).to be_nil
		end
	end
	
	describe '#read_response_body' do
		context "with GET" do
			it "should ignore body for informational responses" do
				expect(client.read_response_body("GET", 100, {'content-length' => '10'})).to be_nil
			end
		end
		
		context "with HEAD" do
			it "can read length of head response" do
				body = client.read_response_body("HEAD", 200, {'content-length' => 3773})
				
				expect(body).to be_kind_of ::Protocol::HTTP::Body::Head
				expect(body.length).to be == 3773
				expect(body.read).to be nil
			end
			
			it "ignores zero length body" do
				body = client.read_response_body("HEAD", 200, {'content-length' => 0})
				
				expect(body).to be_nil
			end
		end
	end
	
	describe '#write_chunked_body' do
		let(:chunks) {["Hello", "World"]}
		let(:body) {::Protocol::HTTP::Body::Buffered.wrap(chunks)}
		
		it "can generate and read chunked response" do
			server.write_chunked_body(body, false)
			server.close
			
			headers = client.read_headers
			expect(headers).to be == [['transfer-encoding', 'chunked']]
			
			body = client.read_body(headers, false)
			expect(body.join).to be == chunks.join
		end
		
		it "can generate and read trailer" do
			chunks = ["Hello", "World"]
			
			server.write_headers({'trailer' => 'etag'})
			server.write_chunked_body(body, false, {'etag' => 'abcd'})
			server.close
			
			headers = client.read_headers
			expect(headers).to be == [['trailer', 'etag'], ['transfer-encoding', 'chunked']]
			
			body = client.read_body(headers, false)
			expect(body.join).to be == chunks.join
			
			expect(headers).to include('etag')
		end
	end
	
	describe '#write_fixed_length_body' do
		let(:chunks) {["Hello", "World"]}
		let(:body) {::Protocol::HTTP::Body::Buffered.wrap(chunks)}
		
		it "can generate and read chunked response" do
			server.write_fixed_length_body(body, 10, false)
			server.close
			
			headers = client.read_headers
			expect(headers).to be == [['content-length', '10']]
			
			body = client.read_body(headers, false)
			expect(body.join).to be == chunks.join
		end
	end
	
	describe '#write_body' do
		let(:body) {double}
		
		it "can write empty body" do
			expect(body).to receive(:empty?).and_return(true)
			expect(body).to receive(:length).and_return(false)
			
			expect(server).to receive(:write_empty_body)
			server.write_body("HTTP/1.0", body)
		end
		
		it "can write fixed length body" do
			expect(body).to receive(:length).and_return(1024)
			
			expect(server).to receive(:write_fixed_length_body)
			server.write_body("HTTP/1.0", body)
		end
		
		it "can write chunked body" do
			expect(server.persistent).to be true
			
			expect(body).to receive(:empty?).and_return(false)
			expect(body).to receive(:length).and_return(nil)
			
			expect(server).to receive(:write_chunked_body)
			server.write_body("HTTP/1.1", body)
		end
		
		it "can write fixed length body for HTTP/1.1" do
			expect(body).to receive(:length).and_return(1024)
			
			expect(server).to receive(:write_fixed_length_body)
			server.write_body("HTTP/1.1", body)
		end
		
		it "can write closed body" do
			expect(server.persistent).to be true
			
			expect(body).to receive(:empty?).and_return(false)
			expect(body).to receive(:length).and_return(nil)
			
			expect(server).to receive(:write_body_and_close)
			server.write_body("HTTP/1.0", body)
		end
	end
	
	context 'bad requests' do
		it "should fail with negative content length" do
			client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: -1\r\n\r\nHello World"
			client.stream.close
			
			expect do
				server.read_request
			end.to raise_error(Protocol::HTTP1::BadRequest)
		end
		
		it "should fail with invalid headers" do
			client.stream.write "GET / HTTP/1.1\r\nHost: \000localhost\r\n\r\nHello World"
			client.stream.close
			
			expect do
				server.read_request
			end.to raise_error(Protocol::HTTP1::BadHeader)
		end
	end
	
	context 'bad responses' do
		it 'should fail if headers contain \r characters' do
			expect do
				server.write_headers(
					[["id", "5\rSet-Cookie: foo-bar"]]
				)
			end.to raise_error(Protocol::HTTP1::BadHeader)
		end
		
		it 'should fail if headers contain \n characters' do
			expect do
				server.write_headers(
					[["id", "5\nSet-Cookie: foo-bar"]]
				)
			end.to raise_error(Protocol::HTTP1::BadHeader)
		end
	end
end