-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathclient_spec.cr
236 lines (202 loc) · 7.22 KB
/
client_spec.cr
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
require "spec"
require "openssl"
require "http/client"
require "http/server"
private def test_server(host, port, read_time = 0, content_type = "text/plain")
server = TCPServer.new(host, port)
begin
spawn do
io = server.accept
sleep read_time
response = HTTP::Client::Response.new(200, headers: HTTP::Headers{"Content-Type" => content_type}, body: "OK")
response.to_io(io)
io.flush
end
yield server
ensure
server.close
end
end
private class TestClient < HTTP::Client
def set_defaults(request)
super
end
end
module HTTP
describe Client do
typeof(Client.new("host"))
typeof(Client.new("host", port: 8080))
typeof(Client.new("host", tls: true))
typeof(Client.new(URI.new))
typeof(Client.new(URI.parse("http://www.example.com")))
{% for method in %w(get post put head delete patch options) %}
typeof(Client.{{method.id}} "url")
typeof(Client.new("host").{{method.id}}("uri"))
typeof(Client.new("host").{{method.id}}("uri", headers: Headers {"Content-Type" => "text/plain"}))
typeof(Client.new("host").{{method.id}}("uri", body: "body"))
{% end %}
typeof(Client.post "url", form: {"a" => "b"})
typeof(Client.post("url", form: {"a" => "b"}) { })
typeof(Client.put "url", form: {"a" => "b"})
typeof(Client.put("url", form: {"a" => "b"}) { })
typeof(Client.new("host").basic_auth("username", "password"))
typeof(Client.new("host").before_request { |req| })
typeof(Client.new("host").close)
typeof(Client.new("host").compress = true)
typeof(Client.new("host").compress?)
typeof(Client.get(URI.parse("http://www.example.com")))
typeof(Client.get(URI.parse("http://www.example.com")))
typeof(Client.get("http://www.example.com"))
typeof(Client.post("http://www.example.com", body: IO::Memory.new))
typeof(Client.new("host").post("/", body: IO::Memory.new))
typeof(Client.post("http://www.example.com", body: Bytes[65]))
typeof(Client.new("host").post("/", body: Bytes[65]))
describe "from URI" do
it "has sane defaults" do
cl = Client.new(URI.parse("http://example.com"))
cl.tls?.should be_nil
cl.port.should eq(80)
end
{% if !flag?(:without_openssl) %}
it "detects HTTPS" do
cl = Client.new(URI.parse("https://example.com"))
cl.tls?.should be_truthy
cl.port.should eq(443)
end
it "keeps context" do
ctx = OpenSSL::SSL::Context::Client.new
cl = Client.new(URI.parse("https://example.com"), ctx)
cl.tls.should be(ctx)
end
it "doesn't take context for HTTP" do
ctx = OpenSSL::SSL::Context::Client.new
expect_raises(ArgumentError, "TLS context given") do
Client.new(URI.parse("http://example.com"), ctx)
end
end
it "allows for specified ports" do
cl = Client.new(URI.parse("https://example.com:9999"))
cl.tls?.should be_truthy
cl.port.should eq(9999)
end
{% else %}
it "raises when trying to activate TLS" do
expect_raises(Exception, "TLS is disabled") do
Client.new "example.org", 443, tls: true
end
end
{% end %}
it "raises error if not http schema" do
expect_raises(ArgumentError, "Unsupported scheme: ssh") do
Client.new(URI.parse("ssh://example.com"))
end
end
it "raises error if URI is missing host" do
expect_raises(ArgumentError, "must have host") do
Client.new(URI.parse("http:/"))
end
end
it "yields to a block" do
Client.new(URI.parse("http://example.com")) do |client|
typeof(client)
end
end
end
context "from a host" do
it "yields to a block" do
Client.new("example.com") do |client|
typeof(client)
end
end
end
it "sends the host header ipv6 with brackets" do
server = HTTP::Server.new do |context|
context.response.print context.request.headers["Host"]
end
address = server.bind_unused_port "::1"
spawn { server.listen }
HTTP::Client.get("http://[::1]:#{address.port}/").body.should eq("[::1]:#{address.port}")
server.close
end
it "sends a 'connection: close' header on one-shot request" do
server = HTTP::Server.new do |context|
context.response.print context.request.headers["connection"]
end
address = server.bind_unused_port "::1"
spawn { server.listen }
HTTP::Client.get("http://[::1]:#{address.port}/").body.should eq("close")
server.close
end
it "sends a 'connection: close' header on one-shot request with block" do
server = HTTP::Server.new do |context|
context.response.print context.request.headers["connection"]
end
address = server.bind_unused_port "::1"
spawn { server.listen }
HTTP::Client.get("http://[::1]:#{address.port}/") do |response|
response.body_io.gets_to_end
end.should eq("close")
server.close
end
it "doesn't read the body if request was HEAD" do
resp_get = test_server("localhost", 0, 0) do |server|
client = Client.new("localhost", server.local_address.port)
break client.get("/")
end
test_server("localhost", 0, 0) do |server|
client = Client.new("localhost", server.local_address.port)
resp_head = client.head("/")
resp_head.headers.should eq(resp_get.headers)
resp_head.body.should eq("")
end
end
it "raises if URI is missing scheme" do
expect_raises(ArgumentError, "Missing scheme") do
HTTP::Client.get URI.parse("www.example.com")
end
end
it "raises if URI is missing host" do
expect_raises(ArgumentError, "must have host") do
HTTP::Client.get URI.parse("http://")
end
end
it "tests read_timeout" do
test_server("localhost", 0, 0) do |server|
client = Client.new("localhost", server.local_address.port)
client.read_timeout = 1.second
client.get("/")
end
test_server("localhost", 0, 0.5) do |server|
client = Client.new("localhost", server.local_address.port)
expect_raises(IO::Timeout, "Read timed out") do
client.read_timeout = 0.001
client.get("/?sleep=1")
end
end
end
it "tests connect_timeout" do
test_server("localhost", 0, 0) do |server|
client = Client.new("localhost", server.local_address.port)
client.connect_timeout = 0.5
client.get("/")
end
end
it "tests empty Content-Type" do
test_server("localhost", 0, content_type: "") do |server|
client = Client.new("localhost", server.local_address.port)
client.get("/")
end
end
describe "#set_defaults" do
it "sets default Host header" do
client = TestClient.new "www.example.com"
request = HTTP::Request.new("GET", "/")
client.set_defaults(request)
request.host.should eq "www.example.com"
request = HTTP::Request.new("GET", "/", HTTP::Headers{"Host" => "other.example.com"})
client.set_defaults(request)
request.host.should eq "other.example.com"
end
end
end
end