segregate 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/segregate/version.rb +1 -1
- data/lib/segregate.rb +13 -9
- data/spec/segregate_spec.rb +24 -27
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e0d78f2e3626e434f62af51a9bd25dc6f2f7608
|
4
|
+
data.tar.gz: cb81f3d82418ea3d07cebf692418a901ca305969
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2586ce6b36f3f33d41923bea19d07b7227b1de6c5fa00e9283e7b5514285ad896457ff36bd57a8d81c590fce74cea1fd0f458e02dcaf0638abf0e47d1661df6e
|
7
|
+
data.tar.gz: dc606c644b5d5221c870412a5afee531a1882782d56298a273fb1c8aa5f48124359a818e5a35f5b5707dcf6a97a478d887d5825e31f80c03081411d431108b35
|
data/lib/segregate/version.rb
CHANGED
data/lib/segregate.rb
CHANGED
@@ -81,10 +81,10 @@ class Segregate
|
|
81
81
|
|
82
82
|
def update_content_length
|
83
83
|
if @body_complete
|
84
|
-
@headers['content-length'] = @body.
|
84
|
+
@headers['content-length'] = @body.size.to_s
|
85
85
|
@header_orders.push 'content-length' unless @header_orders.include? 'content-length'
|
86
|
-
@headers.delete '
|
87
|
-
@header_orders.delete '
|
86
|
+
@headers.delete 'transfer-encoding'
|
87
|
+
@header_orders.delete 'transfer-encoding'
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
@@ -97,7 +97,7 @@ class Segregate
|
|
97
97
|
raw_message << "%s: %s\r\n" % [header, headers[header]]
|
98
98
|
end
|
99
99
|
|
100
|
-
raw_message << "\r\n" + @body + "\r\n"
|
100
|
+
raw_message << "\r\n" + @body + "\r\n" if @body_complete
|
101
101
|
raw_message << "\r\n"
|
102
102
|
end
|
103
103
|
|
@@ -108,13 +108,13 @@ class Segregate
|
|
108
108
|
read_headers data unless @headers_complete
|
109
109
|
read_body data unless data.eof?
|
110
110
|
|
111
|
-
@callback.on_message_complete self if @callback.respond_to?(:on_message_complete) &&
|
111
|
+
@callback.on_message_complete self if @callback.respond_to?(:on_message_complete) && message_complete?
|
112
112
|
end
|
113
113
|
|
114
114
|
private
|
115
115
|
|
116
|
-
def
|
117
|
-
(@headers['content-length'].nil? && @headers['
|
116
|
+
def message_complete?
|
117
|
+
@body_complete || (@headers_complete && @headers['content-length'].nil? && @headers['transfer-encoding'].nil?)
|
118
118
|
end
|
119
119
|
|
120
120
|
def read data, size = nil
|
@@ -133,6 +133,10 @@ class Segregate
|
|
133
133
|
parse_request_line line
|
134
134
|
elsif line =~ STATUS_LINE
|
135
135
|
parse_status_line line
|
136
|
+
elsif line =~ UNKNOWN_REQUEST_LINE
|
137
|
+
raise "ERROR: Unknown http method: %s" % line[/^\S+/]
|
138
|
+
else
|
139
|
+
raise "ERROR: Unknown first line: %s" % line
|
136
140
|
end
|
137
141
|
|
138
142
|
@first_line_complete = true
|
@@ -174,7 +178,7 @@ class Segregate
|
|
174
178
|
def read_body data
|
175
179
|
if headers.key? 'content-length'
|
176
180
|
parse_body data
|
177
|
-
elsif headers['
|
181
|
+
elsif headers['transfer-encoding'] == 'chunked'
|
178
182
|
parse_chunked_data data
|
179
183
|
end
|
180
184
|
end
|
@@ -187,7 +191,7 @@ class Segregate
|
|
187
191
|
|
188
192
|
def parse_chunked_data data
|
189
193
|
while !data.eof? && !@body_complete
|
190
|
-
chunk_size = read(data).to_i
|
194
|
+
chunk_size = read(data).to_i(16)
|
191
195
|
if chunk_size == 0
|
192
196
|
@body_complete = true
|
193
197
|
else
|
data/spec/segregate_spec.rb
CHANGED
@@ -38,6 +38,14 @@ describe Segregate do
|
|
38
38
|
it 'accepts one argument' do
|
39
39
|
expect(@parser).to respond_to(:parse).with(1).argument
|
40
40
|
end
|
41
|
+
|
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'
|
44
|
+
end
|
45
|
+
|
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'
|
48
|
+
end
|
41
49
|
end
|
42
50
|
|
43
51
|
context 'a request line has been parsed' do
|
@@ -175,6 +183,10 @@ describe Segregate do
|
|
175
183
|
it 'returns the uri methods' do
|
176
184
|
expect(@parser.path).to eq '/endpoint'
|
177
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'/
|
189
|
+
end
|
178
190
|
end
|
179
191
|
|
180
192
|
describe '#respond_to?' do
|
@@ -307,8 +319,7 @@ describe Segregate do
|
|
307
319
|
|
308
320
|
context 'a header has been parsed' do
|
309
321
|
before(:each) do
|
310
|
-
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
311
|
-
@parser.parse "Accept: application/json\r\n"
|
322
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\nAccept: application/json\r\n"
|
312
323
|
end
|
313
324
|
|
314
325
|
describe '#headers' do
|
@@ -331,10 +342,7 @@ describe Segregate do
|
|
331
342
|
|
332
343
|
context 'all headers have been parsed' do
|
333
344
|
before(:each) do
|
334
|
-
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
335
|
-
@parser.parse "Accept: application/json\r\n"
|
336
|
-
@parser.parse "Host: www.google.com\r\n"
|
337
|
-
@parser.parse "\r\n"
|
345
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\nAccept: application/json\r\nHost: www.google.com\r\n\r\n"
|
338
346
|
end
|
339
347
|
|
340
348
|
describe '#headers' do
|
@@ -365,10 +373,7 @@ describe Segregate do
|
|
365
373
|
|
366
374
|
context 'a body has been parsed' do
|
367
375
|
before(:each) do
|
368
|
-
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
369
|
-
@parser.parse "Host: www.google.com\r\n"
|
370
|
-
@parser.parse "Content-Length: 20\r\n"
|
371
|
-
@parser.parse "\r\n"
|
376
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\nHost: www.google.com\r\nContent-Length: 20\r\n\r\n"
|
372
377
|
@parser.parse "This is the content!\r\n\r\n"
|
373
378
|
end
|
374
379
|
|
@@ -406,11 +411,8 @@ describe Segregate do
|
|
406
411
|
|
407
412
|
context 'a partial chunked body has been parsed' do
|
408
413
|
before(:each) do
|
409
|
-
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
410
|
-
@parser.parse "
|
411
|
-
@parser.parse "Content-Encoding: chunked\r\n"
|
412
|
-
@parser.parse "\r\n"
|
413
|
-
@parser.parse "26\r\nThis is the first content!\r\n"
|
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"
|
414
416
|
end
|
415
417
|
|
416
418
|
describe '#body' do
|
@@ -431,8 +433,7 @@ describe Segregate do
|
|
431
433
|
|
432
434
|
context 'the body parsing is completed' do
|
433
435
|
before(:each) do
|
434
|
-
@parser.parse "
|
435
|
-
@parser.parse "0\r\n\r\n"
|
436
|
+
@parser.parse "1b\r\nThis is the second content!\r\n0\r\n\r\n"
|
436
437
|
end
|
437
438
|
|
438
439
|
describe '#body' do
|
@@ -459,11 +460,11 @@ describe Segregate do
|
|
459
460
|
describe '#update_content_length' do
|
460
461
|
it 'updates the content lenght header' do
|
461
462
|
expect(@parser.headers['content-length']).to be_nil
|
462
|
-
expect(@parser.headers['
|
463
|
+
expect(@parser.headers['transfer-encoding']).to eq 'chunked'
|
463
464
|
@parser.body = 'new content'
|
464
465
|
@parser.update_content_length
|
465
466
|
expect(@parser.headers['content-length']).to eq '11'
|
466
|
-
expect(@parser.headers['
|
467
|
+
expect(@parser.headers['transfer-encoding']).to be_nil
|
467
468
|
end
|
468
469
|
end
|
469
470
|
|
@@ -506,16 +507,14 @@ describe Segregate do
|
|
506
507
|
describe 'on_headers_complete' do
|
507
508
|
it 'calls the callback object' do
|
508
509
|
@callback_object.should_receive(:on_headers_complete).with(@parser)
|
509
|
-
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
510
|
-
@parser.parse "Host: www.google.com\r\n\r\n"
|
510
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\nHost: www.google.com\r\n\r\n"
|
511
511
|
end
|
512
512
|
end
|
513
513
|
|
514
514
|
describe 'on_body' do
|
515
515
|
it 'calls the callback object' do
|
516
516
|
@callback_object.should_receive(:on_body).with("TestData")
|
517
|
-
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
518
|
-
@parser.parse "Content-Length: 8\r\n\r\n"
|
517
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\nContent-Length: 8\r\n\r\n"
|
519
518
|
@parser.parse "TestData\r\n\r\n"
|
520
519
|
end
|
521
520
|
end
|
@@ -523,15 +522,13 @@ describe Segregate do
|
|
523
522
|
describe 'on_message_complete' do
|
524
523
|
it 'calls the callback object with a body' do
|
525
524
|
@callback_object.should_receive(:on_message_complete).with(@parser)
|
526
|
-
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
527
|
-
@parser.parse "Content-Length: 8\r\n\r\n"
|
525
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\nContent-Length: 8\r\n\r\n"
|
528
526
|
@parser.parse "TestData\r\n\r\n"
|
529
527
|
end
|
530
528
|
|
531
529
|
it 'calls the callback object without a body' do
|
532
530
|
@callback_object.should_receive(:on_message_complete).with(@parser)
|
533
|
-
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
534
|
-
@parser.parse "host: www.google.com\r\n\r\n"
|
531
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\nhost: www.google.com\r\n\r\n"
|
535
532
|
end
|
536
533
|
end
|
537
534
|
end
|