Created
November 13, 2008 03:32
-
-
Save jackdempsey/24351 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb') | |
| include Merb::ControllerExceptions | |
| describe "JSON service" do | |
| after do | |
| @response.should have_content_type(:json) | |
| end | |
| describe "#index" do | |
| before do | |
| @response = request("/gists.json") | |
| end | |
| it "should return successfully" do | |
| @response.should be_successful | |
| end | |
| end | |
| describe "#show" do | |
| describe "successful" do | |
| before do | |
| gist = Gist.create(:url => 'www.example.com') | |
| @response = request("/gists/#{gist.id}.json") | |
| end | |
| it "should display an item when found" do | |
| @response.should be_successful | |
| end | |
| end | |
| describe "unsuccessful" do | |
| before do | |
| @response = request("/gists/0.json") | |
| end | |
| it "should return a NotFound (404) if missing" do | |
| @response.should be_missing | |
| end | |
| end | |
| end | |
| describe "#create" do | |
| describe "successful" do | |
| before do | |
| #curl -H "Content-Type:application/json" -d "{\"gist\":{\"url\":\"test.com\"}}" http://localhost:4000/gists.json | |
| @response = request("/gists.json", :params => {:gist => {:url => 'www.example.com'}}, | |
| :method => "POST") # we can't use :post; look in the code, it checks for "POST" | |
| end | |
| it "should render successfully" do | |
| @response.should be_successful | |
| end | |
| it "should return a Created (201) status" do | |
| @response.status.should == Created.status | |
| end | |
| end | |
| describe "unsuccessful" do | |
| before do | |
| @response = request("/gists.json", :params => {:gist => {:url => ''}}, | |
| :method => "POST") | |
| end | |
| it "should return a BadRequest (400)" do | |
| @response.should be_client_error | |
| end | |
| it "should display the errors on the gist" do | |
| @response.body.to_s.should include("Url must not be blank") | |
| end | |
| end | |
| end | |
| describe "#update" do | |
| describe "successful" do | |
| before do | |
| # create object to update | |
| result = request("/gists.json", :params => {:gist => {:url => 'www.example.com'}}, | |
| :method => 'POST') | |
| result_body = JSON.parse(result.body.to_s) | |
| # send the update | |
| @new_url = "#{result_body['url']}#{Time.now.to_i}" | |
| @response = request("/gists/#{result_body['id']}.json", :params => {:gist => {:url => @new_url}}, | |
| :method => 'PUT') | |
| @body = JSON.parse(@response.body.to_s) | |
| end | |
| it "should return an Accepted (202) status" do | |
| @response.status.should == Accepted.status | |
| end | |
| it "should display the gist with updated attributes" do | |
| @body['url'].should == @new_url | |
| end | |
| end | |
| describe "unsuccessful" do | |
| before do | |
| # create object to update | |
| result = request("/gists.json", :params => {:gist => {:url => 'www.example.com'}}, | |
| :method => 'POST') | |
| result_body = JSON.parse(result.body.to_s) | |
| # send the bad update | |
| @response = request("/gists/#{result_body['id']}.json", :params => {:gist => {:url => ''}}, | |
| :method => 'PUT') | |
| @body = JSON.parse(@response.body.to_s) | |
| end | |
| it "should return a NotFound (404) if missing" do | |
| request("/gists/0.json", :params => {:gist => {:url => ''}},:method => 'PUT').should be_missing | |
| end | |
| it "should return a BadRequest (400)" do | |
| @response.should be_client_error | |
| end | |
| it "should display the errors" do | |
| # look at standard_error.json.erb for a layout of whats displayed. That structure is why we're looking at @body['exceptions'] here | |
| # and grabbing the list of exceptions to search in | |
| @body['exceptions'].map {|exception| exception['message']}.should include("Url must not be blank") | |
| end | |
| end | |
| end | |
| describe "#destroy" do | |
| describe "successful" do | |
| before do | |
| # create a gist to destroy | |
| result = request("/gists.json", :params => {:gist => {:url => 'www.example.com'}}, | |
| :method => 'POST') | |
| result_body = JSON.parse(result.body.to_s) | |
| # send the delete | |
| @response = request("/gists/#{result_body['id']}.json", :method => 'DELETE') | |
| end | |
| it "should render a NoContent (204)" do | |
| @response.status.should == NoContent.status # a successful DELETE returns a 204 and no body | |
| end | |
| it "should return an empty body" do | |
| @response.should have_body('') | |
| end | |
| end | |
| describe "unsuccessful" do | |
| before do | |
| # send the delete for a missing id | |
| @response = request("/gists/0.json", :method => 'DELETE') | |
| end | |
| it "should return a NotFound (404) if missing" do | |
| @response.should be_missing | |
| end | |
| it "should return a BadRequest (400)" do | |
| @response.should be_client_error | |
| end | |
| it "should display the NotFound (404) errors" do | |
| body = JSON.parse(@response.body.to_s) | |
| body['exceptions'].map {|exception| exception['message']}.should include("Merb::ControllerExceptions::NotFound") | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment