-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightsourceserver_tests.rb
650 lines (511 loc) · 24.3 KB
/
lightsourceserver_tests.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
require 'rubygems'
require 'spec'
require 'lightsourceserver_http_handler'
require 'rack'
##################
# what's left
##################
#
# gem
# template("mytemplate").using_vars("var1" => data1, "var2" => data2)
# filters
#
def request_for(path, host = nil, method = 'GET')
request_string = "#{method} #{path} HTTP/1.1"
request_string = "#{request_string}\nHost: #{host}\nCookie: hello=world" if host != nil
HTTPRequest.new(request_string)
end
def html_response_for(html)
"HTTP/1.1 200 OK\nContent-Type: text/html\n\n#{html}\n"
end
describe HTTPHandlerBuilder do
it 'should be able to remove trailing and leading / from /test/' do
builder = HTTPHandlerBuilder.new
builder.trim("/test/").should be_eql('test')
end
it 'should be able to add a handler for / that serves <html><body>hello world</body></html>' do
handlers = []
builder = HTTPHandlerBuilder.new(handlers)
http_handler = builder.get "/" do |http|
http.response << "<html><body>hello world</body></html>"
end
handlers.length.should be_eql(1)
http_request = request_for("/")
http_handler.handles?(http_request).should be_true
http_handler.request(http_request).class.should be_eql(RackHTTPResponseAdapter)
http_handler.request(http_request).to_s.should be_eql(html_response_for("<html><body>hello world</body></html>"))
end
it 'should be able to yield a new builder by adding a url part' do
builder = (HTTPHandlerBuilder.new([]) + "test")
builder.to_s.should be_eql("/test")
end
it "should return a handler for /notes/new that serves <html><body>A new note</body></html>" do
builder = HTTPHandlerBuilder.new([])
http_handlers = builder.path "/notes" do
get "/new" do |http|
http.response << "<html><body>A new note</body></html>"
end
end
http_handlers.length.should be_eql(1)
http_handler = http_handlers[0]
http_request = HTTPRequest.new("GET /notes/new HTTP/1.1")
http_handler.handles?(http_request).should be_true
http_handler.request(http_request).to_s.should be_eql(html_response_for("<html><body>A new note</body></html>"))
end
it 'should return a handler set that responds to /test when requested for a specific domain with <html><body>test</body></html>' do
builder = HTTPHandlerBuilder.new([])
http_handlers = builder.hosts "test.com", "www.test.com" do
get "/test" do |http|
http.response << "test"
end
end
http_handlers.length.should be_eql(1)
http_handler = http_handlers[0]
http_request = HTTPRequest.new("GET /test HTTP/1.1\nHost: test.com\n\n")
http_handler.handles?(http_request).should be_true
http_handler.request(http_request).to_s.should be_eql(html_response_for("test"))
http_request = HTTPRequest.new("GET /test HTTP/1.1\nHost: www.test.com\n\n")
http_handler.handles?(http_request).should be_true
http_handler.request(http_request).to_s.should be_eql(html_response_for("test"))
end
it 'should return a handler that recognizes that it cannot handle a request for test123.com if it has been configured for notrecognized.com' do
builder = HTTPHandlerBuilder.new([])
http_handlers = builder.hosts "test.com" do
get "/test" do |http|
http.response << "test"
end
end
http_handlers.length.should be_eql(1)
http_handler = http_handlers[0]
http_request = HTTPRequest.new("GET /test HTTP/1.1\nHost: notrecognized.com\n\n")
http_handler.handles?(http_request).should be_false
end
it 'should return a handler that services a request for /test at hosts test1.com and test2.com' do
builder = HTTPHandlerBuilder.new([])
http_handlers = builder.hosts ["test1.com", "test2.com"] do
get "/test" do |http|
http.response << "test"
end
end
http_handlers.length.should be_eql(1)
http_handler = http_handlers[0]
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/test", "HTTP_HOST" => "test1.com", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_handler.handles?(http_request).should be_true
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/test", "HTTP_HOST" => "test2.com", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_handler.handles?(http_request).should be_true
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/test", "HTTP_HOST" => "test3.com", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_handler.handles?(http_request).should be_false
end
it 'should be able to filter out a url' do
builder = HTTPHandlerBuilder.new([])
http_handlers = builder.hosts ["test1.com"] do
filter "/test" do |http|
http.response << "Not allowed"
dont_allow_request
end
end
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/test", "HTTP_HOST" => "test1.com", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_handlers[0].request(http_request).to_s.should be_eql(html_response_for("Not allowed"))
end
it 'should allow a url through a filter by default' do
builder = HTTPHandlerBuilder.new([])
http_handlers = builder.hosts ["test1.com"] do
filter "/filtertest" do |http|
http.response << "allowed"
end
get "/filtertest" do |http|
http.response << "came to get handler"
end
end
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/filtertest", "HTTP_HOST" => "test1.com", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_handlers[0].request(http_request).to_s.should be_eql(html_response_for("allowed"))
end
end
describe HTTPHandler do
it 'should serve <html><body>hello world</body></html> when requested for / with the verb GET' do
handler_proc = Proc.new do |http|
http.response << "<html><body>hello world</body></html>"
end
http_handler = HTTPHandler.new({"uri" => "/", "verb" => "GET"}, handler_proc)
request = HTTPRequest.new("GET / HTTP/1.1")
http_handler.handles?(request).should be_true
http_handler.request(request).to_s.should be_eql(html_response_for("<html><body>hello world</body></html>"))
end
it 'should serve <html><body>hello world</body></html> when requested for / with host test.com:2000 and with the verb GET, when configured with host test.com' do
handler_proc = Proc.new do |http|
http.response << "<html><body>hello world</body></html>"
end
http_handler = HTTPHandler.new({"hosts" => ['test.com'], "uri" => "/", "verb" => "GET"}, handler_proc)
request = HTTPRequest.new("GET / HTTP/1.1\nHost: test.com:2000")
http_handler.handles?(request).should be_true
http_handler.request(request).to_s.should be_eql(html_response_for("<html><body>hello world</body></html>"))
end
it 'should recognize that it serves /hello/test if configured for /hello with a handler taking parameters http and name' do
handler_proc = Proc.new do |http, name|
http.response << "#{name}"
end
http_handler = HTTPHandler.new({"uri" => "/hello", "verb" => "GET"}, handler_proc)
request = HTTPRequest.new("GET /hello/test HTTP/1.1")
http_handler.handles?(request).should be_true
end
it 'should recognize that it serves /hello/test/world if configured for /hello/test with a handler taking parameters http and name, and should not serve for /hello/world' do
handler_proc = Proc.new do |http, *params|
http.response << "#{name}"
end
http_handler = HTTPHandler.new({"uri" => "/hello/test", "verb" => "GET"}, handler_proc)
request = HTTPRequest.new("GET /hello/test/world HTTP/1.1")
http_handler.handles?(request).should be_true
request = HTTPRequest.new("GET /hello/world HTTP/1.1")
http_handler.handles?(request).should be_false
end
it 'should recognize that it serves /hello/test if configured for /hello/test with a handler taking parameters http and name' do
handler_proc = Proc.new do |http, name|
http.response << "#{name}"
end
http_handler = HTTPHandler.new({"uri" => "/hello/test", "verb" => "GET"}, handler_proc)
request = HTTPRequest.new("GET /hello/test HTTP/1.1")
http_handler.handles?(request).should be_true
end
it 'should serve <html><body>test</body></html> when configured for url /hello accessed with url /hello/test' do
html_creator = Proc.new {|param| "<html><body>#{param}</body></html>" }
handler_proc = Proc.new do |http, name|
http.response << html_creator.call(name)
end
http_handler = HTTPHandler.new({"uri" => "/hello", "verb" => "GET"}, handler_proc)
request = HTTPRequest.new("GET /hello/test HTTP/1.1")
http_handler.handles?(request).should be_true
http_handler.request(request).to_s.should be_eql(html_response_for(html_creator.call("test")))
end
it 'should recognize that it does NOT serve GET /test when configured for GET and POST /test and requested for POST /test' do
handler_proc = Proc.new do |http, name|
http.response << "test"
end
http_handler = HTTPHandler.new({"uri" => "/test", "verb" => "GET"}, handler_proc)
request = HTTPRequest.new("POST /hello/test HTTP/1.1")
http_handler.handles?(request).should be_false
end
it 'should use a response object if one is passed to it' do
handler_proc = Proc.new do |http, name|
http.response << "test"
end
http_handler = HTTPHandler.new({"uri" => "/test", "verb" => "GET"}, handler_proc)
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/test", "HTTP_HOST" => "localhost:2000", "HTTP_VERSION" => "HTTP/1.1")
rack_http_request_adapter = RackAdapterHTTPRequest.new(rack_request)
rack_http_response_adapter = RackHTTPResponseAdapter.new("1.1", 200)
rack_http_response_adapter << "some stuff here "
http_handler.request(rack_http_request_adapter, rack_http_response_adapter).to_s.should be_eql(html_response_for('some stuff here test'))
end
end
describe "http configuration" do
def start_http_server(router, ip = "0.0.0.0", port = 80)
@started_with_ip = ip
@started_with_port = port
@started_with_router = router
end
it 'should start the http server and serve <html><body>new note</body></html> when queried for /notes/new' do
http do
path '/notes' do
get '/new' do |http|
http.response << "<html><body>new note</body></html>"
end
end
end
@started_with_ip.should be_eql("127.0.0.1")
@started_with_port.should be_eql(80)
(@started_with_router != nil).should be_true
router = @started_with_router
router.request(request_for("/notes/new")).to_s.should be_eql(html_response_for("<html><body>new note</body></html>"))
end
it 'should start the http server and serve <html><body>new note</body></html> when queried for /notes/new, and <html><body>new page</body></html> when queried for /pages/new' do
http do
path '/notes' do
get '/new' do |http|
http.response << "<html><body>new note</body></html>"
end
end
get '/pages/new' do |http|
http.response << "<html><body>new page</body></html>"
end
end
@started_with_ip.should be_eql("127.0.0.1")
@started_with_port.should be_eql(80)
(@started_with_router != nil).should be_true
router = @started_with_router
router.request(request_for("/notes/new")).to_s.should be_eql(html_response_for("<html><body>new note</body></html>"))
router.request(request_for("/pages/new")).to_s.should be_eql(html_response_for("<html><body>new page</body></html>"))
end
it 'should start the http server and serve <html><body>new note</body></html> when queried for /notes/new' do
http "ip" => "127.0.0.1", "port" => 8080 do
path '/notes' do
get '/new' do |http|
http.response << "<html><body>new note</body></html>"
end
end
end
@started_with_ip.should be_eql("127.0.0.1")
@started_with_port.should be_eql(8080)
(@started_with_router != nil).should be_true
router = @started_with_router
router.request(request_for("/notes/new")).to_s.should be_eql(html_response_for("<html><body>new note</body></html>"))
end
it 'should start an http server and raise a ResourceNotFoundError when queried with with /test for host abc.com, where it is configured with host test.com' do
http "ip" => "127.0.0.1", "port" => 8080 do
hosts "test.com" do
get '/notes/new' do |http|
http.response << "<html><body>new note</body></html>"
end
end
end
@started_with_ip.should be_eql("127.0.0.1")
@started_with_port.should be_eql(8080)
(@started_with_router != nil).should be_true
router = @started_with_router
lambda { router.request(request_for("/notes/new", 'abc.com')) }.should raise_error(ResourceNotFoundError)
end
it 'should start an http server and raise a ResourceNotFoundError when queried for / for host 127.0.0.1:8080 given a configured domain localhost' do
http "ip" => "127.0.0.1", "port" => 8080 do
hosts 'localhost' do
get "/" do |http|
http.response << "<html><body><b>Hello world</b></body></html>"
end
get "/test" do |http|
http.response << "<html><body><b>test</b> without hello world</body></html>"
end
path "/test" do
get "/hello" do |http|
http.response << "<html><body><h1>Hello</h1> world</body></html>"
end
get "/world" do |http|
http.response << "<html><body>Hello <h1>world</h1></body></html>"
end
end
end
end
@started_with_ip.should be_eql("127.0.0.1")
@started_with_port.should be_eql(8080)
(@started_with_router != nil).should be_true
router = @started_with_router
lambda { router.request(request_for("/test/world", '127.0.0.1:8080')) }.should raise_error(ResourceNotFoundError)
end
end
describe HTTPRequestRouter do
it 'should serves <html><body>new note</body></html> for /notes/new and <html><body>new page</body></html> for /pages/new' do
builder = HTTPHandlerBuilder.new
handlers = builder.path '/' do
get '/notes/new' do |http|
http.response << "<html><body>new note</body></html>"
end
get '/pages/new' do |http|
http.response << "<html><body>new page</body></html>"
end
end
router = HTTPRequestRouter.new(handlers)
router.request(request_for("/notes/new")).to_s.should be_eql(html_response_for("<html><body>new note</body></html>"))
router.request(request_for("/pages/new")).to_s.should be_eql(html_response_for("<html><body>new page</body></html>"))
end
it 'should raise a 404 if a resource which is not recognized is requested' do
builder = HTTPHandlerBuilder.new
handlers = builder.path '/' do
get '/notes/new' do
"<html><body>new note</body></html>"
end
end
router = HTTPRequestRouter.new(handlers)
lambda { router.request(request_for("/")) }.should raise_error(ResourceNotFoundError)
end
it 'should support the following verbs GET, POST, PUT, DELETE, HEAD, OPTIONS, TRACE' do
builder = HTTPHandlerBuilder.new
handlers = builder.path '/' do
get '/get' do |http|
http.response << "get"
end
post '/post' do |http|
http.response << "post"
end
put '/put' do |http|
http.response << "put"
end
delete '/delete' do |http|
http.response << "delete"
end
head '/head' do |http|
http.response << "head"
end
options '/options' do |http|
http.response << "options"
end
trace '/trace' do |http|
http.response << "trace"
end
end
router = HTTPRequestRouter.new(handlers)
request_test = Proc.new do |verb|
rack_request = Rack::Request.new("REQUEST_METHOD" => verb, "PATH_INFO" => "/index.html", "HTTP_HOST" => "test.com", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
router.request(request_for("/#{verb.downcase}", "test.com", verb)).to_s.should be_eql(html_response_for(verb.downcase))
end
["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE"].each {|verb| request_test.call(verb) }
end
it 'should refrain from evaluating the succeeding handlers if a handler returns nil' do
builder = HTTPHandlerBuilder.new
handlers = builder.path '/' do
post 'hello' do |http|
http.response << "hello"
end
get '/test' do |http|
http.response << "test"
end
end
router = HTTPRequestRouter.new(handlers)
rack_request = Rack::Request.new("REQUEST_METHOD" => "POST", "PATH_INFO" => "/hello", "HTTP_HOST" => "test.com", "HTTP_VERSION" => "HTTP/1.1", "Content-Type" => "application/x-www-form-urlencoded", 'rack.input' => StringIO.new('hello=world&color=white'))
http_request = RackAdapterHTTPRequest.new(rack_request)
router.request(http_request).to_s.should be_eql(html_response_for('hello'))
end
it 'should route filtered requests correctly' do
builder = HTTPHandlerBuilder.new
handlers = builder.path '/' do
filter 'filteredhello' do |http|
http.response << "hello "
end
get 'filteredhello' do |http|
http.response << "test"
end
end
router = HTTPRequestRouter.new(handlers)
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/filteredhello", "HTTP_HOST" => "test.com", "HTTP_VERSION" => "HTTP/1.1", "Content-Type" => "application/x-www-form-urlencoded", 'rack.input' => StringIO.new('hello=world&color=white'))
http_request = RackAdapterHTTPRequest.new(rack_request)
router.request(http_request).to_s.should be_eql(html_response_for('hello test'))
end
end
describe HTTPRequest do
it 'should be able to get the verb GET, http version 1.1, and host test.com from the http packet GET /index.html HTTP/1.1\nHost: test.com\n\nHello world' do
http_request = HTTPRequest.new("GET /index.html HTTP/1.1\nHost: test.com\n\nHello world")
http_request.verb.should be_eql("GET")
http_request.resource.should be_eql("/index.html")
http_request.http_version.should be_eql("1.1")
http_request.headers["Host"].should be_eql("test.com")
http_request.payload.should be_eql("Hello world")
end
it 'should have a host header of localhost:2000, given a request having host localhost:2000' do
http_request = HTTPRequest.new("GET /test.html HTTP/1.1\nHost: localhost:2000\n\ntest")
http_request.headers["Host"].should be_eql("localhost:2000")
end
end
describe HTTPResponse do
it 'should be able to marshal an HTTP response with response code 200, http version 1.1, headers Content-Type: text/html; charset=UTF-8, server LightSourceServer server in ruby, and body <html><body><h1>It works</h1></body></html>n' do
http_response = HTTPResponse.new("1.1", 200)
http_response.set_headers "Content-Type" => "text/html; charset=UTF-8", "Server" => "LightSourceServer server in ruby"
http_response.body = "<html><body><h1>It works</h1></body></html>"
response_string = http_response.to_s
header, body = *(response_string.split("\n\n"))
header.index("HTTP/1.1 200 OK").should_not be_eql(nil)
header.index("Content-Type: text/html; charset=UTF-8").should_not be_eql(nil)
header.index("Server: LightSourceServer server in ruby").should_not be_eql(nil)
body.should be_eql("<html><body><h1>It works</h1></body></html>\n")
end
it "should have 'hello world' in it's body after response << 'hello world'" do
http_response = HTTPResponse.new("1.1", 200)
http_response << "hello world"
http_response.to_s.should be_eql("HTTP/1.1 200 OK\n\nhello world\n")
end
it 'should have settable http vesion and response' do
http_response = HTTPResponse.new("1.1", 200)
http_response.http_version = "1.0"
http_response.status_code = 400
http_response.to_s.should be_eql("HTTP/1.0 400 Bad Request\n\n\n")
end
it 'should be redirectable' do
http_response = HTTPResponse.new("1.1", 200)
http_response.redirect("http://www.stuff.com")
http_response.to_s.should be_eql("HTTP/1.1 302 Moved Temporarily\nLocation: http://www.stuff.com\n\n\n")
end
it 'should expose the headers of the response' do
http_response = HTTPResponse.new("1.1", 200)
http_response.headers.class.should be_eql(Hash)
end
end
describe HTTPIO do
it 'should always contain a request and a response object for the current http request' do
lambda { HTTPIO.new(nil, HTTPResponse.new("1.1", 200)) }.should raise_error(NullParameter)
lambda { HTTPIO.new(HTTPRequest.new("GET / HTTP/1.1\n\n"), nil) }.should raise_error(NullParameter)
end
end
describe RackAdapterHTTPRequest do
it 'should be able to get the verb GET, http version 1.1, host test.com, a payload and GET vars hello=world and color=white from a rack http request' do
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/index.html", "HTTP_HOST" => "test.com", "HTTP_VERSION" => "HTTP/1.1", "QUERY_STRING" => "hello=world&color=white", 'rack.input' => StringIO.new('Hello world'))
http_request = RackAdapterHTTPRequest.new(rack_request)
http_request.verb.should be_eql("GET")
http_request.resource.should be_eql("/index.html")
http_request.http_version.should be_eql("1.1")
http_request.headers["Host"].should be_eql("test.com")
http_request.payload.should be_eql("Hello world")
http_request.GET['hello'].should be_eql('world')
http_request.GET['color'].should be_eql('white')
end
it 'should be able to get the verb POST, http version 1.1, host test.com and FORM vars hello=world and color=white from a rack http request' do
rack_request = Rack::Request.new("REQUEST_METHOD" => "POST", "PATH_INFO" => "/index.html", "HTTP_HOST" => "test.com", "HTTP_VERSION" => "HTTP/1.1", "Content-Type" => "application/x-www-form-urlencoded", 'rack.input' => StringIO.new('hello=world&color=white'))
http_request = RackAdapterHTTPRequest.new(rack_request)
http_request.verb.should be_eql("POST")
http_request.resource.should be_eql("/index.html")
http_request.http_version.should be_eql("1.1")
http_request.headers["Host"].should be_eql("test.com")
http_request.FORM['hello'].should be_eql('world')
http_request.FORM['color'].should be_eql('white')
end
it 'should have a host header of localhost:2000, given a request having host localhost:2000' do
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/index.html", "HTTP_HOST" => "localhost:2000", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_request.headers["Host"].should be_eql("localhost:2000")
end
it 'should expose the original rack request property' do
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/index.html", "HTTP_HOST" => "localhost:2000", "HTTP_VERSION" => "HTTP/1.1")
rack_request = Rack::Request.new("REQUEST_METHOD" => "GET", "PATH_INFO" => "/index.html", "HTTP_HOST" => "localhost:2000", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_request.rack_request.class.should be_eql(Rack::Request)
end
it 'should support cookies' do
rack_request = Rack::Request.new("HTTP_COOKIE" => "hello=world", "REQUEST_METHOD" => "GET", "PATH_INFO" => "/index.html", "HTTP_HOST" => "localhost:2000", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_request.cookies['hello'].should be_eql('world')
end
it 'should be able to share context between handlers' do
rack_request = Rack::Request.new("HTTP_COOKIE" => "hello=world", "REQUEST_METHOD" => "GET", "PATH_INFO" => "/index.html", "HTTP_HOST" => "localhost:2000", "HTTP_VERSION" => "HTTP/1.1")
http_request = RackAdapterHTTPRequest.new(rack_request)
http_request.context[:hello] = "world"
http_request.context[:hello].should be_eql("world")
end
end
describe RackHTTPHeadersAdapter do
it 'should be able to read the Accept-Encoding header from a hash containing a key HTTP_ACCEPT_ENCODING' do
rack_headers_adapter = RackHTTPHeadersAdapter.new("HTTP_ACCEPT_ENCODING" => "test encoding")
rack_headers_adapter["Accept-Encoding"].should be_eql("test encoding")
end
end
describe RackHTTPResponseAdapter do
it 'should inherit from HTTPResponse' do
rack_http_response_adapter = RackHTTPResponseAdapter.new("1.1", 200)
rack_http_response_adapter.class.ancestors.include?(HTTPResponse).should be_true
end
it 'should return a rack response object with the parameters provided' do
http_response = RackHTTPResponseAdapter.new("1.1", 200)
http_response.set_headers "Content-Type" => "text/html; charset=UTF-8", "Server" => "LightSourceServer server in ruby"
http_response.body = "<html><body><h1>It works</h1></body></html>"
rack_response = http_response.rack_response
rack_response["Content-Type"].should be_eql("text/html; charset=UTF-8")
rack_response["Server"].should be_eql("LightSourceServer server in ruby")
rack_response.ok?.should be_true
got_called = false
rack_response.each {|item| got_called = true; item.should be_eql("<html><body><h1>It works</h1></body></html>") }
got_called.should be_true
end
it 'should return a rack response object which is not an OK response' do
http_response = RackHTTPResponseAdapter.new("1.1", 302)
rack_response = http_response.rack_response
rack_response.ok?.should be_false
end
end