/
http11_spec.rb
executable file
·121 lines (98 loc) · 3.93 KB
/
http11_spec.rb
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
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'async/http/protocol/http11'
require_relative 'shared_examples'
RSpec.describe Async::HTTP::Protocol::HTTP11, timeout: 2 do
it_behaves_like Async::HTTP::Protocol
context '#read_request' do
let(:stream) {Async::IO::Stream.new(io)}
subject {described_class.new(stream)}
describe "simple request" do
let(:request) {"GET / HTTP/1.1\r\nHost: localhost\r\nAccept: */*\r\nHeader-0: value 1\r\n"}
let(:io) {StringIO.new(request)}
it "reads request" do
authority, method, url, version, headers, body = subject.read_request
expect(authority).to be == 'localhost'
expect(method).to be == 'GET'
expect(url).to be == '/'
expect(version).to be == 'HTTP/1.1'
expect(headers).to be == {'accept' => ['*/*'], "header-0" => ["value 1"]}
expect(body).to be nil
end
end
describe "simple request with fixed body" do
let(:request) {"GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 11\r\n\r\nHello World"}
let(:io) {StringIO.new(request)}
it "reads request" do
authority, method, url, version, headers, body = subject.read_request
expect(authority).to be == 'localhost'
expect(method).to be == 'GET'
expect(url).to be == '/'
expect(version).to be == 'HTTP/1.1'
expect(headers).to be == {'content-length' => "11"}
expect(body.read).to be == "Hello World"
end
end
describe "simple request with chunked body" do
let(:request) {"GET / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n"}
let(:io) {StringIO.new(request)}
it "reads request" do
authority, method, url, version, headers, body = subject.read_request
expect(authority).to be == 'localhost'
expect(method).to be == 'GET'
expect(url).to be == '/'
expect(version).to be == 'HTTP/1.1'
expect(headers).to be == {'transfer-encoding' => ['chunked']}
expect(body.read).to be == "Hello World"
end
end
end
context 'raw response' do
include_context Async::HTTP::Server
let(:server) do
Async::HTTP::Server.for(endpoint, protocol) do |request|
peer = request.hijack
peer.write(
"#{request.version} 200 It worked!\r\n" +
"connection: close\r\n" +
"\r\n" +
"Hello World!"
)
peer.close
nil
end
end
it "reads raw response" do
response = client.get("/")
expect(response.read).to be == "Hello World!"
end
end
context 'bad requests' do
include_context Async::HTTP::Server
it "should fail with negative content length" do
response = client.post("/", {'content-length' => '-1'})
expect(response).to be_bad_request
end
it "should fail with both transfer encoding and content length" do
response = client.post("/", {'transfer-encoding' => 'chunked', 'content-length' => '0'})
expect(response).to be_bad_request
end
end
end