Skip to content

Commit ca799a9

Browse files
committed
Merge pull request postrank-labs#54 from wicz/master
Class-based map never gets executed
2 parents 90cdd5c + 8ea10e9 commit ca799a9

File tree

7 files changed

+155
-46
lines changed

7 files changed

+155
-46
lines changed

‎examples/rack_routes.rb‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ def response(env)
2727
end
2828

2929
classHola < Goliath::API
30+
useGoliath::Rack::Params
31+
useGoliath::Rack::Validation::RequiredParam,{:key=>"foo"}
32+
3033
defresponse(env)
31-
[200,{},"¡hola!"]
34+
[200,{},"hola!"]
3235
end
3336
end
3437

@@ -55,7 +58,6 @@ class RackRoutes < Goliath::API
5558

5659
map"/hola"do
5760
useGoliath::Rack::Validation::RequestMethod,%w(GET)
58-
5961
runHola.new
6062
end
6163

@@ -64,4 +66,9 @@ class RackRoutes < Goliath::API
6466
map'/'do
6567
runProc.new{ |env| [404,{"Content-Type"=>"text/html"},["Try /version /hello_world, /bonjour, or /hola"]]}
6668
end
69+
70+
# You must use either maps or response, but never both!
71+
defresponse(env)
72+
raiseRuntimeException.new("#response is ignored when using maps, so this exception won't raise. See spec/integration/rack_routes_spec.")
73+
end
6774
end

‎lib/goliath/api.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def maps
8383
@maps ||= []
8484
end
8585

86+
defmaps?
87+
!maps.empty?
88+
end
89+
8690
# Specify a router map to be used by the API
8791
#
8892
# @example

‎lib/goliath/rack/builder.rb‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,17 @@ def self.build(klass, api)
1212
use(mw_klass, *args, &blk)
1313
end
1414

15-
# If you use map you can't use run as
16-
# the rack builder will blowup.
17-
ifklass.maps.empty?
18-
runapi
19-
else
20-
klass.maps.eachdo |(path,route_klass,blk)|
21-
ifroute_klass
22-
map(path)do
23-
route_klass.middlewares.eachdo |mw_klass,args,blk|
24-
use(mw_klass, *args, &blk)
25-
end
26-
runklass.new
27-
end
28-
else
29-
map(path, &blk)
30-
end
31-
end
15+
klass.maps.eachdo |path,route_klass,blk|
16+
blk ||= Proc.new{
17+
runBuilder.build(route_klass,route_klass.new)
18+
}
19+
map(path, &blk)
3220
end
21+
22+
runapiunlessklass.maps?
3323
end
3424
end
25+
3526
end
3627
end
3728
end

‎spec/integration/rack_routes_spec.rb‎

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,67 @@
55
describeRackRoutesdo
66
let(:err){Proc.new{fail"API request failed"}}
77

8-
it'routes to the correct API'do
9-
with_api(RackRoutes)do
10-
get_request({:path=>'/bonjour'},err)do |c|
11-
c.response_header.status.should == 200
12-
c.response.should == 'bonjour!'
8+
context"when using maps"do
9+
10+
it"ignores #response"do
11+
expect{
12+
with_api(RackRoutes)do
13+
get_request({:path=>'/'},err){}
14+
end
15+
}.to_notraise_error
16+
end
17+
18+
it'fallback not found to /'do
19+
with_api(RackRoutes)do
20+
get_request({:path=>'/donkey'},err)do |cb|
21+
cb.response_header.status.should == 404
22+
cb.response.should == 'Try /version /hello_world, /bonjour, or /hola'
23+
end
1324
end
1425
end
15-
end
1626

17-
it'routes to the default'do
18-
with_api(RackRoutes)do
19-
get_request({:path=>'/donkey'},err)do |c|
20-
c.response_header.status.should == 404
21-
c.response.should == 'Try /version /hello_world, /bonjour, or /hola'
27+
it'routes to the correct API'do
28+
with_api(RackRoutes)do
29+
get_request({:path=>'/bonjour'},err)do |c|
30+
c.response_header.status.should == 200
31+
c.response.should == 'bonjour!'
32+
end
2233
end
2334
end
24-
end
2535

26-
it'uses API middleware'do
27-
with_api(RackRoutes)do
28-
post_request({:path=>'/hola'},err)do |c|
29-
# the /hola route only supports GET requests
30-
c.response_header.status.should == 400
31-
c.response.should == '[:error, "Invalid request method"]'
36+
context"defined in blocks"do
37+
it'uses middleware defined in the block'do
38+
with_api(RackRoutes)do
39+
post_request({:path=>'/hola'},err)do |c|
40+
# the /hola route only supports GET requests
41+
c.response_header.status.should == 400
42+
c.response.should == '[:error, "Invalid request method"]'
43+
end
44+
end
45+
end
46+
47+
it"doesn't use middleware defined in the API"do
48+
with_api(RackRoutes)do
49+
get_request({:path=>'/hola'},err)do |cb|
50+
# it doesn't raise required param error
51+
cb.response_header.status.should == 200
52+
cb.response.should == "hola!"
53+
end
54+
end
3255
end
3356
end
34-
end
3557

36-
it'uses API middleware'do
37-
with_api(RackRoutes)do
38-
post_request({:path=>'/aloha'},err)do |c|
39-
# the /hola route only supports GET requests
40-
c.response_header.status.should == 400
41-
c.response.should == '[:error, "Invalid request method"]'
58+
context"defined in classes"do
59+
it'uses API middleware'do
60+
with_api(RackRoutes)do
61+
post_request({:path=>'/aloha'},err)do |c|
62+
# the /hola route only supports GET requests
63+
c.response_header.status.should == 400
64+
c.response.should == '[:error, "Invalid request method"]'
65+
end
66+
end
4267
end
4368
end
69+
4470
end
45-
end
71+
end

‎spec/spec_helper.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
require'goliath/test_helper'
77

8+
Goliath.env=:test
9+
810
RSpec.configuredo |c|
911
c.includeGoliath::TestHelper,:example_group=>{
1012
:file_path=>/spec\/integration/
1113
}
12-
end
14+
end

‎spec/unit/api_spec.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,29 @@
22
require'goliath/api'
33

44
describeGoliath::APIdo
5+
6+
DummyApi=Class.new(Goliath::API)
7+
58
describe'middlewares'do
69
it"doesn't change after multi calls"do
710
2.times{Goliath::API.shouldhave(2).middlewares}
811
end
912
end
13+
14+
describe".maps?"do
15+
context"when not using maps"do
16+
it"returns false"do
17+
DummyApi.maps?.shouldbe_false
18+
end
19+
end
20+
21+
context"when using maps"do
22+
it"returns true"do
23+
DummyApi.map"/foo"
24+
DummyApi.maps?.shouldbe_true
25+
end
26+
end
27+
end
28+
29+
1030
end

‎spec/unit/rack/builder_spec.rb‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# encoding: utf-8
2+
require'spec_helper'
3+
4+
describeGoliath::Rack::Builderdo
5+
6+
SimpleMiddleware=Class.new
7+
NestedMiddleware=Class.new
8+
9+
classNestedClassApi < Goliath::API
10+
useNestedMiddleware,'class'
11+
end
12+
13+
classNestedBlockApi < Goliath::API
14+
useNestedMiddleware,'block'
15+
end
16+
17+
classSimpleRouter < Goliath::API
18+
useSimpleMiddleware
19+
map"/class",NestedClassApi
20+
map"/block"do
21+
runNestedBlockApi.new
22+
end
23+
end
24+
25+
let(:router){SimpleRouter.new}
26+
27+
describe'.build'do
28+
29+
it"builds rack app for api"do
30+
Rack::Builder.should_receive(:app)
31+
Goliath::Rack::Builder.build(router.class,router)
32+
end
33+
34+
it"loads rack/goliath middlewares"do
35+
SimpleMiddleware.should_receive(:new)
36+
Goliath::Rack::Builder.build(router.class,router)
37+
end
38+
39+
context"when API using maps"do
40+
41+
it"loads mappings from classes"do
42+
NestedClassApi.should_receive(:new)# once in Builder
43+
Goliath::Rack::Builder.build(router.class,router)
44+
end
45+
46+
it"loads mappings from blocks"do
47+
NestedBlockApi.should_receive(:new)# once in SimpleRouter
48+
Goliath::Rack::Builder.build(router.class,router)
49+
end
50+
51+
it"loads middlewares only for class mappings"do
52+
NestedMiddleware.should_receive(:new)# once in NestedClassApi
53+
Goliath::Rack::Builder.build(router.class,router)
54+
end
55+
56+
end
57+
58+
end
59+
end

0 commit comments

Comments
(0)