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.
@@ -6,26 +6,8 @@ describe Segregate do
6
6
  end
7
7
 
8
8
  describe '::new' do
9
- it 'returns an instance of Segregate' do
10
- expect(Segregate.new).to be_an_instance_of Segregate
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 '#parse' do
38
- it 'accepts one argument' do
39
- expect(@parser).to respond_to(:parse).with(1).argument
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 'errors if an incorrect first line is received' do
43
- expect{ @parser.parse "fail\r\n" }.to raise_error RuntimeError, 'ERROR: Unknown first line: fail'
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 'errors if an incorrect http method is received' do
47
- expect{ @parser.parse "FAIL /endpoint HTTP/1.1\r\n" }.to raise_error RuntimeError, 'ERROR: Unknown http method: FAIL'
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
- context 'a request line has been parsed' do
52
- before(:each) do
53
- @parser.parse "GET /endpoint HTTP/1.1\r\n"
65
+ describe '#uri' do
66
+ it 'returns nil' do
67
+ expect(@parser.uri).to be_nil
54
68
  end
69
+ end
55
70
 
56
- describe '#request_line' do
57
- it 'returns a string' do
58
- expect(@parser.request_line).to be_an_instance_of String
59
- end
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
- it 'returns a request line' do
62
- expect(@parser.request_line).to match Segregate::REQUEST_LINE
63
- end
76
+ it 'is in a nil state' do
77
+ expect(@parser.type.state).to be_nil
78
+ end
79
+ end
64
80
 
65
- it 'returns a modified method request line' do
66
- @parser.request_method = 'POST'
67
- expect(@parser.request_line).to match Segregate::REQUEST_LINE
68
- expect(@parser.request_line).to eq "POST /endpoint HTTP/1.1"
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
- it 'returns a modified path request line' do
72
- @parser.path = "/new/endpoint"
73
- expect(@parser.request_line).to match Segregate::REQUEST_LINE
74
- expect(@parser.request_line).to eq "GET /new/endpoint HTTP/1.1"
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
- it 'returns a modified http version request line' do
78
- @parser.http_version = [2,3]
79
- expect(@parser.request_line).to match Segregate::REQUEST_LINE
80
- expect(@parser.request_line).to eq "GET /endpoint HTTP/2.3"
81
- end
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
- it 'returns a modified major http version request line' do
84
- @parser.major_http_version = 2
85
- expect(@parser.request_line).to match Segregate::REQUEST_LINE
86
- expect(@parser.request_line).to eq "GET /endpoint HTTP/2.1"
87
- end
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
- it 'returns a modified minor http version request line' do
90
- @parser.minor_http_version = 2
91
- expect(@parser.request_line).to match Segregate::REQUEST_LINE
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
- describe '#status_line' do
97
- it 'returns nil' do
98
- expect(@parser.status_line).to be_nil
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
- describe '#request?' do
103
- it 'returns true' do
104
- expect(@parser.request?).to be_an_instance_of TrueClass
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
- describe '#response?' do
109
- it 'returns false' do
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
- describe '#http_version' do
115
- it 'returns an array' do
116
- expect(@parser.http_version).to be_an_instance_of Array
117
- end
125
+ describe '#body' do
126
+ it 'is empty' do
127
+ expect(@parser.body).to be_empty
128
+ end
129
+ end
118
130
 
119
- it 'returns [1, 1]' do
120
- expect(@parser.http_version).to eql [1,1]
121
- end
131
+ describe '#request?' do
132
+ it 'returns false' do
133
+ expect(@parser.request?).to be_false
122
134
  end
135
+ end
123
136
 
124
- describe '#major_http_version' do
125
- it 'returns an integer' do
126
- expect(@parser.major_http_version).to be_an_instance_of Fixnum
127
- end
137
+ describe '#response?' do
138
+ it 'returns false' do
139
+ expect(@parser.response?).to be_false
140
+ end
141
+ end
128
142
 
129
- it 'returns 1' do
130
- expect(@parser.major_http_version).to eql 1
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 '#minor_http_version' do
135
- it 'returns an integer' do
136
- expect(@parser.minor_http_version).to be_an_instance_of Fixnum
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 'returns 1' do
140
- expect(@parser.minor_http_version).to eql 1
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 '#request_method' do
145
- it 'returns an string' do
146
- expect(@parser.request_method).to be_an_instance_of String
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
- it 'returns GET' do
150
- expect(@parser.request_method).to eq 'GET'
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 '#request_url' do
155
- it 'returns an string' do
156
- expect(@parser.request_url).to be_an_instance_of String
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
- it 'returns /endpoint' do
160
- expect(@parser.request_url).to eq '/endpoint'
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 '#uri' do
177
- it 'returns a URI' do
178
- expect(@parser.uri).to be_an_instance_of URI::Generic
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 '#method_missing' do
183
- it 'returns the uri methods' do
184
- expect(@parser.path).to eq '/endpoint'
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 '#respond_to?' do
193
- it 'responds to the uri path' do
194
- expect(@parser.respond_to? :uri).to be_true
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 '#request_line' do
206
- it 'returns nil' do
207
- expect(@parser.request_line).to be_nil
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 '#status_line' do
212
- it 'returns a string' do
213
- expect(@parser.status_line).to be_an_instance_of String
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
- it 'returns a status line' do
217
- expect(@parser.status_line).to match Segregate::STATUS_LINE
272
+ describe '#done?' do
273
+ it 'returns false' do
274
+ expect(@parser.done?).to be_false
218
275
  end
276
+ end
219
277
 
220
- it 'returns a modified http version status line' do
221
- @parser.http_version = [2,3]
222
- expect(@parser.status_line).to match Segregate::STATUS_LINE
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
- it 'returns a modified status code status line' do
227
- @parser.status_code = 404
228
- expect(@parser.status_line).to match Segregate::STATUS_LINE
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
- it 'returns a modified status phrase status line' do
233
- @parser.status_phrase = 'NOT_OK'
234
- expect(@parser.status_line).to match Segregate::STATUS_LINE
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 '#request?' do
240
- it 'returns false' do
241
- expect(@parser.request?).to be_an_instance_of FalseClass
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 '#response?' do
246
- it 'returns true' do
247
- expect(@parser.response?).to be_an_instance_of TrueClass
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
- describe '#http_version' do
252
- it 'returns an array' do
253
- expect(@parser.http_version).to be_an_instance_of Array
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
- it 'returns [1, 1]' do
257
- expect(@parser.http_version).to eql [1,1]
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
- describe '#major_http_version' do
262
- it 'returns an integer' do
263
- expect(@parser.major_http_version).to be_an_instance_of Fixnum
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
- it 'returns 1' do
267
- expect(@parser.major_http_version).to eql 1
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
- describe '#minor_http_version' do
272
- it 'returns an integer' do
273
- expect(@parser.minor_http_version).to be_an_instance_of Fixnum
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
- it 'returns 1' do
277
- expect(@parser.minor_http_version).to eql 1
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
- describe '#request_method' do
282
- it 'returns nil' do
283
- expect(@parser.request_method).to be_nil
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
- describe '#request_url' do
288
- it 'returns nil' do
289
- expect(@parser.request_url).to be_nil
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
- describe '#status_code' do
294
- it 'returns an integer' do
295
- expect(@parser.status_code).to be_an_instance_of Fixnum
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
- it 'returns 200' do
299
- expect(@parser.status_code).to eql 200
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
- describe '#status_phrase' do
304
- it 'returns an string' do
305
- expect(@parser.status_phrase).to be_an_instance_of String
433
+ describe '#body' do
434
+ it 'is empty' do
435
+ expect(@parser.body).to be_empty
436
+ end
306
437
  end
307
438
 
308
- it 'returns OK' do
309
- expect(@parser.status_phrase).to eq 'OK'
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
- describe '#uri' do
314
- it 'returns nil' do
315
- expect(@parser.uri).to be_nil
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 header has been parsed' do
453
+ context 'a status line has been parsed' do
321
454
  before(:each) do
322
- @parser.parse "GET /endpoint HTTP/1.1\r\nAccept: application/json\r\n"
455
+ @parser.parse_data "HTTP/1.1 200 OK\r\n"
323
456
  end
324
457
 
325
- describe '#headers' do
326
- it 'returns an instans of a hashie mash' do
327
- expect(@parser.headers).to be_an_instance_of Hashie::Mash
458
+ describe '#uri' do
459
+ it 'returns nil' do
460
+ expect(@parser.uri).to be_nil
328
461
  end
462
+ end
329
463
 
330
- it 'contains the parsed header' do
331
- expect(@parser.headers).to respond_to(:accept)
332
- expect(@parser.headers.accept).to eq 'application/json'
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 '#headers_complete?' do
337
- it 'returns false' do
338
- expect(@parser.headers_complete?).to be_an_instance_of FalseClass
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
- context 'all headers have been parsed' do
344
- before(:each) do
345
- @parser.parse "GET /endpoint HTTP/1.1\r\nAccept: application/json\r\nHost: www.google.com\r\n\r\n"
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 '#headers' do
349
- it 'returns an instans of a hashie mash' do
350
- expect(@parser.headers).to be_an_instance_of Hashie::Mash
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
- it 'contains all the parsed headers' do
354
- expect(@parser.headers).to respond_to(:accept)
355
- expect(@parser.headers).to respond_to(:host)
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
- it 'contains modified headers' do
361
- @parser.headers.host = 'www.yahoo.com'
362
- expect(@parser.headers).to respond_to(:host)
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 '#headers_complete?' do
368
- it 'returns true' do
369
- expect(@parser.headers_complete?).to be_an_instance_of TrueClass
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
- context 'a body has been parsed' do
375
- before(:each) do
376
- @parser.parse "GET /endpoint HTTP/1.1\r\nHost: www.google.com\r\nContent-Length: 20\r\n\r\n"
377
- @parser.parse "This is the content!\r\n\r\n"
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 '#body' do
381
- it 'returns a string' do
382
- expect(@parser.body).to be_an_instance_of String
512
+ describe '#request?' do
513
+ it 'returns false' do
514
+ expect(@parser.request?).to be_false
383
515
  end
516
+ end
384
517
 
385
- it 'contains the body text' do
386
- expect(@parser.body).to eq 'This is the content!'
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 '#body_complete?' do
391
- it 'returns true' do
392
- expect(@parser.body_complete?).to be_an_instance_of TrueClass
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 '#update_content_length' do
397
- it 'updates the content lenght header' do
398
- expect(@parser.headers['content-length']).to eq '20'
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 '#raw_data' do
406
- it 'returns the raw message' do
407
- expect(@parser.raw_data).to eq "GET /endpoint HTTP/1.1\r\nhost: www.google.com\r\ncontent-length: 20\r\n\r\nThis is the content!\r\n\r\n"
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
- context 'a partial chunked body has been parsed' do
413
- before(:each) do
414
- @parser.parse "GET /endpoint HTTP/1.1\r\nHost: www.google.com\r\nTransfer-Encoding: chunked\r\n\r\n"
415
- @parser.parse "1a\r\nThis is the first content!\r\n"
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 '#body' do
419
- it 'returns a string' do
420
- expect(@parser.body).to be_an_instance_of String
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
- it 'contains the body text' do
424
- expect(@parser.body).to eq 'This is the first content!'
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 '#body_complete?' do
429
- it 'returns false' do
430
- expect(@parser.body_complete?).to be_an_instance_of FalseClass
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 'the body parsing is completed' do
566
+ context 'headers have been parsed' do
435
567
  before(:each) do
436
- @parser.parse "1b\r\nThis is the second content!\r\n0\r\n\r\n"
568
+ @parser.parse_data "host: www.google.com\r\ntransfer-encoding: chunked\r\n\r\n"
437
569
  end
438
570
 
439
- describe '#body' do
440
- it 'returns a string' do
441
- expect(@parser.body).to be_an_instance_of String
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
- it 'contains the body text' do
445
- expect(@parser.body).to eq 'This is the first content!This is the second content!'
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 'contains the modified body' do
449
- @parser.body.sub!('first', 'third')
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 '#body_complete?' do
455
- it 'returns true' do
456
- expect(@parser.body_complete?).to be_an_instance_of TrueClass
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 '#update_content_length' do
461
- it 'updates the content lenght header' do
462
- expect(@parser.headers['content-length']).to be_nil
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 '#raw_data' do
472
- it 'returns the raw message' do
473
- expect(@parser.raw_data).to eq "GET /endpoint HTTP/1.1\r\nhost: www.google.com\r\ncontent-length: 53\r\n\r\nThis is the first content!This is the second content!\r\n\r\n"
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
- context 'a new parser has been created with a callback object' do
481
- before(:each) do
482
- @callback_object = double
483
- @parser = Segregate.new @callback_object
484
- end
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
- describe 'on_message_begin' do
487
- it 'calls the callback object' do
488
- @callback_object.should_receive(:on_message_begin).with(@parser)
489
- @parser.parse "GET /endpoint HTTP/1.1\r\n"
490
- end
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
- describe 'on_request' do
494
- it 'calls the callback object' do
495
- @callback_object.should_receive(:on_request_line).with(@parser)
496
- @parser.parse "GET /endpoint HTTP/1.1\r\n"
497
- end
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
- describe 'on_response' do
501
- it 'calls the callback object' do
502
- @callback_object.should_receive(:on_status_line).with(@parser)
503
- @parser.parse "HTTP/1.1 200 OK\r\n"
504
- end
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
- describe 'on_headers_complete' do
508
- it 'calls the callback object' do
509
- @callback_object.should_receive(:on_headers_complete).with(@parser)
510
- @parser.parse "GET /endpoint HTTP/1.1\r\nHost: www.google.com\r\n\r\n"
511
- end
512
- end
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
- describe 'on_body' do
515
- it 'calls the callback object' do
516
- @callback_object.should_receive(:on_body).with("TestData")
517
- @parser.parse "GET /endpoint HTTP/1.1\r\nContent-Length: 8\r\n\r\n"
518
- @parser.parse "TestData\r\n\r\n"
519
- end
520
- end
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
- describe 'on_message_complete' do
523
- it 'calls the callback object with a body' do
524
- @callback_object.should_receive(:on_message_complete).with(@parser)
525
- @parser.parse "GET /endpoint HTTP/1.1\r\nContent-Length: 8\r\n\r\n"
526
- @parser.parse "TestData\r\n\r\n"
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
- it 'calls the callback object without a body' do
530
- @callback_object.should_receive(:on_message_complete).with(@parser)
531
- @parser.parse "GET /endpoint HTTP/1.1\r\nhost: www.google.com\r\n\r\n"
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