segregate 0.1.0 → 0.2.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 +12 -1
- data/spec/segregate_spec.rb +45 -0
- 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: 7421d31435304199cb1032dbe326420d13a75a69
|
4
|
+
data.tar.gz: eea29b9926c873d0c66a2bbf6a9106aa847b2e5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d52b0c763d8705420c29d53722c70b74f84549fb5fafc426a4fb303b6057cd39b71b96904e413485027d226cf8d93840e0bde29469a8699efe609684576eb41
|
7
|
+
data.tar.gz: bdc263d1000ecaca777f3a808fb4c3e0ee6cbd3d93d87f3f2a0692611ba4a2e5312d2faf7336b3a7c5ff9ff0a092199602c54085f9573294b9661cc7b0cbac95
|
data/lib/segregate/version.rb
CHANGED
data/lib/segregate.rb
CHANGED
@@ -18,7 +18,9 @@ class Segregate
|
|
18
18
|
@uri.respond_to?(meth, include_private) || super
|
19
19
|
end
|
20
20
|
|
21
|
-
def initialize
|
21
|
+
def initialize callback = nil
|
22
|
+
@callback = callback
|
23
|
+
|
22
24
|
@uri = nil
|
23
25
|
@request_method = nil
|
24
26
|
@status_code = nil
|
@@ -94,6 +96,7 @@ class Segregate
|
|
94
96
|
end
|
95
97
|
|
96
98
|
def read_first_line data
|
99
|
+
@callback.on_message_begin self if @callback.respond_to?(:on_message_begin)
|
97
100
|
line = read data
|
98
101
|
|
99
102
|
if line =~ REQUEST_LINE
|
@@ -114,6 +117,8 @@ class Segregate
|
|
114
117
|
@request_method, url, @http_version[0], @http_version[1] = line.scan(REQUEST_LINE).flatten
|
115
118
|
@http_version.map! {|v| v.to_i}
|
116
119
|
@uri = URI.parse url
|
120
|
+
|
121
|
+
@callback.on_request_line self if @callback.respond_to?(:on_request_line)
|
117
122
|
end
|
118
123
|
|
119
124
|
def parse_status_line line
|
@@ -121,6 +126,8 @@ class Segregate
|
|
121
126
|
@http_version[0], @http_version[1], code, @status_phrase = line.scan(STATUS_LINE).flatten
|
122
127
|
@http_version.map! {|v| v.to_i}
|
123
128
|
@status_code = code.to_i
|
129
|
+
|
130
|
+
@callback.on_status_line self if @callback.respond_to?(:on_status_line)
|
124
131
|
end
|
125
132
|
|
126
133
|
def read_headers data
|
@@ -134,6 +141,8 @@ class Segregate
|
|
134
141
|
@header_order << key.downcase
|
135
142
|
end
|
136
143
|
end
|
144
|
+
|
145
|
+
@callback.on_headers_complete self if @callback.respond_to?(:on_headers_complete) && @headers_complete
|
137
146
|
end
|
138
147
|
|
139
148
|
def read_body data
|
@@ -142,6 +151,8 @@ class Segregate
|
|
142
151
|
elsif headers['content-encoding'] == 'chunked'
|
143
152
|
parse_chunked_data data
|
144
153
|
end
|
154
|
+
|
155
|
+
@callback.on_body_complete self if @callback.respond_to?(:on_body_complete) && @body_complete
|
145
156
|
end
|
146
157
|
|
147
158
|
def parse_body data
|
data/spec/segregate_spec.rb
CHANGED
@@ -396,4 +396,49 @@ describe Segregate do
|
|
396
396
|
end
|
397
397
|
end
|
398
398
|
end
|
399
|
+
|
400
|
+
context 'a new parser has been created with a callback object' do
|
401
|
+
before(:each) do
|
402
|
+
@callback_object = double
|
403
|
+
@parser = Segregate.new @callback_object
|
404
|
+
end
|
405
|
+
|
406
|
+
describe 'on_message_begin' do
|
407
|
+
it 'calls the callback object' do
|
408
|
+
@callback_object.should_receive(:on_message_begin).with(@parser)
|
409
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
describe 'on_request' do
|
414
|
+
it 'calls the callback object' do
|
415
|
+
@callback_object.should_receive(:on_request_line).with(@parser)
|
416
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
describe 'on_response' do
|
421
|
+
it 'calls the callback object' do
|
422
|
+
@callback_object.should_receive(:on_status_line).with(@parser)
|
423
|
+
@parser.parse "HTTP/1.1 200 OK\r\n"
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
describe 'on_headers_complete' do
|
428
|
+
it 'calls the callback object' do
|
429
|
+
@callback_object.should_receive(:on_headers_complete).with(@parser)
|
430
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
431
|
+
@parser.parse "Host: www.google.com\r\n\r\n"
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
describe 'on_body_complete' do
|
436
|
+
it 'calls the callback object' do
|
437
|
+
@callback_object.should_receive(:on_body_complete).with(@parser)
|
438
|
+
@parser.parse "GET /endpoint HTTP/1.1\r\n"
|
439
|
+
@parser.parse "Content-Length: 8\r\n\r\n"
|
440
|
+
@parser.parse "TestData\r\n\r\n"
|
441
|
+
end
|
442
|
+
end
|
443
|
+
end
|
399
444
|
end
|