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 309 310 311 312 313
|
require 'http-parser'
describe HttpParser::Parser, "#initialize" do
before :each do
@inst = HttpParser::Parser.new_instance
end
describe "callbacks" do
describe "on_message_begin" do
subject do
described_class.new do |parser|
parser.on_message_begin { @begun = true }
end
end
it "should trigger on a new request" do
subject.parse @inst, "GET / HTTP/1.1"
expect(@begun).to eq(true)
end
end
describe "on_url" do
let(:expected) { '/foo?q=1' }
subject do
described_class.new do |parser|
parser.on_url { |inst, data| @url = data }
end
end
it "should pass the recognized url" do
subject.parse @inst, "GET "
expect(@url).to be_nil
subject.parse @inst, "#{expected} HTTP/1.1"
expect(@url).to eq(expected)
end
end
describe "on_header_field" do
let(:expected) { 'Host' }
subject do
described_class.new do |parser|
parser.on_header_field { |inst, data| @header_field = data }
end
end
it "should pass the recognized header-name" do
subject.parse @inst, "GET /foo HTTP/1.1\r\n"
expect(@header_field).to be_nil
subject.parse @inst, "#{expected}: example.com\r\n"
expect(@header_field).to eq(expected)
end
end
describe "on_header_value" do
let(:expected) { 'example.com' }
subject do
described_class.new do |parser|
parser.on_header_value { |inst, data| @header_value = data }
end
end
it "should pass the recognized header-value" do
subject.parse @inst, "GET /foo HTTP/1.1\r\n"
expect(@header_value).to be_nil
subject.parse @inst, "Host: #{expected}\r\n"
expect(@header_value).to eq(expected)
end
end
describe "on_headers_complete" do
subject do
described_class.new do |parser|
parser.on_headers_complete { @header_complete = true }
end
end
it "should trigger on the last header" do
subject.parse @inst, "GET / HTTP/1.1\r\nHost: example.com\r\n"
expect(@header_complete).to be_nil
subject.parse @inst, "\r\n"
expect(@header_complete).to eq(true)
end
context "when #stop! is called" do
subject do
described_class.new do |parser|
parser.on_headers_complete { @inst.stop! }
parser.on_body { |inst, data| @body = data }
end
end
it "should indicate there is no request body to parse" do
subject.parse @inst, "GET / HTTP/1.1\r\nHost: example.com\r\n\r\nBody"
expect(@body).to be_nil
end
end
end
describe "on_body" do
let(:expected) { "Body" }
subject do
described_class.new do |parser|
parser.on_body { |inst, data| @body = data }
end
end
it "should trigger on the body" do
subject.parse @inst, "POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n"
expect(@body).to be_nil
subject.parse @inst, "#{"%x" % expected.length}\r\n#{expected}"
expect(@body).to eq(expected)
end
end
describe "on_message_complete" do
subject do
described_class.new do |parser|
parser.on_message_complete { @message_complete = true }
end
end
it "should trigger at the end of the message" do
subject.parse @inst, "GET / HTTP/1.1\r\n"
expect(@message_complete).to be_nil
subject.parse @inst, "Host: example.com\r\n\r\n"
expect(@message_complete).to eq(true)
end
end
end
describe "#http_method" do
let(:expected) { :POST }
it "should set the http_method field" do
subject.parse @inst, "#{expected} / HTTP/1.1\r\n"
expect(@inst.http_method).to eq(expected)
end
end
describe "#http_major" do
let(:expected) { 1 }
before do
@inst.type = :request
end
context "when parsing requests" do
it "should set the http_major field" do
subject.parse @inst, "GET / HTTP/#{expected}."
expect(@inst.http_major).to eq(expected)
end
end
context "when parsing responses" do
before do
@inst.type = :response
end
it "should set the http_major field" do
subject.parse @inst, "HTTP/#{expected}."
expect(@inst.http_major).to eq(expected)
end
end
end
describe "#http_minor" do
let(:expected) { 2 }
context "when parsing requests" do
it "should set the http_minor field" do
subject.parse @inst, "GET / HTTP/1.#{expected}\r\n"
expect(@inst.http_minor).to eq(expected)
end
end
context "when parsing responses" do
before do
@inst.type = :response
end
it "should set the http_major field" do
subject.parse @inst, "HTTP/1.#{expected} "
expect(@inst.http_minor).to eq(expected)
end
end
end
describe "#http_version" do
let(:expected) { '1.1' }
before do
subject.parse @inst, "GET / HTTP/#{expected}\r\n"
end
it "should combine #http_major and #http_minor" do
expect(@inst.http_version).to eq(expected)
end
end
describe "#http_status" do
context "when parsing requests" do
before do
subject.parse @inst, "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
end
it "should not be set" do
expect(@inst.http_status).to be_zero
end
end
context "when parsing responses" do
let(:expected) { 200 }
before do
@inst.type = :response
subject.parse @inst, "HTTP/1.1 #{expected} OK\r\n"
subject.parse @inst, "Location: http://example.com/\r\n\r\n"
end
it "should set the http_status field" do
expect(@inst.http_status).to eq(expected)
end
end
end
describe "#upgrade?" do
let(:upgrade) { 'WebSocket' }
before do
subject.parse @inst, "GET /demo HTTP/1.1\r\n"
subject.parse @inst, "Upgrade: #{upgrade}\r\n"
subject.parse @inst, "Connection: Upgrade\r\n"
subject.parse @inst, "Host: example.com\r\n"
subject.parse @inst, "Origin: http://example.com\r\n"
subject.parse @inst, "WebSocket-Protocol: sample\r\n"
subject.parse @inst, "\r\n"
end
xit "should return true if the Upgrade header was set" do
expect(@inst.upgrade?).to eq(true)
end
end
describe "pipelined requests" do
subject do
@begun = 0
described_class.new do |parser|
parser.on_message_begin { @begun += 1 }
end
end
it "should trigger on a new request" do
subject.parse @inst, "GET /demo HTTP/1.1\r\n\r\nGET /demo HTTP/1.1\r\n\r\n"
expect(@begun).to eq(2)
end
end
describe "method based instead of block based" do
class SomeParserClass
attr_reader :url
def on_url(inst, data)
@url = data
end
end
let(:expected) { '/foo?q=1' }
it "should simplify the process" do
callbacks = SomeParserClass.new
parser = described_class.new(callbacks)
parser.parse @inst, "GET "
expect(callbacks.url).to be_nil
parser.parse @inst, "#{expected} HTTP/1.1"
expect(callbacks.url).to eq(expected)
end
end
end
|