Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

test that Faraday::Connection instances pass the params and headers

  • Loading branch information...
commit 43a26e59c06be91e5b87fc0ac54061ecf36f9d2d 1 parent d70413a
@technoweenie technoweenie authored
View
11 lib/faraday/adapter/mock_request.rb
@@ -35,16 +35,19 @@ def new_stub(path, request_headers, block)
class Stub < Struct.new(:path, :request_headers, :status, :response_headers, :body)
def matches?(request_path, headers)
return false if request_path != path
- return true if request_headers.empty?
request_headers.each do |key, value|
- return true if headers[key] == value
- end
- false
+ return false if headers[key] != value
+ end
+ true
end
end
def initialize &block
super nil
+ configure(&block) if block
+ end
+
+ def configure
yield stubs
end
View
4 lib/faraday/adapter/net_http.rb
@@ -5,9 +5,9 @@ module NetHttp
extend Faraday::Connection::Options
def _get(uri, request_headers)
- http = Net::HTTP.new(uri.host, uri.port)
+ http = Net::HTTP.new(uri.host, uri.port)
response_class.new do |resp|
- http_resp = http.get(uri.path, request_headers) do |chunk|
+ http_resp = http.get(path_for(uri), request_headers) do |chunk|
resp.process(chunk)
end
http_resp.each_header do |key, value|
View
22 lib/faraday/connection.rb
@@ -83,7 +83,7 @@ def path_prefix=(value)
@path_prefix = value
end
- def build_uri(url, params = {})
+ def build_uri(url, params = nil)
uri = URI.parse(url)
uri.scheme ||= @scheme
uri.host ||= @host
@@ -91,17 +91,29 @@ def build_uri(url, params = {})
if @path_prefix && uri.path !~ /^\//
uri.path = "#{@path_prefix.size > 1 ? @path_prefix : nil}/#{uri.path}"
end
- query = params_to_query(params)
- if !query.empty? then uri.query = query end
+ if params && !params.empty?
+ uri.query = params_to_query(params)
+ end
uri
end
+ def path_for(uri)
+ uri.path.tap do |s|
+ s << "?#{uri.query}" if uri.query
+ s << "##{uri.fragment}" if uri.fragment
+ end
+ end
+
def build_params(existing)
build_hash :params, existing
end
def build_headers(existing)
- build_hash :headers, existing
+ build_hash(:headers, existing).tap do |headers|
+ headers.keys.each do |key|
+ headers[key] = headers.delete(key).to_s
+ end
+ end
end
def build_hash(method, existing)
@@ -117,7 +129,7 @@ def params_to_query(params)
# Some servers convert +'s in URL query params to spaces.
# Go ahead and encode it.
def escape_for_querystring(s)
- URI.encode_component(s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped|
+ URI.encode_component(s.to_s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped|
escaped.gsub! /\+/, "%2B"
end
end
View
10 test/adapter_test.rb
@@ -11,6 +11,16 @@ class AdapterTest < Faraday::TestCase
@connection.extend adapter
end
+ it "passes params" do
+ @connection.params = {:a => 1}
+ assert_equal "params[:a] == 1", @connection.get('params').body
+ end
+
+ it "passes headers" do
+ @connection.headers = {"X-Test" => 1}
+ assert_equal "env[HTTP_X_TEST] == 1", @connection.get('headers').body
+ end
+
it "retrieves the response body" do
assert_equal 'hello world', @connection.get('hello_world').body
end
View
6 test/helper.rb
@@ -8,6 +8,12 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'faraday'
+begin
+ require 'ruby-debug'
+ Debugger.start
+rescue LoadError
+end
+
module Faraday
class TestCase < Test::Unit::TestCase
LIVE_SERVER = 'http://localhost:4567'
View
8 test/live_server.rb
@@ -7,4 +7,12 @@
get '/json' do
"[1,2,3]"
+end
+
+get '/params' do
+ %(params[:a] == #{params[:a]})
+end
+
+get "/headers" do
+ %(env[HTTP_X_TEST] == #{env["HTTP_X_TEST"]})
end
Please sign in to comment.
Something went wrong with that request. Please try again.