require File.expand_path("../../../spec_helper", __FILE__) module Bamboo module Client module Http describe Json do let(:url) { "http://example.com" } let(:json) { Json.new(url) } it "does a GET" do RestClient.should_receive(:get).with( "#{url}/", :accept => :json, :cookies => nil ).and_return('{"some": "data"}') doc = json.get "/" doc.should be_kind_of(Json::Doc) end it "does a POST" do RestClient.should_receive(:post).with( "#{url}/", '{"some":"data"}', :accept => :json, :content_type => :json, :cookies => nil ).and_return('') json.post("/", :some => "data").should be_nil end it 'does a POST with query instead of JSON data' do RestClient.should_receive(:post).with( "#{url}/?some=data", '{}', :accept => :json, :content_type => :json, :cookies => nil ).and_return('') json.post_with_query("/", :some => "data").should be_nil end it "returns cookies from GET" do net_http_resp = Net::HTTPResponse.new(1.0, 200, "OK") net_http_resp.add_field 'Set-Cookie', 'Cookie=Value;' resp = RestClient::Response.create("",net_http_resp, nil) RestClient.should_receive(:get).with("#{url}/", :params => nil).and_return(resp) cookies = json.get_cookies("/").should == {'Cookie' => 'Value'} end end describe Json::Doc do it "auto expands a one-level expansion" do doc = Json::Doc.new( "expand" => "a", "a" => {"some" => "data"} ) doc.auto_expand(SpecHelper::Wrapper, nil).obj.should == {"some" => "data"} end it "auto expands Arrays" do expected = [{"some" => "data"}, {"something" => "else"}] doc = Json::Doc.new( "expand" => "a", "a" => expected ) actual = doc.auto_expand(SpecHelper::Wrapper, nil).map { |e| e.obj } actual.should == expected end it "auto-expands the next expansion level" do doc = Json::Doc.new( "expand" => "a", "a" => { "expand" => "b", "b" => %w[foo bar] } ) actual = doc.auto_expand(SpecHelper::Wrapper, nil).map { |e| e.obj } actual.should == %w[foo bar] end it "raises a TypeError if an object can't be expanded" do doc = Json::Doc.new( "expand" => "a", "a" => 1 ) lambda { doc.auto_expand(SpecHelper::Wrapper, nil) }.should raise_error(TypeError) end it "returns a doc for the given key" do doc = Json::Doc.new("foo" => {"bar" => "baz"}) doc.doc_for("foo").data.should == {"bar" => "baz"} end end end # Http end # Client end # Bamboo