%w(support base64 hugs).each { |r| require r } describe Hugs do before do @scheme = "https" @host = "example.com" @port = 80 @base = "#{@host}:#{@port}" @valid_options = { :host => @host, :port => @port, :scheme => @scheme, } WebMock.reset! @instance = Hugs.new @valid_options end describe "#response_for" do before do @request = Net::HTTP::Get end describe "path" do it "is valid" do stub_request :get, "#{@scheme}://#{@base}/" @instance.send :response_for, @request, "/", {} assert_requested :get, "#{@scheme}://#{@base}/" end it "is valid when an invalid :query is supplied" do stub_request :get, "#{@scheme}://#{@base}/" @instance.send :response_for, @request, "/", :query => nil assert_requested :get, "#{@scheme}://#{@base}/" end it "also has a query string" do stub_request(:get, "#{@scheme}://#{@base}/").with:query => {"foo" => "bar"} @instance.send :response_for, @request, "/", :query => "foo=bar" assert_requested :get, "#{@scheme}://#{@base}/", :query => {"foo" => "bar"} end end describe "multi-part" do Content_Type_Matcher = %r{multipart/form-data} before do @request = Net::HTTP::Post end it "uploads a file" do stub_request :post, "#{@scheme}://#{@base}/" upload = { :upload => { :parts => { :file => "/dev/null" }, :content_type => "type/subtype" } } @instance.send :response_for, @request, "/", upload assert_requested :post, "#{@scheme}://#{@base}/", :body => %r{Content-Type: type/subtype}, :headers => { "Content-Type" => Content_Type_Matcher } end it "has parts" do stub_request :post, "#{@scheme}://#{@base}/" upload = { :upload => { :parts => { :foo => :bar, :baz => :xyzzy }, :content_type => "foo/bar" } } @instance.send :response_for, @request, "/", upload ### wtf can't use mx together. content_disposition_matcher = %r{^Content-Disposition: form-data; name="foo".*^bar.*^Content-Disposition: form-data; name="baz".*^xyzzy.*}m assert_requested :post, "#{@scheme}://#{@base}/", :body => content_disposition_matcher, :headers => { "Content-Type" => Content_Type_Matcher } end end describe "body" do before do stub_request :get, "#{@scheme}://#{@base}/" end it "is not set when :body invalid" do @instance.send :response_for, @request, "/", :body => nil assert_requested :get, "#{@scheme}://#{@base}/", :body => nil end it "is not set when :body is missing" do @instance.send :response_for, @request, "/", {} assert_requested :get, "#{@scheme}://#{@base}/", {} end describe "json" do before do @instance = Hugs.new @valid_options.merge(:type => :json) end it "is valid" do @instance.send :response_for, @request, "/", :body => {:foo => :bar} assert_requested :get, "#{@scheme}://#{@base}/", :body => '{"foo":"bar"}' end end describe "xml" do before do @instance = Hugs.new @valid_options.merge(:type => :xml) end it "is valid" do @instance.send :response_for, @request, "/", :body => "foo bar" assert_requested :get, "#{@scheme}://#{@base}/", :body => "foo bar" end end end describe "headers" do it "authenticates" do stub_request :get, "#{@scheme}://user:credentials@#{@base}/" @instance = Hugs.new @valid_options.merge(:user => "user", :password => "credentials") @instance.send :response_for, @request, "/", {} assert_requested :get, "#{@scheme}://user:credentials@#{@base}/" end [:user, :password].each do |option| it "doesn't authenticate when '#{option}' missing" do stub_request :get, "#{@scheme}://#{@base}/" invalid_options = @valid_options.reject { |k,v| k == option } @instance = Hugs.new invalid_options @instance.send :response_for, @request, "/", {} assert_requested :get, "#{@scheme}://#{@base}/" end end [ { :json => "application/json" }, { :xml => "application/xml" }, ].each do |pair| pair.each_pair do |type, subtype| [:post, :put].each do |verb| clazz = eval "Net::HTTP::#{verb.capitalize}" it "has '#{subtype}' Content-Type and Accept for '#{clazz}'" do stub_request verb, "#{@scheme}://#{@base}/" @instance = Hugs.new @valid_options.merge(:type => type) @instance.send :response_for, clazz, "/", {} assert_requested verb, "#{@scheme}://#{@base}/", :headers => { "Accept" => ["*/*", subtype], "Content-Type" => subtype } end end [:get, :delete].each do |verb| clazz = eval "Net::HTTP::#{verb.capitalize}" it "has '#{subtype}' Accept for '#{clazz}'" do stub_request verb, "#{@scheme}://#{@base}/" @instance = Hugs.new @valid_options.merge(:type => type) @instance.send :response_for, clazz, "/", {} assert_requested verb, "#{@scheme}://#{@base}/", :headers => { "Accept" => ["*/*", subtype] } end end end end end end describe "HTTP methods" do describe "json" do before do @instance = Hugs.new @valid_options.merge(:type => :json) end it "objectifies and returns a hash" do stub_request(:get, "#{@scheme}://#{@base}/").to_return :body => '{"foo":"bar"}' response = @instance.get "/", :body => { :foo => :bar } response.body.must_be_kind_of Hash end end describe "xml" do before do @instance = Hugs.new @valid_options.merge(:type => :xml) end it "parses and returns a Nokogiri object" do stub_request(:get, "#{@scheme}://#{@base}/").to_return :body => "" response = @instance.get "/", :body => { :foo => :bar } response.body.must_be_kind_of Nokogiri::XML::Document end end end end