segregate 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/segregate/http_headers.rb +64 -0
- data/lib/segregate/version.rb +1 -1
- data/lib/segregate.rb +108 -75
- data/spec/segregate_spec.rb +500 -324
- metadata +16 -1
data/spec/segregate_spec.rb
CHANGED
@@ -6,26 +6,8 @@ describe Segregate do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
describe '::new' do
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'has the inital values' do
|
14
|
-
@parser = Segregate.new
|
15
|
-
expect(@parser.request?).to be_false
|
16
|
-
expect(@parser.response?).to be_false
|
17
|
-
expect(@parser.uri).to be_nil
|
18
|
-
expect(@parser.request_line).to be_nil
|
19
|
-
expect(@parser.request_method).to be_nil
|
20
|
-
expect(@parser.request_url).to be_nil
|
21
|
-
expect(@parser.status_line).to be_nil
|
22
|
-
expect(@parser.status_code).to be_nil
|
23
|
-
expect(@parser.status_phrase).to be_nil
|
24
|
-
expect(@parser.http_version).to eq [nil, nil]
|
25
|
-
expect(@parser.major_http_version).to be_nil
|
26
|
-
expect(@parser.minor_http_version).to be_nil
|
27
|
-
expect(@parser.headers).to be_empty
|
28
|
-
expect(@parser.body).to be_empty
|
9
|
+
it "creates a new parser" do
|
10
|
+
expect(Segregate.new).to be_an_instance_of Segregate
|
29
11
|
end
|
30
12
|
end
|
31
13
|
|
@@ -34,130 +16,214 @@ describe Segregate do
|
|
34
16
|
@parser = Segregate.new
|
35
17
|
end
|
36
18
|
|
37
|
-
describe '#
|
38
|
-
it '
|
39
|
-
expect
|
19
|
+
describe '#method_missing' do
|
20
|
+
it 'raises an error if URI does not respond' do
|
21
|
+
expect{ @parser.not_uri }.to raise_error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#parse_data' do
|
26
|
+
it 'raises an error if an incorret fist line is passed' do
|
27
|
+
expect{ @parser.parse_data("NOT A HTTP LINE\r\n") }.to raise_error RuntimeError, 'ERROR: Unknown first line: NOT A HTTP LINE'
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'raises an error if an incorret request method is passed' do
|
31
|
+
expect{ @parser.parse_data("FAIL /endpoint HTTP/1.1\r\n") }.to raise_error RuntimeError, 'ERROR: Unknown http method: FAIL'
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'can accept partial first lines' do
|
35
|
+
@parser.parse_data "GET /endpoint"
|
36
|
+
@parser.parse_data " HTTP/1.1\r\n"
|
37
|
+
expect(@parser.request_line).to eq "GET /endpoint HTTP/1.1"
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'can accept partial headers' do
|
41
|
+
@parser.parse_data "GET /endpoint HTTP/1.1\r\n"
|
42
|
+
@parser.parse_data "host: www.goo"
|
43
|
+
@parser.parse_data "gle.com\r\n"
|
44
|
+
expect(@parser.headers.host).to eq 'www.google.com'
|
40
45
|
end
|
41
46
|
|
42
|
-
it '
|
43
|
-
|
47
|
+
it 'can accept partial body' do
|
48
|
+
@parser.parse_data "GET /endpoint HTTP/1.1\r\n"
|
49
|
+
@parser.parse_data "transfer-encoding: chunked\r\n\r\n"
|
50
|
+
@parser.parse_data "9\r\n"
|
51
|
+
@parser.parse_data "12345\r\n"
|
52
|
+
@parser.parse_data "6789\r\n"
|
53
|
+
expect(@parser.body).to eq "123456789"
|
44
54
|
end
|
45
55
|
|
46
|
-
it '
|
47
|
-
|
56
|
+
it 'can accept partial body' do
|
57
|
+
@parser.parse_data "GET /endpoint HTTP/1.1\r\n"
|
58
|
+
@parser.parse_data "content-length: 9\r\n\r\n"
|
59
|
+
@parser.parse_data "12345\r\n"
|
60
|
+
@parser.parse_data "6789\r\n"
|
61
|
+
expect(@parser.body).to eq "123456789"
|
48
62
|
end
|
49
63
|
end
|
50
64
|
|
51
|
-
|
52
|
-
|
53
|
-
@parser.
|
65
|
+
describe '#uri' do
|
66
|
+
it 'returns nil' do
|
67
|
+
expect(@parser.uri).to be_nil
|
54
68
|
end
|
69
|
+
end
|
55
70
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
71
|
+
describe '#type' do
|
72
|
+
it 'is an instance of juncture' do
|
73
|
+
expect(@parser.type).to be_an_instance_of Juncture
|
74
|
+
end
|
60
75
|
|
61
|
-
|
62
|
-
|
63
|
-
|
76
|
+
it 'is in a nil state' do
|
77
|
+
expect(@parser.type.state).to be_nil
|
78
|
+
end
|
79
|
+
end
|
64
80
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
81
|
+
describe '#state' do
|
82
|
+
it 'is an instance of juncture' do
|
83
|
+
expect(@parser.state).to be_an_instance_of Juncture
|
84
|
+
end
|
70
85
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
end
|
86
|
+
it 'is in a waiting state' do
|
87
|
+
expect(@parser.state.state).to eq :waiting
|
88
|
+
end
|
89
|
+
end
|
76
90
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
91
|
+
describe '#http_version' do
|
92
|
+
it 'returns [nil, nil]' do
|
93
|
+
expect(@parser.http_version).to eq [nil, nil]
|
94
|
+
end
|
95
|
+
end
|
82
96
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
97
|
+
describe '#request_method' do
|
98
|
+
it 'returns nil' do
|
99
|
+
expect(@parser.request_method).to be_nil
|
100
|
+
end
|
101
|
+
end
|
88
102
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
expect(@parser.request_line).to eq "GET /endpoint HTTP/1.2"
|
93
|
-
end
|
103
|
+
describe '#status_code' do
|
104
|
+
it 'returns nil' do
|
105
|
+
expect(@parser.status_code).to be_nil
|
94
106
|
end
|
107
|
+
end
|
95
108
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
109
|
+
describe '#status_phrase' do
|
110
|
+
it 'returns nil' do
|
111
|
+
expect(@parser.status_phrase).to be_nil
|
100
112
|
end
|
113
|
+
end
|
101
114
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
115
|
+
describe '#headers' do
|
116
|
+
it 'is an instance of hashie mash' do
|
117
|
+
expect(@parser.headers).to be_an_instance_of Hashie::Mash
|
106
118
|
end
|
107
119
|
|
108
|
-
|
109
|
-
|
110
|
-
expect(@parser.response?).to be_an_instance_of FalseClass
|
111
|
-
end
|
120
|
+
it 'is empty' do
|
121
|
+
expect(@parser.headers).to be_empty
|
112
122
|
end
|
123
|
+
end
|
113
124
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
125
|
+
describe '#body' do
|
126
|
+
it 'is empty' do
|
127
|
+
expect(@parser.body).to be_empty
|
128
|
+
end
|
129
|
+
end
|
118
130
|
|
119
|
-
|
120
|
-
|
121
|
-
|
131
|
+
describe '#request?' do
|
132
|
+
it 'returns false' do
|
133
|
+
expect(@parser.request?).to be_false
|
122
134
|
end
|
135
|
+
end
|
123
136
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
137
|
+
describe '#response?' do
|
138
|
+
it 'returns false' do
|
139
|
+
expect(@parser.response?).to be_false
|
140
|
+
end
|
141
|
+
end
|
128
142
|
|
129
|
-
|
130
|
-
|
143
|
+
describe '#headers_complete?' do
|
144
|
+
it 'returns false' do
|
145
|
+
expect(@parser.headers_complete?).to be_false
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe '#done?' do
|
150
|
+
it 'returns false' do
|
151
|
+
expect(@parser.done?).to be_false
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe '#request_line' do
|
156
|
+
it 'returns nil' do
|
157
|
+
expect(@parser.request_line).to be_nil
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe '#status_line' do
|
162
|
+
it 'returns nil' do
|
163
|
+
expect(@parser.status_line).to be_nil
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe '#request_url' do
|
168
|
+
it 'returns nil' do
|
169
|
+
expect(@parser.request_url).to be_nil
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe '#major_http_version' do
|
174
|
+
it 'returns nil' do
|
175
|
+
expect(@parser.major_http_version).to be_nil
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe '#minor_http_version' do
|
180
|
+
it 'returns nil' do
|
181
|
+
expect(@parser.minor_http_version).to be_nil
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'a request line has been parsed' do
|
186
|
+
before(:each) do
|
187
|
+
@parser.parse_data "GET /endpoint HTTP/1.1\r\n"
|
188
|
+
end
|
189
|
+
|
190
|
+
describe '#uri' do
|
191
|
+
it 'is an instance of URI' do
|
192
|
+
expect(@parser.uri).to be_an_instance_of URI::Generic
|
131
193
|
end
|
132
194
|
end
|
133
195
|
|
134
|
-
describe '#
|
135
|
-
it '
|
136
|
-
expect(@parser.
|
196
|
+
describe '#respond_to?' do
|
197
|
+
it 'responds to segregate methods' do
|
198
|
+
expect(@parser.respond_to?(:request_line)).to be_true
|
137
199
|
end
|
138
200
|
|
139
|
-
it '
|
140
|
-
expect(@parser.
|
201
|
+
it 'responds to uri methods' do
|
202
|
+
expect(@parser.respond_to?(:hostname)).to be_true
|
141
203
|
end
|
142
204
|
end
|
143
205
|
|
144
|
-
describe '#
|
145
|
-
it '
|
146
|
-
expect(@parser.
|
206
|
+
describe '#type' do
|
207
|
+
it 'is in a request state' do
|
208
|
+
expect(@parser.type.state).to eq :request
|
147
209
|
end
|
210
|
+
end
|
148
211
|
|
149
|
-
|
150
|
-
|
212
|
+
describe '#state' do
|
213
|
+
it 'is in a headers state' do
|
214
|
+
expect(@parser.state.state).to eq :headers
|
151
215
|
end
|
152
216
|
end
|
153
217
|
|
154
|
-
describe '#
|
155
|
-
it 'returns
|
156
|
-
expect(@parser.
|
218
|
+
describe '#http_version' do
|
219
|
+
it 'returns [1, 1]' do
|
220
|
+
expect(@parser.http_version).to eq [1, 1]
|
157
221
|
end
|
222
|
+
end
|
158
223
|
|
159
|
-
|
160
|
-
|
224
|
+
describe '#request_method' do
|
225
|
+
it 'returns GET' do
|
226
|
+
expect(@parser.request_method).to eq 'GET'
|
161
227
|
end
|
162
228
|
end
|
163
229
|
|
@@ -173,362 +239,472 @@ describe Segregate do
|
|
173
239
|
end
|
174
240
|
end
|
175
241
|
|
176
|
-
describe '#
|
177
|
-
it '
|
178
|
-
expect(@parser.
|
242
|
+
describe '#headers' do
|
243
|
+
it 'is empty' do
|
244
|
+
expect(@parser.headers).to be_empty
|
179
245
|
end
|
180
246
|
end
|
181
247
|
|
182
|
-
describe '#
|
183
|
-
it '
|
184
|
-
expect(@parser.
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'raises an error if uri does not respond to the method' do
|
188
|
-
expect{ @parser.not_present }.to raise_error NoMethodError, /undefined method `not_present'/
|
248
|
+
describe '#body' do
|
249
|
+
it 'is empty' do
|
250
|
+
expect(@parser.body).to be_empty
|
189
251
|
end
|
190
252
|
end
|
191
253
|
|
192
|
-
describe '#
|
193
|
-
it '
|
194
|
-
expect(@parser.
|
195
|
-
expect(@parser.respond_to? :path).to be_true
|
254
|
+
describe '#request?' do
|
255
|
+
it 'returns false' do
|
256
|
+
expect(@parser.request?).to be_true
|
196
257
|
end
|
197
258
|
end
|
198
|
-
end
|
199
|
-
|
200
|
-
context 'a response line has been parsed' do
|
201
|
-
before(:each) do
|
202
|
-
@parser.parse "HTTP/1.1 200 OK\r\n"
|
203
|
-
end
|
204
259
|
|
205
|
-
describe '#
|
206
|
-
it 'returns
|
207
|
-
expect(@parser.
|
260
|
+
describe '#response?' do
|
261
|
+
it 'returns false' do
|
262
|
+
expect(@parser.response?).to be_false
|
208
263
|
end
|
209
264
|
end
|
210
265
|
|
211
|
-
describe '#
|
212
|
-
it 'returns
|
213
|
-
expect(@parser.
|
266
|
+
describe '#headers_complete?' do
|
267
|
+
it 'returns false' do
|
268
|
+
expect(@parser.headers_complete?).to be_false
|
214
269
|
end
|
270
|
+
end
|
215
271
|
|
216
|
-
|
217
|
-
|
272
|
+
describe '#done?' do
|
273
|
+
it 'returns false' do
|
274
|
+
expect(@parser.done?).to be_false
|
218
275
|
end
|
276
|
+
end
|
219
277
|
|
220
|
-
|
221
|
-
|
222
|
-
expect(@parser.
|
223
|
-
expect(@parser.status_line).to eq "HTTP/2.3 200 OK"
|
278
|
+
describe '#request_line' do
|
279
|
+
it 'returns a valid request line' do
|
280
|
+
expect(@parser.request_line).to match Segregate::REQUEST_LINE
|
224
281
|
end
|
282
|
+
end
|
225
283
|
|
226
|
-
|
227
|
-
|
228
|
-
expect(@parser.status_line).to
|
229
|
-
expect(@parser.status_line).to eq "HTTP/1.1 404 OK"
|
284
|
+
describe '#status_line' do
|
285
|
+
it 'returns nil' do
|
286
|
+
expect(@parser.status_line).to be_nil
|
230
287
|
end
|
288
|
+
end
|
231
289
|
|
232
|
-
|
233
|
-
|
234
|
-
expect(@parser.
|
235
|
-
expect(@parser.status_line).to eq "HTTP/1.1 200 NOT_OK"
|
290
|
+
describe '#request_url' do
|
291
|
+
it 'returns /endpoint' do
|
292
|
+
expect(@parser.request_url).to eq '/endpoint'
|
236
293
|
end
|
237
294
|
end
|
238
295
|
|
239
|
-
describe '#
|
240
|
-
it 'returns
|
241
|
-
expect(@parser.
|
296
|
+
describe '#major_http_version' do
|
297
|
+
it 'returns 1' do
|
298
|
+
expect(@parser.major_http_version).to eq 1
|
242
299
|
end
|
243
300
|
end
|
244
301
|
|
245
|
-
describe '#
|
246
|
-
it 'returns
|
247
|
-
expect(@parser.
|
302
|
+
describe '#minor_http_version' do
|
303
|
+
it 'returns 1' do
|
304
|
+
expect(@parser.minor_http_version).to eq 1
|
248
305
|
end
|
249
306
|
end
|
250
307
|
|
251
|
-
|
252
|
-
|
253
|
-
|
308
|
+
context 'headers have been parsed' do
|
309
|
+
before(:each) do
|
310
|
+
@parser.parse_data "host: www.google.com\r\ncontent-length: 10\r\n\r\n"
|
254
311
|
end
|
255
312
|
|
256
|
-
|
257
|
-
|
313
|
+
describe '#state' do
|
314
|
+
it 'is in a body state' do
|
315
|
+
expect(@parser.state.state).to eq :body
|
316
|
+
end
|
258
317
|
end
|
259
|
-
end
|
260
318
|
|
261
|
-
|
262
|
-
|
263
|
-
|
319
|
+
describe '#headers' do
|
320
|
+
it 'has the correct keys' do
|
321
|
+
expect(@parser.headers.keys).to eq ['host', 'content-length']
|
322
|
+
end
|
323
|
+
|
324
|
+
it 'has the correct values' do
|
325
|
+
expect(@parser.headers.values).to eq ['www.google.com', '10']
|
326
|
+
end
|
264
327
|
end
|
265
328
|
|
266
|
-
|
267
|
-
|
329
|
+
describe '#body' do
|
330
|
+
it 'is empty' do
|
331
|
+
expect(@parser.body).to be_empty
|
332
|
+
end
|
268
333
|
end
|
269
|
-
end
|
270
334
|
|
271
|
-
|
272
|
-
|
273
|
-
|
335
|
+
describe '#headers_complete?' do
|
336
|
+
it 'returns true' do
|
337
|
+
expect(@parser.headers_complete?).to be_true
|
338
|
+
end
|
274
339
|
end
|
275
340
|
|
276
|
-
|
277
|
-
|
341
|
+
describe '#done?' do
|
342
|
+
it 'returns false' do
|
343
|
+
expect(@parser.done?).to be_false
|
344
|
+
end
|
278
345
|
end
|
279
|
-
end
|
280
346
|
|
281
|
-
|
282
|
-
|
283
|
-
|
347
|
+
context 'a body has been parsed' do
|
348
|
+
before(:each) do
|
349
|
+
@parser.parse_data "1234567890\r\n"
|
350
|
+
end
|
351
|
+
describe '#state' do
|
352
|
+
it 'is in a done state' do
|
353
|
+
expect(@parser.state.state).to eq :done
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
describe '#body' do
|
358
|
+
it 'has the correct data' do
|
359
|
+
expect(@parser.body).to eq '1234567890'
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
describe '#done?' do
|
364
|
+
it 'returns true' do
|
365
|
+
expect(@parser.done?).to be_true
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
describe '#raw_data' do
|
370
|
+
it 'returns the message in string form' do
|
371
|
+
expect(@parser.raw_data).to eq "GET /endpoint HTTP/1.1\r\nhost: www.google.com\r\ncontent-length: 10\r\n\r\n1234567890\r\n\r\n"
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
describe '#major_http_version=' do
|
376
|
+
it 'updates the major http version' do
|
377
|
+
@parser.major_http_version = 2
|
378
|
+
expect(@parser.request_line).to eq 'GET /endpoint HTTP/2.1'
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
describe '#minor_http_version=' do
|
383
|
+
it 'updates the minor http version' do
|
384
|
+
@parser.minor_http_version = 2
|
385
|
+
expect(@parser.request_line).to eq 'GET /endpoint HTTP/1.2'
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
describe '#request_method=' do
|
390
|
+
it 'updates the request method' do
|
391
|
+
@parser.request_method = 'POST'
|
392
|
+
expect(@parser.request_line).to eq 'POST /endpoint HTTP/1.1'
|
393
|
+
end
|
394
|
+
end
|
395
|
+
|
396
|
+
describe '#path=' do
|
397
|
+
it 'updates the request url' do
|
398
|
+
@parser.path = '/new/endpoint'
|
399
|
+
expect(@parser.request_line).to eq 'GET /new/endpoint HTTP/1.1'
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
describe '#body=' do
|
404
|
+
it 'updates the body' do
|
405
|
+
@parser.body = 'this is the body'
|
406
|
+
expect(@parser.raw_data).to eq "GET /endpoint HTTP/1.1\r\nhost: www.google.com\r\ncontent-length: 16\r\n\r\nthis is the body\r\n\r\n"
|
407
|
+
end
|
408
|
+
end
|
284
409
|
end
|
285
410
|
end
|
286
411
|
|
287
|
-
|
288
|
-
|
289
|
-
|
412
|
+
context 'non body headers have been parsed' do
|
413
|
+
before(:each) do
|
414
|
+
@parser.parse_data "host: www.google.com\r\naccept: *\r\n\r\n"
|
290
415
|
end
|
291
|
-
end
|
292
416
|
|
293
|
-
|
294
|
-
|
295
|
-
|
417
|
+
describe '#state' do
|
418
|
+
it 'is in a done state' do
|
419
|
+
expect(@parser.state.state).to eq :done
|
420
|
+
end
|
296
421
|
end
|
297
422
|
|
298
|
-
|
299
|
-
|
423
|
+
describe '#headers' do
|
424
|
+
it 'has the correct keys' do
|
425
|
+
expect(@parser.headers.keys).to eq ['host', 'accept']
|
426
|
+
end
|
427
|
+
|
428
|
+
it 'has the correct values' do
|
429
|
+
expect(@parser.headers.values).to eq ['www.google.com', '*']
|
430
|
+
end
|
300
431
|
end
|
301
|
-
end
|
302
432
|
|
303
|
-
|
304
|
-
|
305
|
-
|
433
|
+
describe '#body' do
|
434
|
+
it 'is empty' do
|
435
|
+
expect(@parser.body).to be_empty
|
436
|
+
end
|
306
437
|
end
|
307
438
|
|
308
|
-
|
309
|
-
|
439
|
+
describe '#headers_complete?' do
|
440
|
+
it 'returns true' do
|
441
|
+
expect(@parser.headers_complete?).to be_true
|
442
|
+
end
|
310
443
|
end
|
311
|
-
end
|
312
444
|
|
313
|
-
|
314
|
-
|
315
|
-
|
445
|
+
describe '#done?' do
|
446
|
+
it 'returns true' do
|
447
|
+
expect(@parser.done?).to be_true
|
448
|
+
end
|
316
449
|
end
|
317
450
|
end
|
318
451
|
end
|
319
452
|
|
320
|
-
context 'a
|
453
|
+
context 'a status line has been parsed' do
|
321
454
|
before(:each) do
|
322
|
-
@parser.
|
455
|
+
@parser.parse_data "HTTP/1.1 200 OK\r\n"
|
323
456
|
end
|
324
457
|
|
325
|
-
describe '#
|
326
|
-
it 'returns
|
327
|
-
expect(@parser.
|
458
|
+
describe '#uri' do
|
459
|
+
it 'returns nil' do
|
460
|
+
expect(@parser.uri).to be_nil
|
328
461
|
end
|
462
|
+
end
|
329
463
|
|
330
|
-
|
331
|
-
|
332
|
-
expect(@parser.
|
464
|
+
describe '#type' do
|
465
|
+
it 'is in a request state' do
|
466
|
+
expect(@parser.type.state).to eq :response
|
333
467
|
end
|
334
468
|
end
|
335
469
|
|
336
|
-
describe '#
|
337
|
-
it '
|
338
|
-
expect(@parser.
|
470
|
+
describe '#state' do
|
471
|
+
it 'is in a headers state' do
|
472
|
+
expect(@parser.state.state).to eq :headers
|
339
473
|
end
|
340
474
|
end
|
341
|
-
end
|
342
475
|
|
343
|
-
|
344
|
-
|
345
|
-
|
476
|
+
describe '#http_version' do
|
477
|
+
it 'returns [1, 1]' do
|
478
|
+
expect(@parser.http_version).to eq [1, 1]
|
479
|
+
end
|
346
480
|
end
|
347
481
|
|
348
|
-
describe '#
|
349
|
-
it 'returns
|
350
|
-
expect(@parser.
|
482
|
+
describe '#request_method' do
|
483
|
+
it 'returns nil' do
|
484
|
+
expect(@parser.request_method).to be_nil
|
351
485
|
end
|
486
|
+
end
|
352
487
|
|
353
|
-
|
354
|
-
|
355
|
-
expect(@parser.
|
356
|
-
expect(@parser.headers.accept).to eq 'application/json'
|
357
|
-
expect(@parser.headers.host).to eq 'www.google.com'
|
488
|
+
describe '#status_code' do
|
489
|
+
it 'returns 200' do
|
490
|
+
expect(@parser.status_code).to eq 200
|
358
491
|
end
|
492
|
+
end
|
359
493
|
|
360
|
-
|
361
|
-
|
362
|
-
expect(@parser.
|
363
|
-
expect(@parser.headers.host).to eq 'www.yahoo.com'
|
494
|
+
describe '#status_phrase' do
|
495
|
+
it 'returns OK' do
|
496
|
+
expect(@parser.status_phrase).to eq 'OK'
|
364
497
|
end
|
365
498
|
end
|
366
499
|
|
367
|
-
describe '#
|
368
|
-
it '
|
369
|
-
expect(@parser.
|
500
|
+
describe '#headers' do
|
501
|
+
it 'is empty' do
|
502
|
+
expect(@parser.headers).to be_empty
|
370
503
|
end
|
371
504
|
end
|
372
|
-
end
|
373
505
|
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
506
|
+
describe '#body' do
|
507
|
+
it 'is empty' do
|
508
|
+
expect(@parser.body).to be_empty
|
509
|
+
end
|
378
510
|
end
|
379
511
|
|
380
|
-
describe '#
|
381
|
-
it 'returns
|
382
|
-
expect(@parser.
|
512
|
+
describe '#request?' do
|
513
|
+
it 'returns false' do
|
514
|
+
expect(@parser.request?).to be_false
|
383
515
|
end
|
516
|
+
end
|
384
517
|
|
385
|
-
|
386
|
-
|
518
|
+
describe '#response?' do
|
519
|
+
it 'returns false' do
|
520
|
+
expect(@parser.response?).to be_true
|
387
521
|
end
|
388
522
|
end
|
389
523
|
|
390
|
-
describe '#
|
391
|
-
it 'returns
|
392
|
-
expect(@parser.
|
524
|
+
describe '#headers_complete?' do
|
525
|
+
it 'returns false' do
|
526
|
+
expect(@parser.headers_complete?).to be_false
|
393
527
|
end
|
394
528
|
end
|
395
529
|
|
396
|
-
describe '#
|
397
|
-
it '
|
398
|
-
expect(@parser.
|
399
|
-
@parser.body = 'new content'
|
400
|
-
@parser.update_content_length
|
401
|
-
expect(@parser.headers['content-length']).to eq '11'
|
530
|
+
describe '#done?' do
|
531
|
+
it 'returns false' do
|
532
|
+
expect(@parser.done?).to be_false
|
402
533
|
end
|
403
534
|
end
|
404
535
|
|
405
|
-
describe '#
|
406
|
-
it 'returns
|
407
|
-
expect(@parser.
|
536
|
+
describe '#request_line' do
|
537
|
+
it 'returns nil' do
|
538
|
+
expect(@parser.request_line).to be_nil
|
408
539
|
end
|
409
540
|
end
|
410
|
-
end
|
411
541
|
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
542
|
+
describe '#status_line' do
|
543
|
+
it 'returns a valid status line' do
|
544
|
+
expect(@parser.status_line).to match Segregate::STATUS_LINE
|
545
|
+
end
|
416
546
|
end
|
417
547
|
|
418
|
-
describe '#
|
419
|
-
it 'returns
|
420
|
-
expect(@parser.
|
548
|
+
describe '#request_url' do
|
549
|
+
it 'returns nil' do
|
550
|
+
expect(@parser.request_url).to be_nil
|
421
551
|
end
|
552
|
+
end
|
422
553
|
|
423
|
-
|
424
|
-
|
554
|
+
describe '#major_http_version' do
|
555
|
+
it 'returns 1' do
|
556
|
+
expect(@parser.major_http_version).to eq 1
|
425
557
|
end
|
426
558
|
end
|
427
559
|
|
428
|
-
describe '#
|
429
|
-
it 'returns
|
430
|
-
expect(@parser.
|
560
|
+
describe '#minor_http_version' do
|
561
|
+
it 'returns 1' do
|
562
|
+
expect(@parser.minor_http_version).to eq 1
|
431
563
|
end
|
432
564
|
end
|
433
565
|
|
434
|
-
context '
|
566
|
+
context 'headers have been parsed' do
|
435
567
|
before(:each) do
|
436
|
-
@parser.
|
568
|
+
@parser.parse_data "host: www.google.com\r\ntransfer-encoding: chunked\r\n\r\n"
|
437
569
|
end
|
438
570
|
|
439
|
-
describe '#
|
440
|
-
it '
|
441
|
-
expect(@parser.
|
571
|
+
describe '#state' do
|
572
|
+
it 'is in a body state' do
|
573
|
+
expect(@parser.state.state).to eq :body
|
442
574
|
end
|
575
|
+
end
|
443
576
|
|
444
|
-
|
445
|
-
|
577
|
+
describe '#headers' do
|
578
|
+
it 'has the correct keys' do
|
579
|
+
expect(@parser.headers.keys).to eq ['host', 'transfer-encoding']
|
446
580
|
end
|
447
581
|
|
448
|
-
it '
|
449
|
-
@parser.
|
450
|
-
expect(@parser.body).to eq 'This is the third content!This is the second content!'
|
582
|
+
it 'has the correct values' do
|
583
|
+
expect(@parser.headers.values).to eq ['www.google.com', 'chunked']
|
451
584
|
end
|
452
585
|
end
|
453
586
|
|
454
|
-
describe '#
|
455
|
-
it '
|
456
|
-
expect(@parser.
|
587
|
+
describe '#body' do
|
588
|
+
it 'is empty' do
|
589
|
+
expect(@parser.body).to be_empty
|
457
590
|
end
|
458
591
|
end
|
459
592
|
|
460
|
-
describe '#
|
461
|
-
it '
|
462
|
-
expect(@parser.
|
463
|
-
expect(@parser.headers['transfer-encoding']).to eq 'chunked'
|
464
|
-
@parser.body = 'new content'
|
465
|
-
@parser.update_content_length
|
466
|
-
expect(@parser.headers['content-length']).to eq '11'
|
467
|
-
expect(@parser.headers['transfer-encoding']).to be_nil
|
593
|
+
describe '#headers_complete?' do
|
594
|
+
it 'returns true' do
|
595
|
+
expect(@parser.headers_complete?).to be_true
|
468
596
|
end
|
469
597
|
end
|
470
598
|
|
471
|
-
describe '#
|
472
|
-
it 'returns
|
473
|
-
expect(@parser.
|
599
|
+
describe '#done?' do
|
600
|
+
it 'returns false' do
|
601
|
+
expect(@parser.done?).to be_false
|
474
602
|
end
|
475
603
|
end
|
476
|
-
end
|
477
|
-
end
|
478
|
-
end
|
479
604
|
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
605
|
+
context 'a body has been parsed' do
|
606
|
+
before(:each) do
|
607
|
+
@parser.parse_data "9\r\n123456789\r\n0\r\n\r\n"
|
608
|
+
end
|
609
|
+
describe '#state' do
|
610
|
+
it 'is in a done state' do
|
611
|
+
expect(@parser.state.state).to eq :done
|
612
|
+
end
|
613
|
+
end
|
485
614
|
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
end
|
615
|
+
describe '#body' do
|
616
|
+
it 'has the correct data' do
|
617
|
+
expect(@parser.body).to eq '123456789'
|
618
|
+
end
|
619
|
+
end
|
492
620
|
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
end
|
621
|
+
describe '#done?' do
|
622
|
+
it 'returns true' do
|
623
|
+
expect(@parser.done?).to be_true
|
624
|
+
end
|
625
|
+
end
|
499
626
|
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
end
|
627
|
+
describe '#raw_data' do
|
628
|
+
it 'returns the message in string form' do
|
629
|
+
expect(@parser.raw_data).to eq "HTTP/1.1 200 OK\r\nhost: www.google.com\r\ncontent-length: 9\r\n\r\n123456789\r\n\r\n"
|
630
|
+
end
|
631
|
+
end
|
506
632
|
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
633
|
+
describe '#major_http_version=' do
|
634
|
+
it 'updates the major http version' do
|
635
|
+
@parser.major_http_version = 2
|
636
|
+
expect(@parser.status_line).to eq 'HTTP/2.1 200 OK'
|
637
|
+
end
|
638
|
+
end
|
513
639
|
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
640
|
+
describe '#minor_http_version=' do
|
641
|
+
it 'updates the minor http version' do
|
642
|
+
@parser.minor_http_version = 2
|
643
|
+
expect(@parser.status_line).to eq 'HTTP/1.2 200 OK'
|
644
|
+
end
|
645
|
+
end
|
646
|
+
|
647
|
+
describe '#status_code=' do
|
648
|
+
it 'updates the status code' do
|
649
|
+
@parser.status_code = 404
|
650
|
+
expect(@parser.status_line).to eq 'HTTP/1.1 404 OK'
|
651
|
+
end
|
652
|
+
end
|
653
|
+
|
654
|
+
describe '#status_phrase=' do
|
655
|
+
it 'updates the status phrase' do
|
656
|
+
@parser.status_phrase = 'not found'
|
657
|
+
expect(@parser.status_line).to eq 'HTTP/1.1 200 not found'
|
658
|
+
end
|
659
|
+
end
|
521
660
|
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
661
|
+
describe '#body=' do
|
662
|
+
it 'updates the body' do
|
663
|
+
@parser.body = 'this is the body'
|
664
|
+
expect(@parser.raw_data).to eq "HTTP/1.1 200 OK\r\nhost: www.google.com\r\ncontent-length: 16\r\n\r\nthis is the body\r\n\r\n"
|
665
|
+
end
|
666
|
+
end
|
667
|
+
end
|
527
668
|
end
|
528
669
|
|
529
|
-
|
530
|
-
|
531
|
-
|
670
|
+
context 'non body headers have been parsed' do
|
671
|
+
before(:each) do
|
672
|
+
@parser.parse_data "host: www.google.com\r\naccept: *\r\n\r\n"
|
673
|
+
end
|
674
|
+
|
675
|
+
describe '#state' do
|
676
|
+
it 'is in a done state' do
|
677
|
+
expect(@parser.state.state).to eq :done
|
678
|
+
end
|
679
|
+
end
|
680
|
+
|
681
|
+
describe '#headers' do
|
682
|
+
it 'has the correct keys' do
|
683
|
+
expect(@parser.headers.keys).to eq ['host', 'accept']
|
684
|
+
end
|
685
|
+
|
686
|
+
it 'has the correct values' do
|
687
|
+
expect(@parser.headers.values).to eq ['www.google.com', '*']
|
688
|
+
end
|
689
|
+
end
|
690
|
+
|
691
|
+
describe '#body' do
|
692
|
+
it 'is empty' do
|
693
|
+
expect(@parser.body).to be_empty
|
694
|
+
end
|
695
|
+
end
|
696
|
+
|
697
|
+
describe '#headers_complete?' do
|
698
|
+
it 'returns true' do
|
699
|
+
expect(@parser.headers_complete?).to be_true
|
700
|
+
end
|
701
|
+
end
|
702
|
+
|
703
|
+
describe '#done?' do
|
704
|
+
it 'returns true' do
|
705
|
+
expect(@parser.done?).to be_true
|
706
|
+
end
|
707
|
+
end
|
532
708
|
end
|
533
709
|
end
|
534
710
|
end
|