-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathapplication_spec.moon
More file actions
910 lines (675 loc) · 25.7 KB
/
application_spec.moon
File metadata and controls
910 lines (675 loc) · 25.7 KB
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
lapis = require "lapis"
import simulate_action, simulate_request, assert_request from require "lapis.spec.request"
mock_app = (...) ->
simulate_action lapis.Application, ...
describe "lapis.application", ->
before_each ->
-- unload any dynamically loaded modules for views & actions
for k,v in pairs package.loaded
if k\match("^actions%.") or k\match("^vies%.")
package.loaded[k] = nil
describe "mutation check", ->
it "prevents adding routes", ->
assert.has_error(
-> lapis.Application\match("hello", ->)
"lapis.Application is not able to be modified with routes. You must either subclass or instantiate it"
)
it "prevents adding features", ->
assert.has_error(
-> lapis.Application\enable "exceptions"
"You tried to enable a feature on the read-only class lapis.Application. You must sub-class it before enabling features"
)
describe "find_action", ->
action1 = ->
action2 = ->
it "finds action", ->
class SomeApp extends lapis.Application
[hello: "/cool-dad"]: action1
[world: "/another-dad"]: action2
assert.same action1, (SomeApp\find_action "hello")
assert.same action2, (SomeApp\find_action "world")
assert.same nil, (SomeApp\find_action "nothing")
it "finds require'd action", ->
package.loaded["actions.hello"] = action1
package.loaded["actions.admin.cool"] = action2
class SomeApp extends lapis.Application
[hello: "/cool-dad"]: true
[world: "/uncool-dad"]: "admin.cool"
assert.same action1, (SomeApp\find_action "hello")
assert.same action2, (SomeApp\find_action "world")
describe "dispatch", ->
describe "lazy loaded actions", ->
import simulate_request from require "lapis.spec.request"
class BaseApp extends lapis.Application
[test_route: "/hello/:var"]: true
[another: "/good-stuff"]: "hello_world"
[regular: "/hmm"]: ->
"/yo": true
before_each ->
package.loaded["actions.test_route"] = spy.new ->
package.loaded["actions.hello_world"] = spy.new ->
it "dispatches action by route name", ->
simulate_request BaseApp, "/hello/5"
assert.spy(package.loaded["actions.test_route"]).was.called!
assert.spy(package.loaded["actions.hello_world"]).was_not.called!
it "dispatches action by string name", ->
simulate_request BaseApp, "/good-stuff"
assert.spy(package.loaded["actions.test_route"]).was_not.called!
assert.spy(package.loaded["actions.hello_world"]).was.called!
it "doesn't call other actions for unrelated route", ->
simulate_request BaseApp, "/hmm"
assert.spy(package.loaded["actions.test_route"]).was_not.called!
assert.spy(package.loaded["actions.hello_world"]).was_not.called!
simulate_request BaseApp, "/hmm"
it "failes to load `true` action with no route name", ->
assert.has_error ->
simulate_request BaseApp, "/yo"
describe "inheritance", ->
local result
before_each ->
result = nil
class BaseApp extends lapis.Application
"/yeah": => result = "base yeah"
[test_route: "/hello/:var"]: => result = "base test"
class ChildApp extends BaseApp
"/yeah": => result = "child yeah"
"/thing": => result = "child thing"
it "finds route in base app", ->
status, buffer, headers = simulate_request ChildApp, "/hello/world", {}
assert.same 200, status
assert.same "base test", result
it "generates url from route in base", ->
url = simulate_action ChildApp, =>
@url_for "test_route", var: "foobar"
assert.same url, "/hello/foobar"
it "overrides route in base class", ->
status, buffer, headers = simulate_request ChildApp, "/yeah", {}
assert.same 200, status
assert.same "child yeah", result
it "overrides route by route name", ->
class Root extends lapis.Application
layout: false
[test_route: "/hello/:var"]: => "original"
class ChildApp extends Root
[test_route: "/zone"]: => "override"
status, res = simulate_request ChildApp, "/zone", {}
assert.same {200, "override"}, {status, res}
-- route does not exist, overwritten
assert.has_error ->
simulate_request ChildApp, "/hello/world", {}
assert.same "/zone", ChildApp!.router\url_for "test_route", var: "whoa"
it "re-used path replaces route name", ->
class Root extends lapis.Application
layout: false
[first: "/hello/:cool"]: => "first"
class ChildApp extends Root
[second: "/hello/:cool"]: => "second"
status, res = simulate_request ChildApp, "/hello/yeah", {}
assert.same {200, "second"}, {status, res}
app = ChildApp!
assert.same "/hello/whoa", app.router\url_for "second", cool: "whoa"
assert.has_error(
-> app.router\url_for "first"
"lapis.router: There is no route named: first"
)
describe "include", ->
local result
before_each ->
result = nil
it "app class includes app class", ->
class SubApp extends lapis.Application
"/hello": => result = "hello"
class App extends lapis.Application
@include SubApp
"/world": => result = "world"
status, buffer, headers = simulate_request App, "/hello", {}
assert.same 200, status
assert.same "hello", result
status, buffer, headers = simulate_request App, "/world", {}
assert.same 200, status
assert.same "world", result
it "app class includes app class with named routes", ->
class SubApp extends lapis.Application
[hello: "/hello"]: => result = "hello"
class App extends lapis.Application
@include SubApp
[world: "/world"]: => result = "world"
app = App!
req = App.Request App!, {}, {}
assert.same "/hello", req\url_for "hello"
assert.same "/world", req\url_for "world"
it "sets sub-app prefix path", ->
class SubApp extends lapis.Application
[hello: "/hello"]: => result = "hello"
class App extends lapis.Application
@include SubApp, path: "/sub"
[world: "/world"]: => result = "world"
app = App!
req = App.Request App!, {}, {}
assert.same "/sub/hello", req\url_for "hello"
assert.same "/world", req\url_for "world"
it "sets sub-app url name prefix", ->
class SubApp extends lapis.Application
[hello: "/hello"]: => result = "hello"
class App extends lapis.Application
@include SubApp, name: "sub_"
[world: "/world"]: => result = "world"
app = App!
req = App.Request App!, {}, {}
assert.has_error -> req\url_for "hello"
assert.same "/hello", req\url_for "sub_hello"
assert.same "/world", req\url_for "world"
it "sets include options from target app", ->
class SubApp extends lapis.Application
@path: "/sub"
@name: "sub_"
[hello: "/hello"]: => result = "hello"
class App extends lapis.Application
@include SubApp
[world: "/world"]: => result = "world"
app = App!
req = App.Request App!, {}, {}
assert.same "/sub/hello", req\url_for "sub_hello"
assert.same "/world", req\url_for "world"
it "included application supports require'd action", ->
s = {} -- use table address for unique identifier for action result
package.loaded["actions.hello"] = -> "action1 #{s}"
package.loaded["actions.admin.cool"] = -> "action2 #{s}"
class SubApp extends lapis.Application
[hello: "/cool-dad"]: true
[world: "/uncool-dad"]: "admin.cool"
class SomeApp extends lapis.Application
layout: false
@include SubApp
"/some-dad": => "hi"
status, buffer, headers = simulate_request SomeApp, "/cool-dad", {}
assert.same {
status: 200
buffer: "action1 #{tostring s}"
}, { :status, :buffer }
status, buffer, headers = simulate_request SomeApp, "/uncool-dad", {}
assert.same {
status: 200
buffer: "action2 #{tostring s}"
}, { :status, :buffer }
it "included application supports require'd action and include name", ->
s = {}
package.loaded["actions.subapp.hello"] = -> "subapp action1 #{s}"
package.loaded["actions.subapp.admin.cool"] = -> "subapp action2 #{s}"
class SubApp extends lapis.Application
name: "subapp."
[hello: "/cool-dad"]: true
[world: "/uncool-dad"]: "admin.cool"
class SomeApp extends lapis.Application
layout: false
@include SubApp
"/some-dad": => "hi"
status, buffer, headers = simulate_request SomeApp, "/cool-dad", {}
assert.same {
status: 200
buffer: "subapp action1 #{tostring s}"
}, { :status, :buffer }
status, buffer, headers = simulate_request SomeApp, "/uncool-dad", {}
assert.same {
status: 200
buffer: "subapp action2 #{tostring s}"
}, { :status, :buffer }
it "included application supports require'd action with before filter", ->
s = {}
package.loaded["actions.one"] = => "action1 #{s} #{@something}"
package.loaded["actions.admin.two"] = => "action2 #{s} #{@something}"
class SubApp extends lapis.Application
@before_filter (r) =>
@something = "Before filter has run!"
[one: "/cool-dad"]: true
[two: "/uncool-dad"]: "admin.two"
class SomeApp extends lapis.Application
layout: false
@include SubApp
"/some-dad": => "hi"
status, buffer, headers = simulate_request SomeApp, "/cool-dad", {}
assert.same {
status: 200
buffer: "action1 #{s} Before filter has run!"
}, { :status, :buffer }
status, buffer, headers = simulate_request SomeApp, "/uncool-dad", {}
assert.same {
status: 200
buffer: "action2 #{s} Before filter has run!"
}, { :status, :buffer }
status, buffer, headers = simulate_request SomeApp, "/some-dad", {}
assert.same {
status: 200
buffer: "hi"
}, { :status, :buffer }
describe "instance #ddd", ->
it "includes instance into instance", ->
app1 = lapis.Application!
app1\match "/hello", => "hello"
app2 = lapis.Application!
app2.layout = false
app2\match "/world", => "world"
app2\include app1
assert.same "hello", (select 2, simulate_request app2, "/hello")
assert.same "world", (select 2, simulate_request app2, "/world")
it "includes class into instance", ->
class Things extends lapis.Application
[hello: "/hello"]: => "hello!"
app = lapis.Application!
app.layout = false
app\match "/world", => "world!"
app\include Things
assert.same "hello!", (select 2, simulate_request app, "/hello")
assert.same "world!", (select 2, simulate_request app, "/world")
app2 = lapis.Application!
app2.layout = false
app2\match "/world", => "world!"
app2\include Things!
assert.same "hello!", (select 2, simulate_request app2, "/hello")
assert.same "world!", (select 2, simulate_request app2, "/world")
it "includes an instance into a class", ->
class Things extends lapis.Application
[hello: "/hello"]: => "hello!!"
things = Things!
things\match "/world", => "world!!"
class Whoa extends lapis.Application
layout: false
@include things
[whoa: "/whoa"]: => "whoa!!"
assert.same "hello!!", (select 2, simulate_request Whoa, "/hello")
assert.same "world!!", (select 2, simulate_request Whoa, "/world")
assert.same "whoa!!", (select 2, simulate_request Whoa, "/whoa")
describe "default route", ->
it "hits default route", ->
local res
class App extends lapis.Application
"/": =>
default_route: =>
res = "bingo!"
status, body = simulate_request App, "/hello", {}
assert.same 200, status
assert.same "bingo!", res
describe "default layout", ->
it "uses widget as layout", ->
import Widget from require "lapis.html"
class TestApp extends lapis.Application
layout: class Layout extends Widget
content: =>
h1 "hello world"
@content_for "inner"
div class: "footer"
"/": => "yeah"
status, body = assert_request TestApp, "/"
assert.same [[<h1>hello world</h1>yeah<div class="footer"></div>]], body
it "uses module name as layout", ->
import Widget from require "lapis.html"
class Layout extends Widget
content: =>
div class: "content", ->
@content_for "inner"
package.loaded["views.test_layout"] = Layout
class TestApp extends lapis.Application
layout: "test_layout"
"/": => "yeah"
status, body = assert_request TestApp, "/"
assert.same [[<div class="content">yeah</div>]], body
describe "error capturing", ->
import capture_errors, capture_errors_json, assert_error,
yield_error from require "lapis.application"
it "capture_errors", ->
result = "no"
errors = nil
class ErrorApp extends lapis.Application
"/error_route": capture_errors {
on_error: =>
errors = @errors
=>
yield_error "something bad happened!"
result = "yes"
}
assert_request ErrorApp, "/error_route"
assert.same "no", result
assert.same {"something bad happened!"}, errors
it "capture_errors_json", ->
result = "no"
class ErrorApp extends lapis.Application
"/error_route": capture_errors_json =>
yield_error "something bad happened!"
result = "yes"
status, body, headers = assert_request ErrorApp, "/error_route"
assert.same "no", result
assert.same [[{"errors":["something bad happened!"]}]], body
assert.same "application/json", headers["Content-Type"]
describe "respond to", ->
import respond_to from require "lapis.application"
it "responds to basic verbs", ->
class RespondToApp extends lapis.Application
layout: false
"/test": respond_to {
GET: => "GET world"
DELETE: => "DELETE world"
PUT: => "PUT world"
}
request_method = (m) ->
(select 2, assert_request RespondToApp, "/test", method: m)
assert.same "GET world", request_method!
assert.same "DELETE world", request_method "DELETE"
assert.same "PUT world", request_method "PUT"
-- spec for default HEAD
it "responds to HEAD by default", ->
fn = respond_to {
GET: => "hello world"
}
assert.same {
layout: false
}, fn {
req: { method: "HEAD" }
}
it "default missing method handler", ->
fn = respond_to {
HEAD: false -- this disables the default head responder
GET: => "hello world"
}
assert.has_error(
->
fn {
req: { method: "PUT" }
}
"don't know how to respond to PUT"
)
assert.has_error(
->
fn {
req: { method: "HEAD" }
}
"don't know how to respond to HEAD"
)
it "on_error", ->
import yield_error, capture_errors from require "lapis.application"
-- we do an extra capture errors to ensure the right error handler is capturing
fn = capture_errors respond_to {
on_error: =>
json: { captured: "hello", errors: @errors }
POST: =>
yield_error "something bad happened!"
}
assert.same {
json: {
errors: {"something bad happened!"}
captured: "hello"
}
}, fn {
req: { method: "POST" }
}
-- no error handler, the outer should capture
fn = capture_errors respond_to {
POST: => yield_error "something bad happened!"
}
assert.same {
render: true
}, fn {
req: { method: "POST" }
}
it "on_invalid_method", ->
fn = respond_to {
on_invalid_method: =>
"got invalid method...: #{@req.method}"
HEAD: false
POST: =>
"hello"
}
assert.same "hello", fn {
req: { method: "POST" }
}
assert.same "got invalid method...: GET", fn {
req: { method: "GET" }
}
assert.same "got invalid method...: HEAD", fn {
req: { method: "HEAD" }
}
it "on_invalid_method & capture_errors", ->
import yield_error from require "lapis.application"
fn = respond_to {
on_error: =>
"<<#{table.concat @errors, ", "}>>"
on_invalid_method: =>
yield_error "bad method: #{@req.method}"
HEAD: false
POST: => "cool"
}
assert.same "<<bad method: HEAD>>", fn {
req: { method: "HEAD" }
}
assert.same "<<bad method: DELETE>>", fn {
req: { method: "DELETE" }
}
assert.same "cool", fn {
req: { method: "POST" }
}
describe "route builders on class", ->
it "calls match on calss", ->
local result
counter = 0
class SomeApp extends lapis.Application
@before_filter ->
counter += 1
@match "/", => result = "root"
@match "/user/:id", => result = "user_id"
@get "cool", "/cool", => result = "get:#{@route_name}"
@post "cool", "/cool", => result = "post:#{@route_name}"
assert_request SomeApp, "/"
assert.same "root", result
assert.same 1, counter
assert_request SomeApp, "/user/23042"
assert.same "user_id", result
assert.same 2, counter
assert_request SomeApp, "/cool"
assert.same "get:cool", result
assert.same 3, counter
assert_request SomeApp, "/cool", method: "POST"
assert.same "post:cool", result
assert.same 4, counter
assert.has_error ->
assert_request SomeApp, "/cool", method: "DELETE"
it "exposes matched route pattern on request", ->
local named_pattern, unnamed_pattern
local named_route_name, unnamed_route_name
class SomeApp extends lapis.Application
@match "named_user", "/user/:id", =>
named_pattern = @route_pattern
named_route_name = @route_name
@match "/post/:slug", =>
unnamed_pattern = @route_pattern
unnamed_route_name = @route_name
assert_request SomeApp, "/user/123"
assert.same "/user/:id", named_pattern
assert.same "named_user", named_route_name
assert_request SomeApp, "/post/hello-world"
assert.same "/post/:slug", unnamed_pattern
assert.same nil, unnamed_route_name
describe "instancing", ->
it "matchs a route", ->
local res
app = lapis.Application!
app\match "/", => res = "root"
app\match "/user/:id", => res = @params.id
app\build_router!
assert_request app, "/"
assert.same "root", res
assert_request app, "/user/124"
assert.same "124", res
it "responds to verb", ->
local res
app = lapis.Application!
app\match "/one", ->
app\get "/hello", => res = "get"
app\post "/hello", => res = "post"
app\match "two", ->
app\build_router!
assert_request app, "/hello"
assert.same "get", res
assert_request app, "/hello", post: {}
assert.same "post", res
it "doesn't allow route mismatch for verbs", ->
app = lapis.Application!
app\get "one", "/one", ->
assert.has_error(
-> app\post("/one", ->)
"You are trying to add a new verb action to a route that was declared with and existing path but different route name. Please ensure you use the same route name and path combination when adding additional verbs to a route."
)
app\get "/two", ->
assert.has_error(
-> app\post("two", "/two", ->)
"You are trying to add a new verb action to a route that was declared with and existing path but different route name. Please ensure you use the same route name and path combination when adding additional verbs to a route."
)
-- TODO: this should fail
app\get "three", "/three", ->
app\post "three", "/four", ->
it "finds actions by name for verb", ->
local res
package.loaded["actions.one"] = -> res = "one"
package.loaded["actions.two"] = -> res = "two"
package.loaded["actions.three"] = -> res = "three"
package.loaded["actions.four.get"] = -> res = "four GET"
package.loaded["actions.four.post"] = -> res = "four POST"
app = lapis.Application!
app\match "one", "/one", true
app\match "/two", "two"
app\post "/three", "three"
app\get "/four", "four.get"
app\post "/four", "four.post"
app\build_router!
assert_request app, "/one"
assert.same "one", res
assert_request app, "/two"
assert.same "two", res
assert_request app, "/three", method: "POST"
assert.same "three", res
assert_request app, "/four"
assert.same "four GET", res
assert_request app, "/four",method: "POST"
assert.same "four POST", res
it "hits default route", ->
local res
app = lapis.Application!
app\match "/", -> res = "/"
app.default_route = -> res = "default_route"
app\build_router!
assert_request app, "/hello"
assert.same "default_route", res
it "strips trailing / to find route", ->
local res
app = lapis.Application!
app\match "/hello", -> res = "/hello"
app\match "/world/", -> res = "/world/"
app\build_router!
-- exact match, no default action
assert_request app, "/world/"
assert.same "/world/", res
status, _, headers = assert_request app, "/hello/"
assert.same 301, status
assert.same "http://localhost/hello", headers.location
it "preserves order of route", ->
app = lapis.Application!
routes = for i=1,20
with r = "/route#{i}"
app\get r, =>
app\build_router!
assert.same routes, [tuple[1] for tuple in *app.router.routes]
describe "errors", ->
class ErrorApp extends lapis.Application
"/": =>
error "I am an error!"
it "renders default error page", ->
status, body, h = simulate_request ErrorApp, "/", allow_error: true
assert.same 500, status
assert.truthy (body\match "I am an error")
-- only set on test env
assert.truthy h["X-Lapis-Error"]
it "renders default error page as json", ->
status, body, h = simulate_request ErrorApp, "/", {
allow_error: true
expect: "json"
headers: {
accept: "application/json"
}
}
assert.same 500, status
assert.truthy body.error
assert body.error\match "I am an error"
assert.truthy body.traceback
assert.truthy body.lapis
-- only set on test env
assert.truthy h["X-Lapis-Error"]
it "raises error in spec by default", ->
assert.has_error ->
simulate_request ErrorApp, "/"
it "renders custom error page", ->
class CustomErrorApp extends lapis.Application
handle_error: (err, msg) =>
assert.truthy @original_request
"hello world", layout: false, status: 444
"/": =>
error "I am an error!"
status, body, h = simulate_request CustomErrorApp, "/", allow_error: true
assert.same 444, status
assert.same "hello world", body
-- should still be set
assert.truthy h["X-Lapis-Error"]
describe "custom request", ->
it "renders with custom request (overriding supuport)", ->
class R extends lapis.Application.Request
@support: {
load_session: =>
@session = {"cool"}
write_session: =>
}
local the_session
class A extends lapis.Application
Request: R
"/": =>
the_session = @session
"ok"
simulate_request A, "/"
assert.same {"cool"}, the_session
-- should be requrest spec?
describe "inline html", ->
class HtmlApp extends lapis.Application
layout: false
"/": =>
@html -> div "hello world"
it "renders html", ->
status, body = assert_request HtmlApp, "/"
assert.same "<div>hello world</div>", body
-- this should be in request spec...
describe "request:build_url", ->
it "build url", ->
assert.same "http://localhost", mock_app "/hello", {}, =>
@build_url!
it "build url with path", ->
assert.same "http://localhost/hello_dog", mock_app "/hello", {}, =>
@build_url "hello_dog"
it "build url with host and port", ->
assert.same "http://leaf:2000/hello",
mock_app "/hello", { host: "leaf", port: 2000 }, =>
@build_url @req.parsed_url.path
it "doesn't include default port for scheme http", ->
assert.same "http://leaf/whoa",
mock_app "/hello", { host: "leaf", port: 80 }, =>
@build_url "whoa"
it "doesn't include default port for scheme https", ->
assert.same "https://leaf/whoa",
mock_app "/hello", { host: "leaf", scheme: "https", port: 443 }, =>
@build_url "whoa"
it "build url with overridden query", ->
assert.same "http://localhost/please?yes=no",
mock_app "/hello", {}, =>
@build_url "please?okay=world", { query: "yes=no" }
it "build url with overridden port and host", ->
assert.same "http://yes:4545/cat?sure=dad",
mock_app "/hello", { host: "leaf", port: 2000 }, =>
@build_url "cat?sure=dad", host: "yes", port: 4545
it "return arg if already build url", ->
assert.same "http://leafo.net",
mock_app "/hello", { host: "leaf", port: 2000 }, =>
@build_url "http://leafo.net"