Module: Adapters::Integration::Common

Extended by:
Forwardable
Included in:
RackTest
Defined in:
test/adapters/integration.rb

Instance Method Summary collapse

Instance Method Details

#adapterObject

Raises:

  • (NotImplementedError)


220
221
222
# File 'test/adapters/integration.rb', line 220

def adapter
  raise NotImplementedError.new("Need to override #adapter")
end

#adapter_optionsObject

extra options to pass when building the adapter



225
226
227
# File 'test/adapters/integration.rb', line 225

def adapter_options
  []
end

#create_connection(options = {}) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'test/adapters/integration.rb', line 229

def create_connection(options = {})
  if adapter == :default
    builder_block = nil
  else
    builder_block = Proc.new do |b|
      b.request :multipart
      b.request :url_encoded
      b.adapter adapter, *adapter_options
    end
  end

  server = self.class.live_server
  url = '%s://%s:%d' % [server.scheme, server.host, server.port]

  options[:ssl] ||= {}
  options[:ssl][:ca_file] ||= ENV['SSL_FILE']

  Faraday::Connection.new(url, options, &builder_block).tap do |conn|
    conn.headers['X-Faraday-Adapter'] = adapter.to_s
    adapter_handler = conn.builder.handlers.last
    conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
  end
end

#test_connection_errorObject



181
182
183
184
185
# File 'test/adapters/integration.rb', line 181

def test_connection_error
  assert_raises Faraday::Error::ConnectionFailed do
    get 'http://localhost:4'
  end
end

#test_DELETE_retrieves_the_bodyObject



170
171
172
# File 'test/adapters/integration.rb', line 170

def test_DELETE_retrieves_the_body
  assert_equal %(delete), delete('echo').body
end

#test_DELETE_retrieves_the_response_headersObject



166
167
168
# File 'test/adapters/integration.rb', line 166

def test_DELETE_retrieves_the_response_headers
  assert_match(/text\/plain/, delete('echo').headers['content-type'])
end

#test_empty_body_response_represented_as_blank_stringObject



215
216
217
218
# File 'test/adapters/integration.rb', line 215

def test_empty_body_response_represented_as_blank_string
  response = get('204')
  assert_equal '', response.body
end

#test_GET_handles_headers_with_multiple_valuesObject



95
96
97
# File 'test/adapters/integration.rb', line 95

def test_GET_handles_headers_with_multiple_values
  assert_equal 'one, two', get('multi').headers['set-cookie']
end

#test_GET_retrieves_the_response_bodyObject



81
82
83
# File 'test/adapters/integration.rb', line 81

def test_GET_retrieves_the_response_body
  assert_equal 'get', get('echo').body
end

#test_GET_retrieves_the_response_headersObject



89
90
91
92
93
# File 'test/adapters/integration.rb', line 89

def test_GET_retrieves_the_response_headers
  response = get('echo')
  assert_match(/text\/plain/, response.headers['Content-Type'], 'original case fail')
  assert_match(/text\/plain/, response.headers['content-type'], 'lowercase fail')
end

#test_GET_send_url_encoded_paramsObject



85
86
87
# File 'test/adapters/integration.rb', line 85

def test_GET_send_url_encoded_params
  assert_equal %(get ?{"name"=>"zack"}), get('echo', :name => 'zack').body
end

#test_GET_sends_user_agentObject



106
107
108
109
# File 'test/adapters/integration.rb', line 106

def test_GET_sends_user_agent
  response = get('echo_header', {:name => 'user-agent'}, :user_agent => 'Agent Faraday')
  assert_equal 'Agent Faraday', response.body
end

#test_GET_sslObject



111
112
113
114
# File 'test/adapters/integration.rb', line 111

def test_GET_ssl
  expected = self.class.ssl_mode?.to_s
  assert_equal expected, get('ssl').body
end

#test_GET_with_bodyObject



99
100
101
102
103
104
# File 'test/adapters/integration.rb', line 99

def test_GET_with_body
  response = get('echo') do |req|
    req.body = {'bodyrock' => true}
  end
  assert_equal %(get {"bodyrock"=>"true"}), response.body
end

#test_HEAD_retrieves_no_response_bodyObject



158
159
160
# File 'test/adapters/integration.rb', line 158

def test_HEAD_retrieves_no_response_body
  assert_equal '', head('echo').body
end

#test_HEAD_retrieves_the_response_headersObject



162
163
164
# File 'test/adapters/integration.rb', line 162

def test_HEAD_retrieves_the_response_headers
  assert_match(/text\/plain/, head('echo').headers['content-type'])
end

#test_OPTIONSObject



153
154
155
156
# File 'test/adapters/integration.rb', line 153

def test_OPTIONS
  resp = run_request(:options, 'echo', nil, {})
  assert_equal 'options', resp.body
end

#test_PATCH_send_url_encoded_paramsObject



149
150
151
# File 'test/adapters/integration.rb', line 149

def test_PATCH_send_url_encoded_params
  assert_equal %(patch {"name"=>"zack"}), patch('echo', :name => 'zack').body
end

#test_POST_retrieves_the_response_headersObject



125
126
127
# File 'test/adapters/integration.rb', line 125

def test_POST_retrieves_the_response_headers
  assert_match(/text\/plain/, post('echo').headers['content-type'])
end

#test_POST_send_url_encoded_nested_paramsObject



120
121
122
123
# File 'test/adapters/integration.rb', line 120

def test_POST_send_url_encoded_nested_params
  resp = post('echo', 'name' => {'first' => 'zack'})
  assert_equal %(post {"name"=>{"first"=>"zack"}}), resp.body
end

#test_POST_send_url_encoded_paramsObject



116
117
118
# File 'test/adapters/integration.rb', line 116

def test_POST_send_url_encoded_params
  assert_equal %(post {"name"=>"zack"}), post('echo', :name => 'zack').body
end

#test_POST_sends_filesObject



129
130
131
132
133
134
# File 'test/adapters/integration.rb', line 129

def test_POST_sends_files
  resp = post('file') do |req|
    req.body = {'uploaded_file' => Faraday::UploadIO.new(__FILE__, 'text/x-ruby')}
  end
  assert_equal "file integration.rb text/x-ruby #{File.size(__FILE__)}", resp.body
end

#test_proxyObject



187
188
189
190
191
192
193
194
195
196
197
198
# File 'test/adapters/integration.rb', line 187

def test_proxy
  proxy_uri = URI(ENV['LIVE_PROXY'])
  conn = create_connection(:proxy => proxy_uri)

  res = conn.get '/echo'
  assert_equal 'get', res.body

  unless self.class.ssl_mode?
    # proxy can't append "Via" header for HTTPS responses
    assert_match(/:#{proxy_uri.port}$/, res['via'])
  end
end

#test_proxy_auth_failObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'test/adapters/integration.rb', line 200

def test_proxy_auth_fail
  proxy_uri = URI(ENV['LIVE_PROXY'])
  proxy_uri.password = 'WRONG'
  conn = create_connection(:proxy => proxy_uri)

  err = assert_raises Faraday::Error::ConnectionFailed do
    conn.get '/echo'
  end

  unless self.class.ssl_mode? && self.class.jruby?
    # JRuby raises "End of file reached" which cannot be distinguished from a 407
    assert_equal %{407 "Proxy Authentication Required "}, err.message
  end
end

#test_PUT_retrieves_the_response_headersObject



145
146
147
# File 'test/adapters/integration.rb', line 145

def test_PUT_retrieves_the_response_headers
  assert_match(/text\/plain/, put('echo').headers['content-type'])
end

#test_PUT_send_url_encoded_nested_paramsObject



140
141
142
143
# File 'test/adapters/integration.rb', line 140

def test_PUT_send_url_encoded_nested_params
  resp = put('echo', 'name' => {'first' => 'zack'})
  assert_equal %(put {"name"=>{"first"=>"zack"}}), resp.body
end

#test_PUT_send_url_encoded_paramsObject



136
137
138
# File 'test/adapters/integration.rb', line 136

def test_PUT_send_url_encoded_params
  assert_equal %(put {"name"=>"zack"}), put('echo', :name => 'zack').body
end

#test_timeoutObject



174
175
176
177
178
179
# File 'test/adapters/integration.rb', line 174

def test_timeout
  conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
  assert_raises Faraday::Error::TimeoutError do
    conn.get '/slow'
  end
end