1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
require File.expand_path('../helper', __FILE__)
class EnvTest < Faraday::TestCase
def setup
@conn = Faraday.new :url => 'http://sushi.com/api',
:headers => {'Mime-Version' => '1.0'},
:request => {:oauth => {:consumer_key => 'anonymous'}}
@conn.options.timeout = 3
@conn.options.open_timeout = 5
@conn.ssl.verify = false
@conn.proxy 'http://proxy.com'
end
def test_request_create_stores_method
env = make_env(:get)
assert_equal :get, env.method
end
def test_request_create_stores_uri
env = make_env do |req|
req.url 'foo.json', 'a' => 1
end
assert_equal 'http://sushi.com/api/foo.json?a=1', env.url.to_s
end
def test_request_create_stores_headers
env = make_env do |req|
req['Server'] = 'Faraday'
end
headers = env.request_headers
assert_equal '1.0', headers['mime-version']
assert_equal 'Faraday', headers['server']
end
def test_request_create_stores_body
env = make_env do |req|
req.body = 'hi'
end
assert_equal 'hi', env.body
end
def test_global_request_options
env = make_env
assert_equal 3, env.request.timeout
assert_equal 5, env.request.open_timeout
end
def test_per_request_options
env = make_env do |req|
req.options.timeout = 10
req.options.boundary = 'boo'
req.options.oauth[:consumer_secret] = 'xyz'
end
assert_equal 10, env.request.timeout
assert_equal 5, env.request.open_timeout
assert_equal 'boo', env.request.boundary
oauth_expected = {:consumer_secret => 'xyz', :consumer_key => 'anonymous'}
assert_equal oauth_expected, env.request.oauth
end
def test_request_create_stores_ssl_options
env = make_env
assert_equal false, env.ssl.verify
end
def test_request_create_stores_proxy_options
env = make_env
assert_equal 'proxy.com', env.request.proxy.host
end
private
def make_env(method = :get, connection = @conn, &block)
request = connection.build_request(method, &block)
request.to_env(connection)
end
end
class HeadersTest < Faraday::TestCase
def setup
@headers = Faraday::Utils::Headers.new
end
def test_normalizes_different_capitalizations
@headers['Content-Type'] = 'application/json'
assert_equal ['Content-Type'], @headers.keys
assert_equal 'application/json', @headers['Content-Type']
assert_equal 'application/json', @headers['CONTENT-TYPE']
assert_equal 'application/json', @headers['content-type']
assert @headers.include?('content-type')
@headers['content-type'] = 'application/xml'
assert_equal ['Content-Type'], @headers.keys
assert_equal 'application/xml', @headers['Content-Type']
assert_equal 'application/xml', @headers['CONTENT-TYPE']
assert_equal 'application/xml', @headers['content-type']
end
def test_fetch_key
@headers['Content-Type'] = 'application/json'
block_called = false
assert_equal 'application/json', @headers.fetch('content-type') { block_called = true }
assert_equal 'application/json', @headers.fetch('Content-Type')
assert_equal 'application/json', @headers.fetch('CONTENT-TYPE')
assert_equal 'application/json', @headers.fetch(:content_type)
assert_equal false, block_called
assert_equal 'default', @headers.fetch('invalid', 'default')
assert_equal false, @headers.fetch('invalid', false)
assert_nil @headers.fetch('invalid', nil)
assert_equal 'Invalid key', @headers.fetch('Invalid') { |key| "#{key} key" }
expected_error = defined?(KeyError) ? KeyError : IndexError
assert_raises(expected_error) { @headers.fetch('invalid') }
end
def test_delete_key
@headers['Content-Type'] = 'application/json'
assert_equal 1, @headers.size
assert @headers.include?('content-type')
assert_equal 'application/json', @headers.delete('content-type')
assert_equal 0, @headers.size
assert !@headers.include?('content-type')
assert_equal nil, @headers.delete('content-type')
end
def test_parse_response_headers_leaves_http_status_line_out
@headers.parse("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
assert_equal %w(Content-Type), @headers.keys
end
def test_parse_response_headers_parses_lower_cased_header_name_and_value
@headers.parse("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")
assert_equal 'text/html', @headers['content-type']
end
def test_parse_response_headers_parses_lower_cased_header_name_and_value_with_colon
@headers.parse("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://sushi.com/\r\n\r\n")
assert_equal 'http://sushi.com/', @headers['location']
end
def test_parse_response_headers_parses_blank_lines
@headers.parse("HTTP/1.1 200 OK\r\n\r\nContent-Type: text/html\r\n\r\n")
assert_equal 'text/html', @headers['content-type']
end
end
class ResponseTest < Faraday::TestCase
def setup
@env = Faraday::Env.from \
:status => 404, :body => 'yikes',
:response_headers => {'Content-Type' => 'text/plain'}
@response = Faraday::Response.new @env
end
def test_finished
assert @response.finished?
end
def test_error_on_finish
assert_raises RuntimeError do
@response.finish({})
end
end
def test_not_success
assert !@response.success?
end
def test_status
assert_equal 404, @response.status
end
def test_body
assert_equal 'yikes', @response.body
end
def test_headers
assert_equal 'text/plain', @response.headers['Content-Type']
assert_equal 'text/plain', @response['content-type']
end
def test_apply_request
@response.apply_request :body => 'a=b', :method => :post
assert_equal 'yikes', @response.body
assert_equal :post, @response.env[:method]
end
def test_marshal
@response = Faraday::Response.new
@response.on_complete { }
@response.finish @env.merge(:params => 'moo')
loaded = Marshal.load Marshal.dump(@response)
assert_nil loaded.env[:params]
assert_equal %w[body response_headers status], loaded.env.keys.map { |k| k.to_s }.sort
end
def test_hash
hash = @response.to_hash
assert_kind_of Hash, hash
assert_equal @env.to_hash, hash
assert_equal hash[:status], @response.status
assert_equal hash[:response_headers], @response.headers
assert_equal hash[:body], @response.body
end
end
|