spec/lib/proxy_spec.rb in flexirest-1.3.16 vs spec/lib/proxy_spec.rb in flexirest-1.3.17

- old
+ new

@@ -1,7 +1,8 @@ require 'spec_helper' require 'active_support/core_ext/hash' +require 'base64' class ProxyExample < Flexirest::ProxyBase get "/all" do url.gsub!("/all", "/getAll") passthrough @@ -28,11 +29,11 @@ put "/update" do body "MY-BODY-CONTENT" passthrough end - + patch "/partial_update" do body "MY-BODY-CONTENT" passthrough end @@ -91,10 +92,12 @@ delete :remove, "/remove" get :hal_test, "/hal_test/:id" end describe Flexirest::Base do + before(:each) { ProxyClientExample.base_url 'http://www.example.com' } + it "allows the URL to be changed" do expect_any_instance_of(Flexirest::Connection).to receive(:get).with("/getAll?id=1", instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{})) ProxyClientExample.all(id:1) end @@ -110,11 +113,11 @@ it "has access to the POST params and allow them to be changed/removed/added" do expect_any_instance_of(Flexirest::Connection).to receive(:post).with("/create", {age:12, first_name:"John"}.to_query, instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", status:200, headers:{})) ProxyClientExample.create(fname:"John", lname:"Smith") end - + it "has access to raw body content for PATCH requests" do expect_any_instance_of(Flexirest::Connection).to receive(:patch).with("/partial_update", "MY-BODY-CONTENT", instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", status:200, response_headers:{}))) ProxyClientExample.partial_update(fname:"John", lname:"Smith") end @@ -195,6 +198,15 @@ expect_any_instance_of(Flexirest::Connection).to receive(:get).with("/hal_test/1", instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", status:200, response_headers:{}))) expect_any_instance_of(Flexirest::Connection).to receive(:get).with("/this/is/a/test", instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:"{\"result\":true}", status:200, response_headers:{}))) expect(ProxyClientExample.hal_test(id:1).test.result).to eq(true) end + it "properly passes basic HTTP auth credentials" do + host, credentials, url_path = 'www.example.com', 'user:pass', '/getAll?id=1' + ProxyClientExample.base_url "http://#{credentials}@#{host}" + stub_request(:get, "#{host}#{url_path}") + ProxyClientExample.all(id:1) + expect(a_request(:get, "#{host}#{url_path}").with(headers: { + 'Authorization'=>"Basic #{Base64.strict_encode64(credentials)}" + })).to have_been_made + end end