Skip to content

Commit

Permalink
Fix faraday adapter so that it properly normalizes query parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Mar 6, 2011
1 parent 534925a commit 0d4d9f5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

[Full Changelog](http://github.com/myronmarston/vcr/compare/v1.7.0...master)

* Fix Faraday adapter so that it properly normalizes query parameters
in the same way that Faraday itself does.

## 1.7.0 (March 1, 2011)

[Full Changelog](http://github.com/myronmarston/vcr/compare/v1.6.0...v1.7.0)
Expand Down
5 changes: 5 additions & 0 deletions lib/vcr/http_stubbing_adapters/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def restore_stubs_checkpoint(cassette)
raise ArgumentError.new("No checkpoint for #{cassette.inspect} could be found")
end

def normalize_uri(uri)
# the adapters can implement this if they want
uri
end

private

def compare_version
Expand Down
4 changes: 4 additions & 0 deletions lib/vcr/http_stubbing_adapters/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def reset!
end
end

def normalize_uri(uri)
super.gsub('+', '%20')
end

private

def version
Expand Down
2 changes: 1 addition & 1 deletion lib/vcr/structs/normalizers/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def normalize_uri
# urls have always included it). We force it to be included
# here by redefining default_port so that URI#to_s will include it.
def u.default_port; nil; end
self.uri = u.to_s
self.uri = VCR.http_stubbing_adapter.normalize_uri(u.to_s)
end
end
end
Expand Down
40 changes: 34 additions & 6 deletions spec/support/shared_example_groups/http_library.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'cgi'

NET_CONNECT_NOT_ALLOWED_ERROR = /You can use VCR to automatically record this request and replay it later/

shared_examples_for "an http library" do |library, supported_request_match_attributes, *other|
Expand All @@ -12,24 +14,50 @@
# so this gives us another alias we can use for the original method.
alias make_request make_http_request

describe '.stub_requests' do
describe 'making an HTTP request' do
let(:status) { VCR::ResponseStatus.new(200, 'OK') }
let(:interaction) { VCR::HTTPInteraction.new(request, response) }
let(:response_body) { "The response body" }
let(:match_requests_on) { [:method, :uri] }
let(:record_mode) { :none }

def perform_stubbing
subject.stub_requests([interaction], [:method, :uri])
before(:each) do
subject.stub_requests([interaction], match_requests_on)
end

context "when the request and response has no headers" do
context "when the the stubbed request and response has no headers" do
let(:request) { VCR::Request.new(:get, 'http://example.com:80/') }
let(:response) { VCR::Response.new(status, nil, response_body, '1.1') }

it 'returns the response when a matching request is made' do
perform_stubbing
it 'returns the response for a matching request' do
get_body_string(make_http_request(:get, 'http://example.com/')).should == response_body
end
end

def self.test_url(description, url)
context "when a URL #{description} has been stubbed" do
let(:request) { VCR::Request.new(:get, url) }
let(:response) { VCR::Response.new(status, nil, response_body, '1.1') }

def should_be_pending?
return false unless described_class == VCR::HttpStubbingAdapters::WebMock
return false unless request.uri.include?(CGI.escape('&'))
self.class.included_modules.first.http_library_name == 'EM HTTP Request'
end

it 'returns the expected response for the same request' do
pending "WebMock/EM-HTTP bug", :if => should_be_pending? do
get_body_string(make_http_request(:get, url)).should == response_body
end
end
end
end

test_url "that has query params", "http://example.com/search?q=param"
test_url "with spaces encoded as +", "http://example.com/search?q=a+b"
test_url "with spaces encoded as %20", "http://example.com/search?q=a%20b"
test_url "with an encoded ampersand", "http://example.com:80/search?q=#{CGI.escape("Q&A")}"
test_url "with a complex escaped query param", "http://example.com:80/search?q=#{CGI.escape("A&(! 234k !@ kasdj232\#$ kjw35")}"
end

describe '.stub_requests using specific match_attributes' do
Expand Down

0 comments on commit 0d4d9f5

Please sign in to comment.