forked from suitmedia/kirk
/
client_spec.rb
303 lines (237 loc) · 7.45 KB
/
client_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
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
require 'spec_helper'
require 'kirk/client'
import org.eclipse.jetty.util.thread.QueuedThreadPool
describe 'Kirk::Client' do
describe "requests" do
before do
start echo_app_path('config.ru')
end
it "allows to pass block for request" do
handler = Class.new do
def initialize(buffer)
@buffer = buffer
end
def on_response_complete(response)
@buffer << response
end
end
@buffer = []
group = Kirk::Client.group(:host => "localhost:9090") do |g|
body = "foobar"
g.request do |r|
r.method :post
r.url "/foo"
r.handler handler.new(@buffer)
r.headers "Accept" => "text/plain", :Bizz => :bazz
r.body body
end
end
response = parse_response(group.responses.first)
response["PATH_INFO"].should == "/foo"
response["HTTP_ACCEPT"].should == "text/plain"
response["REQUEST_METHOD"].should == "POST"
response["HTTP_BIZZ"].should == "bazz"
response["rack.input"].should == "foobar"
@buffer.should == group.responses
end
it "allows to use simplified syntax" do
group = Kirk::Client.group(:host => "localhost:9090") do |g|
g.get '/'
g.put '/'
g.post '/'
g.delete '/'
end
responses = parse_responses(group.responses)
responses.map {|r| r["REQUEST_METHOD"] }.sort.should == ["DELETE", "GET", "POST", "PUT"]
end
it "performs simple GET" do
group = Kirk::Client.group do |s|
s.request :GET, "http://localhost:9090/"
end
group.should have(1).responses
response = parse_response(group.responses.first)
response["PATH_INFO"].should == "/"
response["REQUEST_METHOD"].should == "GET"
end
it "performs more than one GET" do
group = Kirk::Client.group do |s|
s.request :GET, "http://localhost:9090/foo"
s.request :GET, "http://localhost:9090/bar"
end
group.should have(2).responses
parse_responses(group.responses).map { |r| r["PATH_INFO"] }.sort.should == %w(/bar /foo)
end
it "performs POST request" do
body = "zomg"
group = Kirk::Client.group do |g|
g.request :POST, "http://localhost:9090/", nil, body, {'Accept' => 'text/html'}
end
response = parse_response(group.responses.first)
response["HTTP_ACCEPT"].should == "text/html"
response["REQUEST_METHOD"].should == "POST"
response["rack.input"].should == "zomg"
end
it "allows to pass body as IO" do
body = StringIO.new "zomg"
group = Kirk::Client.group do |g|
g.request :POST, "http://localhost:9090/", nil, body
end
response = parse_response(group.responses.first)
response["rack.input"].should == "zomg"
end
end
it "sets the response status when it is successful" do
start lambda { |env| [ 200, { 'Content-Type' => 'text/plain' }, ['Hello'] ] }
resp = Kirk::Client.get 'http://localhost:9090/'
resp.status.should == 200
end
it "sets the response status when it is 201" do
start lambda { |env| [ 201, { 'Content-Type' => 'text/plain' }, ['Hello'] ] }
resp = Kirk::Client.get 'http://localhost:9090/'
resp.status.should == 201
end
it "sets the response status when it is 302" do
start lambda { |env| [ 302, { 'Content-Type' => 'text/plain' }, ['Hello'] ] }
resp = Kirk::Client.get 'http://localhost:9090/'
resp.status.should == 302
end
it "fetches all the headers" do
headers = { 'Content-Type' => 'text/plain', 'X-FooBar' => "zomg" }
start(lambda { |env| [ 200, headers, [ "Hello" ] ] })
headers.to_a.sort { |a, b| a.first <=> b.first }.should ==
[['Content-Type', 'text/plain'], ['X-FooBar', 'zomg']]
end
it "allows to stream body" do
handler = Class.new do
def initialize(buffer)
@buffer = buffer
end
def on_response_body(resp, content)
@buffer << content
end
end
start(lambda do |env|
[ 200, { 'Content-Type' => 'text/plain' }, [ "a" * 10000 ] ]
end)
@buffer = []
group = Kirk::Client.group do |s|
s.request :GET, "http://localhost:9090/", handler.new(@buffer)
end
sleep(0.05)
group.should have(1).responses
@buffer.length.should be > 1
end
context "callbacks" do
it "handles on_response_complete callback" do
handler = Class.new do
def initialize(buffer)
@buffer = buffer
end
def on_response_complete(response)
@buffer << response
end
end
start_default_app
@buffer = []
group = Kirk::Client.group do |s|
s.request :GET, "http://localhost:9090/", handler.new(@buffer)
end
sleep(0.05)
@buffer.first.should == group.responses.first
end
it "handles on_response_head callback" do
handler = Class.new do
def initialize(buffer)
@buffer = buffer
end
def on_response_head(resp)
@buffer << resp.headers
end
end
start_default_app
@buffer = []
group = Kirk::Client.group do |s|
s.request :GET, "http://localhost:9090/", handler.new(@buffer)
end
sleep(0.05)
@buffer.first.should == {
'Content-Type' => 'text/plain',
'Content-Length' => '5',
'Server' => 'Jetty(7.2.2.v20101205)'
}
end
it "calls complete callback after finishing all the requests" do
start_default_app
@completed = false
group = Kirk::Client.group(:host => "localhost:9090") do |g|
g.get "/"
g.complete do
@completed = true
end
end
group.should have(1).responses
@completed.should be_true
end
end
it "allows to set thread_pool" do
start_default_app
my_thread_pool = Class.new(QueuedThreadPool) do
def initialize(buffer)
@buffer = buffer
super()
end
def dispatch(job)
@buffer << 1
super(job)
end
end
@buffer = []
thread_pool = my_thread_pool.new(@buffer)
client = Kirk::Client.new(:thread_pool => thread_pool)
client.group(:host => "http://localhost:9090") do |g|
g.get "/"
g.get "/foo"
end
@buffer.length.should > 0
client.client.get_thread_pool.should == thread_pool
client.stop
end
it "allows to run group on instance" do
start_default_app
client = Kirk::Client.new
result = client.group do |g|
g.request :GET, "http://localhost:9090/"
end
result.responses.first.body.should == "Hello"
end
it "allows to set host for group" do
start_default_app
group = Kirk::Client.group(:host => "localhost:9090") do |g|
g.request :GET, "/"
end
group.responses.first.body.should == "Hello"
end
it "allows to avoid blocking" do
start(lambda { |env| sleep(0.1); [ 200, {}, 'Hello' ] })
group = Kirk::Client.group(:host => "localhost:9090", :block => false) do |g|
g.get "/"
end
group.should have(0).responses
group.join
group.should have(1).responses
end
it "passes self to group" do
client = Kirk::Client.new
group = client.group {}
group.client.should == client
end
def start_default_app
start(lambda { |env| [ 200, { 'Content-Type' => 'text/plain' }, [ "Hello" ] ] })
end
def parse_response(response)
Marshal.load(response.body)
end
def parse_responses(responses)
responses.map { |r| parse_response(r) }
end
end