Skip to content

Commit bfa86c0

Browse files
committed
Merged from upstream.
2 parents 4d01406 + fb69fb8 commit bfa86c0

File tree

4 files changed

+108
-93
lines changed

4 files changed

+108
-93
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Next Release
1111
* [#159](https://github.com/intridea/grape/pull/159): Added `:requirements` to routes, allowing to use reserved characters in paths - [@gaiottino](https://github.com/gaiottino).
1212
* [#156](https://github.com/intridea/grape/pull/156): Added support for adding formatters to entities - [@bobbytables](https://github.com/bobbytables).
1313
* [#183](https://github.com/intridea/grape/pull/183): Added ability to include documentation in entities - [@flah00](https://github.com/flah00).
14+
* [#189](https://github.com/intridea/grape/pull/189): `HEAD` requests no longer return a body - [@stephencelis](https://github.com/stephencelis).
1415
* [#97](https://github.com/intridea/grape/issues/97): Allow overriding `Content-Type` - [@dblock](https://github.com/dblock).
1516

1617
0.2.0 (3/28/2012)

lib/grape/endpoint.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ def run(env)
301301
def build_middleware
302302
b = Rack::Builder.new
303303

304+
b.use Rack::Head
304305
b.use Grape::Middleware::Error,
305306
:default_status => settings[:default_error_status] || 403,
306307
:rescue_all => settings[:rescue_all],

spec/grape/api_spec.rb

Lines changed: 105 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def app; subject end
284284
verb
285285
end
286286
send(verb, '/example')
287-
last_response.body.should eql verb
287+
last_response.body.should eql verb == 'head' ? '' : verb
288288
# Call it with a method other than the properly constrained one.
289289
send(verbs[(verbs.index(verb) + 1) % verbs.size], '/example')
290290
last_response.status.should eql 404
@@ -788,16 +788,15 @@ def three
788788
end
789789
end
790790
describe "api structure with two versions and a namespace" do
791-
class TwitterAPI < Grape::API
792-
# version v1
793-
version 'v1', :using => :path
794-
get "version" do
791+
before :each do
792+
subject.version 'v1', :using => :path
793+
subject.get "version" do
795794
api.version
796795
end
797796
# version v2
798-
version 'v2', :using => :path
799-
prefix 'p'
800-
namespace "n1" do
797+
subject.version 'v2', :using => :path
798+
subject.prefix 'p'
799+
subject.namespace "n1" do
801800
namespace "n2" do
802801
get "version" do
803802
api.version
@@ -806,22 +805,22 @@ class TwitterAPI < Grape::API
806805
end
807806
end
808807
it "should return versions" do
809-
TwitterAPI::versions.should == [ 'v1', 'v2' ]
808+
subject.versions.should == [ 'v1', 'v2' ]
810809
end
811810
it "should set route paths" do
812-
TwitterAPI::routes.size.should >= 2
813-
TwitterAPI::routes[0].route_path.should == "/:version/version(.:format)"
814-
TwitterAPI::routes[1].route_path.should == "/p/:version/n1/n2/version(.:format)"
811+
subject.routes.size.should >= 2
812+
subject.routes[0].route_path.should == "/:version/version(.:format)"
813+
subject.routes[1].route_path.should == "/p/:version/n1/n2/version(.:format)"
815814
end
816815
it "should set route versions" do
817-
TwitterAPI::routes[0].route_version.should == 'v1'
818-
TwitterAPI::routes[1].route_version.should == 'v2'
816+
subject.routes[0].route_version.should == 'v1'
817+
subject.routes[1].route_version.should == 'v2'
819818
end
820819
it "should set a nested namespace" do
821-
TwitterAPI::routes[1].route_namespace.should == "/n1/n2"
820+
subject.routes[1].route_namespace.should == "/n1/n2"
822821
end
823822
it "should set prefix" do
824-
TwitterAPI::routes[1].route_prefix.should == 'p'
823+
subject.routes[1].route_prefix.should == 'p'
825824
end
826825
end
827826
describe "api structure with additional parameters" do
@@ -839,87 +838,101 @@ class TwitterAPI < Grape::API
839838
last_response.body.should == '["a","b,c"]'
840839
end
841840
it "should set route_params" do
842-
subject.routes.size.should == 1
843-
subject.routes[0].route_params.should == { "string" => "", "token" => "a token" }
844-
subject.routes[0].route_optional_params.should == { "limit" => "the limit" }
841+
subject.routes.map { |route|
842+
{ :params => route.route_params, :optional_params => route.route_optional_params }
843+
}.should eq [
844+
{ :params => { "string" => "", "token" => "a token" }, :optional_params => { "limit" => "the limit" } }
845+
]
845846
end
846847
end
847848
end
848849

849850
context "desc" do
850-
describe "empty api structure" do
851-
it "returns an empty array of routes" do
852-
subject.desc "grape api"
853-
subject.routes.should == []
854-
end
855-
end
856-
describe "single method with a desc" do
857-
before(:each) do
858-
subject.desc "ping method"
859-
subject.get :ping do
860-
'pong'
861-
end
862-
end
863-
it "returns route description" do
864-
subject.routes[0].route_description.should == "ping method"
865-
end
866-
end
867-
describe "single method with a an array of params and a desc hash block" do
868-
before(:each) do
869-
subject.desc "ping method", { :params => { "x" => "y" } }
870-
subject.get "ping/:x" do
871-
'pong'
872-
end
873-
end
874-
it "returns route description" do
875-
subject.routes[0].route_description.should == "ping method"
876-
end
877-
end
878-
describe "api structure with multiple methods and descriptions" do
879-
before(:each) do
880-
class JitterAPI < Grape::API
881-
desc "first method"
882-
get "first" do; end
883-
get "second" do; end
884-
desc "third method"
885-
get "third" do; end
886-
end
887-
end
888-
it "should return a description for the first method" do
889-
JitterAPI::routes[0].route_description.should == "first method"
890-
JitterAPI::routes[1].route_description.should be_nil
891-
JitterAPI::routes[2].route_description.should == "third method"
892-
end
893-
end
894-
describe "api structure with multiple methods, namespaces, descriptions and options" do
895-
before(:each) do
896-
class LitterAPI < Grape::API
897-
desc "first method"
898-
get "first" do; end
899-
get "second" do; end
900-
namespace "ns" do
901-
desc "ns second", :foo => "bar"
902-
get "second" do; end
903-
end
904-
desc "third method", :details => "details of third method"
905-
get "third" do; end
906-
desc "Reverses a string.", { :params =>
907-
{ "s" => { :desc => "string to reverse", :type => "string" }}
908-
}
909-
get "reverse" do
910-
params[:s].reverse
911-
end
912-
end
913-
end
914-
it "should return a description for the first method" do
915-
LitterAPI::routes[0].route_description.should == "first method"
916-
LitterAPI::routes[1].route_description.should be_nil
917-
LitterAPI::routes[2].route_description.should == "ns second"
918-
LitterAPI::routes[2].route_foo.should == "bar"
919-
LitterAPI::routes[3].route_description.should == "third method"
920-
LitterAPI::routes[4].route_description.should == "Reverses a string."
921-
LitterAPI::routes[4].route_params.should == { "s" => { :desc => "string to reverse", :type => "string" }}
922-
end
851+
it "empty array of routes" do
852+
subject.routes.should == []
853+
end
854+
it "empty array of routes" do
855+
subject.desc "grape api"
856+
subject.routes.should == []
857+
end
858+
it "should describe a method" do
859+
subject.desc "first method"
860+
subject.get :first do ; end
861+
subject.routes.length.should == 1
862+
route = subject.routes.first
863+
route.route_description.should == "first method"
864+
route.route_foo.should be_nil
865+
route.route_params.should == { }
866+
end
867+
it "should describe methods separately" do
868+
subject.desc "first method"
869+
subject.get :first do ; end
870+
subject.desc "second method"
871+
subject.get :second do ; end
872+
subject.routes.count.should == 2
873+
subject.routes.map { |route|
874+
{ :description => route.route_description, :params => route.route_params }
875+
}.should eq [
876+
{ :description => "first method", :params => {} },
877+
{ :description => "second method", :params => {} }
878+
]
879+
end
880+
it "should reset desc" do
881+
subject.desc "first method"
882+
subject.get :first do ; end
883+
subject.get :second do ; end
884+
subject.routes.map { |route|
885+
{ :description => route.route_description, :params => route.route_params }
886+
}.should eq [
887+
{ :description => "first method", :params => {} },
888+
{ :description => nil, :params => {} }
889+
]
890+
end
891+
it "should namespace and describe arbitrary parameters" do
892+
subject.namespace "ns" do
893+
desc "ns second", :foo => "bar"
894+
get "second" do ; end
895+
end
896+
subject.routes.map { |route|
897+
{ :description => route.route_description, :foo => route.route_foo, :params => route.route_params }
898+
}.should eq [
899+
{ :description => "ns second", :foo => "bar", :params => {} },
900+
]
901+
end
902+
it "should include details" do
903+
subject.desc "method", :details => "method details"
904+
subject.get "method" do ; end
905+
subject.routes.map { |route|
906+
{ :description => route.route_description, :details => route.route_details, :params => route.route_params }
907+
}.should eq [
908+
{ :description => "method", :details => "method details", :params => {} },
909+
]
910+
end
911+
it "should describe a method with parameters" do
912+
subject.desc "Reverses a string.", { :params =>
913+
{ "s" => { :desc => "string to reverse", :type => "string" }}
914+
}
915+
subject.get "reverse" do
916+
params[:s].reverse
917+
end
918+
subject.routes.map { |route|
919+
{ :description => route.route_description, :params => route.route_params }
920+
}.should eq [
921+
{ :description => "Reverses a string.", :params => { "s" => { :desc => "string to reverse", :type => "string" } } }
922+
]
923+
end
924+
it "should not symbolize params" do
925+
subject.desc "Reverses a string.", { :params =>
926+
{ "s" => { :desc => "string to reverse", :type => "string" }}
927+
}
928+
subject.get "reverse/:s" do
929+
params[:s].reverse
930+
end
931+
subject.routes.map { |route|
932+
{ :description => route.route_description, :params => route.route_params }
933+
}.should eq [
934+
{ :description => "Reverses a string.", :params => { "s" => { :desc => "string to reverse", :type => "string" } } }
935+
]
923936
end
924937
end
925938

spec/grape/endpoint_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def memoized
410410
end
411411
send(verb, '/example/and/some/more')
412412
last_response.status.should eql (verb == "post" ? 201 : 200)
413-
last_response.body.should eql verb
413+
last_response.body.should eql verb == 'head' ? '' : verb
414414
end
415415
end
416416
end

0 commit comments

Comments
 (0)