Skip to content

Commit

Permalink
Use params validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Apr 4, 2014
1 parent d91500a commit f60e3c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion api/post_put.rb
Expand Up @@ -12,8 +12,10 @@ class << self
result = (PostPut.rang += 1)
{ rang: result }
end
params do
requires :count, type: Integer
end
put :ring do
error!("missing :count", 400) unless params[:count]
result = (PostPut.rang += params[:count].to_i)
{ rang: result }
end
Expand Down
15 changes: 11 additions & 4 deletions spec/api/post_put_spec.rb
Expand Up @@ -25,10 +25,17 @@ def app
get "/api/ring"
last_response.body.should == { rang: @rang + 2 }.to_json
end
it "PUT ring" do
put "/api/ring?count=2"
last_response.status.should == 200
last_response.body.should == { rang: @rang + 2 }.to_json
context "PUT ring" do
it "succeeds for 2 rings" do
put "/api/ring?count=2"
last_response.status.should == 200
last_response.body.should == { rang: @rang + 2 }.to_json
end
it "fails with a missing ring" do
put "/api/ring"
last_response.status.should == 400
last_response.body.should == { error: 'count is missing' }.to_json
end
end
end
end

0 comments on commit f60e3c5

Please sign in to comment.