faraday 0.9.1 → 2.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +554 -0
- data/LICENSE.md +1 -1
- data/README.md +32 -197
- data/Rakefile +4 -68
- data/examples/client_spec.rb +119 -0
- data/examples/client_test.rb +144 -0
- data/lib/faraday/adapter/test.rb +194 -58
- data/lib/faraday/adapter.rb +76 -20
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +341 -212
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +183 -0
- data/lib/faraday/error.rb +123 -29
- data/lib/faraday/logging/formatter.rb +106 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +18 -25
- data/lib/faraday/middleware_registry.rb +83 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +199 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +69 -0
- data/lib/faraday/options.rb +63 -195
- data/lib/faraday/parameters.rb +4 -180
- data/lib/faraday/rack_builder.rb +99 -59
- data/lib/faraday/request/authorization.rb +37 -30
- data/lib/faraday/request/instrumentation.rb +47 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +48 -24
- data/lib/faraday/request.rb +76 -32
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +22 -48
- data/lib/faraday/response/raise_error.rb +57 -14
- data/lib/faraday/response.rb +32 -35
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +47 -222
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +111 -222
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/test_spec.rb +413 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/connection_spec.rb +793 -0
- data/spec/faraday/error_spec.rb +60 -0
- data/spec/faraday/middleware_registry_spec.rb +31 -0
- data/spec/faraday/middleware_spec.rb +52 -0
- data/spec/faraday/options/env_spec.rb +76 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +150 -0
- data/spec/faraday/rack_builder_spec.rb +317 -0
- data/spec/faraday/request/authorization_spec.rb +83 -0
- data/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +93 -0
- data/spec/faraday/request_spec.rb +110 -0
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/raise_error_spec.rb +172 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +118 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +96 -0
- data/spec/support/shared_examples/adapter.rb +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +263 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- metadata +81 -109
- data/.document +0 -6
- data/CONTRIBUTING.md +0 -36
- data/Gemfile +0 -25
- data/faraday.gemspec +0 -34
- data/lib/faraday/adapter/em_http.rb +0 -237
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -92
- data/lib/faraday/adapter/excon.rb +0 -80
- data/lib/faraday/adapter/httpclient.rb +0 -106
- data/lib/faraday/adapter/net_http.rb +0 -130
- data/lib/faraday/adapter/net_http_persistent.rb +0 -48
- data/lib/faraday/adapter/patron.rb +0 -72
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/adapter/typhoeus.rb +0 -123
- data/lib/faraday/autoload.rb +0 -84
- data/lib/faraday/request/basic_authentication.rb +0 -13
- data/lib/faraday/request/multipart.rb +0 -63
- data/lib/faraday/request/retry.rb +0 -148
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
- data/script/cached-bundle +0 -46
- data/script/console +0 -7
- data/script/generate_certs +0 -42
- data/script/package +0 -7
- data/script/proxy-server +0 -42
- data/script/release +0 -17
- data/script/s3-put +0 -71
- data/script/server +0 -36
- data/script/test +0 -172
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -20
- data/test/adapters/em_synchrony_test.rb +0 -20
- data/test/adapters/excon_test.rb +0 -20
- data/test/adapters/httpclient_test.rb +0 -21
- data/test/adapters/integration.rb +0 -254
- data/test/adapters/logger_test.rb +0 -82
- data/test/adapters/net_http_persistent_test.rb +0 -20
- data/test/adapters/net_http_test.rb +0 -14
- data/test/adapters/patron_test.rb +0 -20
- data/test/adapters/rack_test.rb +0 -31
- data/test/adapters/test_middleware_test.rb +0 -114
- data/test/adapters/typhoeus_test.rb +0 -28
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -111
- data/test/connection_test.rb +0 -522
- data/test/env_test.rb +0 -218
- data/test/helper.rb +0 -81
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -177
- data/test/middleware_stack_test.rb +0 -173
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -252
- data/test/parameters_test.rb +0 -64
- data/test/request_middleware_test.rb +0 -142
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -58
data/script/s3-put
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
# Usage: s3-put <FILE> <S3_BUCKET>[:<PATH>] [<CONTENT_TYPE>]
|
3
|
-
#
|
4
|
-
# Uploads a file to the Amazon S3 service.
|
5
|
-
# Outputs the URL for the newly uploaded file.
|
6
|
-
#
|
7
|
-
# Requirements:
|
8
|
-
# - AMAZON_ACCESS_KEY_ID
|
9
|
-
# - AMAZON_SECRET_ACCESS_KEY
|
10
|
-
# - openssl
|
11
|
-
# - curl
|
12
|
-
#
|
13
|
-
# Author: Mislav Marohnić
|
14
|
-
|
15
|
-
set -e
|
16
|
-
|
17
|
-
authorization() {
|
18
|
-
local signature="$(string_to_sign | hmac_sha1 | base64)"
|
19
|
-
echo "AWS ${AMAZON_ACCESS_KEY_ID?}:${signature}"
|
20
|
-
}
|
21
|
-
|
22
|
-
hmac_sha1() {
|
23
|
-
openssl dgst -binary -sha1 -hmac "${AMAZON_SECRET_ACCESS_KEY?}"
|
24
|
-
}
|
25
|
-
|
26
|
-
base64() {
|
27
|
-
openssl enc -base64
|
28
|
-
}
|
29
|
-
|
30
|
-
bin_md5() {
|
31
|
-
openssl dgst -binary -md5
|
32
|
-
}
|
33
|
-
|
34
|
-
string_to_sign() {
|
35
|
-
echo "$http_method"
|
36
|
-
echo "$content_md5"
|
37
|
-
echo "$content_type"
|
38
|
-
echo "$date"
|
39
|
-
echo "x-amz-acl:$acl"
|
40
|
-
printf "/$bucket/$remote_path"
|
41
|
-
}
|
42
|
-
|
43
|
-
date_string() {
|
44
|
-
LC_TIME=C date "+%a, %d %h %Y %T %z"
|
45
|
-
}
|
46
|
-
|
47
|
-
file="$1"
|
48
|
-
bucket="${2%%:*}"
|
49
|
-
remote_path="${2#*:}"
|
50
|
-
content_type="$3"
|
51
|
-
|
52
|
-
if [ -z "$remote_path" ] || [ "$remote_path" = "$bucket" ]; then
|
53
|
-
remote_path="${file##*/}"
|
54
|
-
fi
|
55
|
-
|
56
|
-
http_method=PUT
|
57
|
-
acl="public-read"
|
58
|
-
content_md5="$(bin_md5 < "$file" | base64)"
|
59
|
-
date="$(date_string)"
|
60
|
-
|
61
|
-
url="https://$bucket.s3.amazonaws.com/$remote_path"
|
62
|
-
|
63
|
-
curl -qsSf -T "$file" \
|
64
|
-
-H "Authorization: $(authorization)" \
|
65
|
-
-H "x-amz-acl: $acl" \
|
66
|
-
-H "Date: $date" \
|
67
|
-
-H "Content-MD5: $content_md5" \
|
68
|
-
-H "Content-Type: $content_type" \
|
69
|
-
"$url"
|
70
|
-
|
71
|
-
echo "$url"
|
data/script/server
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
old_verbose, $VERBOSE = $VERBOSE, nil
|
3
|
-
begin
|
4
|
-
require File.expand_path('../../test/live_server', __FILE__)
|
5
|
-
ensure
|
6
|
-
$VERBOSE = old_verbose
|
7
|
-
end
|
8
|
-
require 'webrick'
|
9
|
-
|
10
|
-
port = 4000
|
11
|
-
if found = ARGV.index('-p')
|
12
|
-
port = ARGV[found + 1].to_i
|
13
|
-
end
|
14
|
-
|
15
|
-
log_io = $stdout
|
16
|
-
log_io.sync = true
|
17
|
-
|
18
|
-
webrick_opts = {
|
19
|
-
:Port => port, :Logger => WEBrick::Log::new(log_io),
|
20
|
-
:AccessLog => [[log_io, "[%{X-Faraday-Adapter}i] %m %U -> %s %b"]]
|
21
|
-
}
|
22
|
-
|
23
|
-
if ENV['SSL_KEY']
|
24
|
-
require 'openssl'
|
25
|
-
require 'webrick/https'
|
26
|
-
webrick_opts.update \
|
27
|
-
:SSLEnable => true,
|
28
|
-
:SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(ENV['SSL_KEY'])),
|
29
|
-
:SSLCertificate => OpenSSL::X509::Certificate.new(File.read(ENV['SSL_FILE'])),
|
30
|
-
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE
|
31
|
-
end
|
32
|
-
|
33
|
-
Rack::Handler::WEBrick.run(Faraday::LiveServer, webrick_opts) do |server|
|
34
|
-
trap(:INT) { server.stop }
|
35
|
-
trap(:TERM) { server.stop }
|
36
|
-
end
|
data/script/test
DELETED
@@ -1,172 +0,0 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
# Usage: script/test [file] [adapter]... -- [test/unit options]
|
3
|
-
# Runs the test suite against a local server spawned automatically in a
|
4
|
-
# thread. After tests are done, the server is shut down.
|
5
|
-
#
|
6
|
-
# If filename arguments are given, only those files are run. If arguments given
|
7
|
-
# are not filenames, they are taken as words that filter the list of files to run.
|
8
|
-
#
|
9
|
-
# Examples:
|
10
|
-
#
|
11
|
-
# $ script/test
|
12
|
-
# $ script/test test/env_test.rb
|
13
|
-
# $ script/test excon typhoeus
|
14
|
-
#
|
15
|
-
# # Run only tests matching /ssl/ for the net_http adapter, with SSL enabled.
|
16
|
-
# $ SSL=yes script/test net_http -- -n /ssl/
|
17
|
-
#
|
18
|
-
# # Run against multiple rbenv versions
|
19
|
-
# $ RBENV_VERSIONS="1.9.3-p194 ree-1.8.7-2012.02" script/test
|
20
|
-
set -e
|
21
|
-
|
22
|
-
if [[ "$RUBYOPT" != *"bundler/setup"* ]]; then
|
23
|
-
export RUBYOPT="-rbundler/setup $RUBYOPT"
|
24
|
-
fi
|
25
|
-
|
26
|
-
port=3999
|
27
|
-
proxy_port=3998
|
28
|
-
scheme=http
|
29
|
-
|
30
|
-
if [ "$SSL" = "yes" ]; then
|
31
|
-
scheme=https
|
32
|
-
if [ -z "$SSL_KEY" ] || [ -z "$SSL_FILE" ]; then
|
33
|
-
eval "$(script/generate_certs -s)"
|
34
|
-
fi
|
35
|
-
fi
|
36
|
-
|
37
|
-
find_test_files() {
|
38
|
-
find "$1" -name '*_test.rb'
|
39
|
-
}
|
40
|
-
|
41
|
-
filter_matching() {
|
42
|
-
pattern="$1"
|
43
|
-
shift
|
44
|
-
for line in "$@"; do
|
45
|
-
[[ $line == *"$pattern"* ]] && echo "$line"
|
46
|
-
done
|
47
|
-
}
|
48
|
-
|
49
|
-
start_server() {
|
50
|
-
mkdir -p log
|
51
|
-
script/server -p $port >log/test.log 2>&1 &
|
52
|
-
echo $!
|
53
|
-
}
|
54
|
-
|
55
|
-
start_proxy() {
|
56
|
-
mkdir -p log
|
57
|
-
script/proxy-server -p $proxy_port -u "faraday@test.local:there is cake" >log/proxy.log 2>&1 &
|
58
|
-
echo $!
|
59
|
-
}
|
60
|
-
|
61
|
-
server_started() {
|
62
|
-
lsof -i :${1?} >/dev/null
|
63
|
-
}
|
64
|
-
|
65
|
-
timestamp() {
|
66
|
-
date +%s
|
67
|
-
}
|
68
|
-
|
69
|
-
wait_for_server() {
|
70
|
-
timeout=$(( `timestamp` + $1 ))
|
71
|
-
while true; do
|
72
|
-
if server_started "$2"; then
|
73
|
-
break
|
74
|
-
elif [ `timestamp` -gt "$timeout" ]; then
|
75
|
-
echo "timed out after $1 seconds" >&2
|
76
|
-
return 1
|
77
|
-
fi
|
78
|
-
done
|
79
|
-
}
|
80
|
-
|
81
|
-
filtered=
|
82
|
-
IFS=$'\n' test_files=($(find_test_files "test"))
|
83
|
-
declare -a explicit_files
|
84
|
-
|
85
|
-
# Process filter arguments:
|
86
|
-
# - test filenames as taken as-is
|
87
|
-
# - other words are taken as pattern to match the list of known files against
|
88
|
-
# - arguments after "--" are forwarded to the ruby process
|
89
|
-
while [ $# -gt 0 ]; do
|
90
|
-
arg="$1"
|
91
|
-
shift
|
92
|
-
if [ "$arg" = "--" ]; then
|
93
|
-
break
|
94
|
-
elif [ -f "$arg" ]; then
|
95
|
-
filtered=true
|
96
|
-
explicit_files[${#explicit_files[@]}+1]="$arg"
|
97
|
-
else
|
98
|
-
filtered=true
|
99
|
-
IFS=$'\n' explicit_files=(
|
100
|
-
${explicit_files[@]}
|
101
|
-
$(filter_matching "$arg" "${test_files[@]}" || true)
|
102
|
-
)
|
103
|
-
fi
|
104
|
-
done
|
105
|
-
|
106
|
-
# If there were filter args, replace test files list with the results
|
107
|
-
if [ -n "$filtered" ]; then
|
108
|
-
if [ ${#explicit_files[@]} -eq 0 ]; then
|
109
|
-
echo "Error: no test files match" >&2
|
110
|
-
exit 1
|
111
|
-
else
|
112
|
-
test_files=(${explicit_files[@]})
|
113
|
-
echo running "${test_files[@]}"
|
114
|
-
fi
|
115
|
-
fi
|
116
|
-
|
117
|
-
# If there are any adapter tests, spin up the HTTP server
|
118
|
-
if [ -n "$(filter_matching "adapters" "${test_files[@]}")" ]; then
|
119
|
-
if server_started $port; then
|
120
|
-
echo "aborted: another instance of server running on $port" >&2
|
121
|
-
exit 1
|
122
|
-
fi
|
123
|
-
server_pid=$(start_server)
|
124
|
-
proxy_pid=$(start_proxy)
|
125
|
-
wait_for_server 30 $port || {
|
126
|
-
cat log/test.log
|
127
|
-
kill "$server_pid"
|
128
|
-
kill "$proxy_pid"
|
129
|
-
exit 1
|
130
|
-
}
|
131
|
-
wait_for_server 5 $proxy_port
|
132
|
-
cleanup() {
|
133
|
-
if [ $? -ne 0 ] && [ -n "$TRAVIS" ]; then
|
134
|
-
cat log/test.log
|
135
|
-
fi
|
136
|
-
kill "$server_pid"
|
137
|
-
kill "$proxy_pid"
|
138
|
-
}
|
139
|
-
trap cleanup INT EXIT
|
140
|
-
export LIVE="${scheme}://localhost:${port}"
|
141
|
-
export LIVE_PROXY="http://faraday%40test.local:there%20is%20cake@localhost:${proxy_port}"
|
142
|
-
fi
|
143
|
-
|
144
|
-
warnings="${TMPDIR:-/tmp}/faraday-warnings.$$"
|
145
|
-
|
146
|
-
run_test_files() {
|
147
|
-
# Save warnings on stderr to a separate file
|
148
|
-
RUBYOPT="$RUBYOPT -w" ruby -e 'while f=ARGV.shift and f!="--"; load f; end' "${test_files[@]}" -- "$@" \
|
149
|
-
2> >(tee >(grep 'warning:' >"$warnings") | grep -v 'warning:')
|
150
|
-
}
|
151
|
-
|
152
|
-
check_warnings() {
|
153
|
-
# Display Ruby warnings from this project's source files. Abort if any were found.
|
154
|
-
num="$(grep -F "$PWD" "$warnings" | grep -v "${PWD}/bundle" | sort | uniq -c | sort -rn | tee /dev/stderr | wc -l)"
|
155
|
-
rm -f "$warnings"
|
156
|
-
if [ "$num" -gt 0 ]; then
|
157
|
-
echo "FAILED: this test suite doesn't tolerate Ruby syntax warnings!" >&2
|
158
|
-
exit 1
|
159
|
-
fi
|
160
|
-
}
|
161
|
-
|
162
|
-
if [ -n "$RBENV_VERSIONS" ]; then
|
163
|
-
IFS=' ' versions=($RBENV_VERSIONS)
|
164
|
-
for version in "${versions[@]}"; do
|
165
|
-
echo "[${version}]"
|
166
|
-
RBENV_VERSION="$version" run_test_files "$@"
|
167
|
-
done
|
168
|
-
else
|
169
|
-
run_test_files "$@"
|
170
|
-
fi
|
171
|
-
|
172
|
-
check_warnings
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require File.expand_path('../integration', __FILE__)
|
2
|
-
|
3
|
-
module Adapters
|
4
|
-
class DefaultTest < Faraday::TestCase
|
5
|
-
|
6
|
-
def adapter() :default end
|
7
|
-
|
8
|
-
Integration.apply(self, :NonParallel) do
|
9
|
-
# default stack is not configured with Multipart
|
10
|
-
undef :test_POST_sends_files
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require File.expand_path('../integration', __FILE__)
|
2
|
-
|
3
|
-
module Adapters
|
4
|
-
class EMHttpTest < Faraday::TestCase
|
5
|
-
|
6
|
-
def adapter() :em_http end
|
7
|
-
|
8
|
-
Integration.apply(self, :Parallel) do
|
9
|
-
# https://github.com/eventmachine/eventmachine/pull/289
|
10
|
-
undef :test_timeout
|
11
|
-
|
12
|
-
def test_binds_local_socket
|
13
|
-
host = '1.2.3.4'
|
14
|
-
conn = create_connection :request => { :bind => { :host => host } }
|
15
|
-
assert_equal host, conn.options[:bind][:host]
|
16
|
-
end
|
17
|
-
end unless jruby? and ssl_mode?
|
18
|
-
# https://github.com/eventmachine/eventmachine/issues/180
|
19
|
-
end
|
20
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require File.expand_path('../integration', __FILE__)
|
2
|
-
|
3
|
-
module Adapters
|
4
|
-
class EMSynchronyTest < Faraday::TestCase
|
5
|
-
|
6
|
-
def adapter() :em_synchrony end
|
7
|
-
|
8
|
-
Integration.apply(self, :Parallel) do
|
9
|
-
# https://github.com/eventmachine/eventmachine/pull/289
|
10
|
-
undef :test_timeout
|
11
|
-
|
12
|
-
def test_binds_local_socket
|
13
|
-
host = '1.2.3.4'
|
14
|
-
conn = create_connection :request => { :bind => { :host => host } }
|
15
|
-
#put conn.get('/who-am-i').body
|
16
|
-
assert_equal host, conn.options[:bind][:host]
|
17
|
-
end
|
18
|
-
end unless RUBY_VERSION < '1.9' or jruby?
|
19
|
-
end
|
20
|
-
end
|
data/test/adapters/excon_test.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require File.expand_path('../integration', __FILE__)
|
2
|
-
|
3
|
-
module Adapters
|
4
|
-
class ExconTest < Faraday::TestCase
|
5
|
-
|
6
|
-
def adapter() :excon end
|
7
|
-
|
8
|
-
Integration.apply(self, :NonParallel) do
|
9
|
-
# https://github.com/geemus/excon/issues/126 ?
|
10
|
-
undef :test_timeout if ssl_mode?
|
11
|
-
|
12
|
-
# Excon lets OpenSSL::SSL::SSLError be raised without any way to
|
13
|
-
# distinguish whether it happened because of a 407 proxy response
|
14
|
-
undef :test_proxy_auth_fail if ssl_mode?
|
15
|
-
|
16
|
-
# https://github.com/geemus/excon/issues/358
|
17
|
-
undef :test_connection_error if RUBY_VERSION >= '2.1.0'
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require File.expand_path('../integration', __FILE__)
|
2
|
-
|
3
|
-
module Adapters
|
4
|
-
class HttpclientTest < Faraday::TestCase
|
5
|
-
|
6
|
-
def adapter() :httpclient end
|
7
|
-
|
8
|
-
Integration.apply(self, :NonParallel) do
|
9
|
-
def setup
|
10
|
-
require 'httpclient' unless defined?(HTTPClient)
|
11
|
-
HTTPClient::NO_PROXY_HOSTS.delete('localhost')
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_binds_local_socket
|
15
|
-
host = '1.2.3.4'
|
16
|
-
conn = create_connection :request => { :bind => { :host => host } }
|
17
|
-
assert_equal host, conn.options[:bind][:host]
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,254 +0,0 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
require File.expand_path("../../helper", __FILE__)
|
3
|
-
Faraday.require_lib 'autoload'
|
4
|
-
|
5
|
-
module Adapters
|
6
|
-
# Adapter integration tests. To use, implement two methods:
|
7
|
-
#
|
8
|
-
# `#adapter` required. returns a symbol for the adapter middleware name
|
9
|
-
# `#adapter_options` optional. extra arguments for building an adapter
|
10
|
-
module Integration
|
11
|
-
def self.apply(base, *extra_features)
|
12
|
-
if base.live_server?
|
13
|
-
features = [:Common]
|
14
|
-
features.concat extra_features
|
15
|
-
features << :SSL if base.ssl_mode?
|
16
|
-
features.each {|name| base.send(:include, self.const_get(name)) }
|
17
|
-
yield if block_given?
|
18
|
-
elsif !defined? @warned
|
19
|
-
warn "Warning: Not running integration tests against a live server."
|
20
|
-
warn "Start the server `ruby test/live_server.rb` and set the LIVE=1 env variable."
|
21
|
-
@warned = true
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
module Parallel
|
26
|
-
def test_in_parallel
|
27
|
-
resp1, resp2 = nil, nil
|
28
|
-
|
29
|
-
connection = create_connection
|
30
|
-
connection.in_parallel do
|
31
|
-
resp1 = connection.get('echo?a=1')
|
32
|
-
resp2 = connection.get('echo?b=2')
|
33
|
-
assert connection.in_parallel?
|
34
|
-
assert_nil resp1.body
|
35
|
-
assert_nil resp2.body
|
36
|
-
end
|
37
|
-
assert !connection.in_parallel?
|
38
|
-
assert_equal 'get ?{"a"=>"1"}', resp1.body
|
39
|
-
assert_equal 'get ?{"b"=>"2"}', resp2.body
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
module NonParallel
|
44
|
-
def test_no_parallel_support
|
45
|
-
connection = create_connection
|
46
|
-
response = nil
|
47
|
-
|
48
|
-
err = capture_warnings do
|
49
|
-
connection.in_parallel do
|
50
|
-
response = connection.get('echo').body
|
51
|
-
end
|
52
|
-
end
|
53
|
-
assert response
|
54
|
-
assert_match "no parallel-capable adapter on Faraday stack", err
|
55
|
-
assert_match __FILE__, err
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
module Compression
|
60
|
-
def test_GET_handles_compression
|
61
|
-
res = get('echo_header', :name => 'accept-encoding')
|
62
|
-
assert_match(/gzip;.+\bdeflate\b/, res.body)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
module SSL
|
67
|
-
def test_GET_ssl_fails_with_bad_cert
|
68
|
-
ca_file = 'tmp/faraday-different-ca-cert.crt'
|
69
|
-
conn = create_connection(:ssl => {:ca_file => ca_file})
|
70
|
-
err = assert_raises Faraday::SSLError do
|
71
|
-
conn.get('/ssl')
|
72
|
-
end
|
73
|
-
assert_includes err.message, "certificate"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
module Common
|
78
|
-
extend Forwardable
|
79
|
-
def_delegators :create_connection, :get, :head, :put, :post, :patch, :delete, :run_request
|
80
|
-
|
81
|
-
def test_GET_retrieves_the_response_body
|
82
|
-
assert_equal 'get', get('echo').body
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_GET_send_url_encoded_params
|
86
|
-
assert_equal %(get ?{"name"=>"zack"}), get('echo', :name => 'zack').body
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_GET_retrieves_the_response_headers
|
90
|
-
response = get('echo')
|
91
|
-
assert_match(/text\/plain/, response.headers['Content-Type'], 'original case fail')
|
92
|
-
assert_match(/text\/plain/, response.headers['content-type'], 'lowercase fail')
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_GET_handles_headers_with_multiple_values
|
96
|
-
assert_equal 'one, two', get('multi').headers['set-cookie']
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_GET_with_body
|
100
|
-
response = get('echo') do |req|
|
101
|
-
req.body = {'bodyrock' => true}
|
102
|
-
end
|
103
|
-
assert_equal %(get {"bodyrock"=>"true"}), response.body
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_GET_sends_user_agent
|
107
|
-
response = get('echo_header', {:name => 'user-agent'}, :user_agent => 'Agent Faraday')
|
108
|
-
assert_equal 'Agent Faraday', response.body
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_GET_ssl
|
112
|
-
expected = self.class.ssl_mode?.to_s
|
113
|
-
assert_equal expected, get('ssl').body
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_POST_send_url_encoded_params
|
117
|
-
assert_equal %(post {"name"=>"zack"}), post('echo', :name => 'zack').body
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_POST_send_url_encoded_nested_params
|
121
|
-
resp = post('echo', 'name' => {'first' => 'zack'})
|
122
|
-
assert_equal %(post {"name"=>{"first"=>"zack"}}), resp.body
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_POST_retrieves_the_response_headers
|
126
|
-
assert_match(/text\/plain/, post('echo').headers['content-type'])
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_POST_sends_files
|
130
|
-
resp = post('file') do |req|
|
131
|
-
req.body = {'uploaded_file' => Faraday::UploadIO.new(__FILE__, 'text/x-ruby')}
|
132
|
-
end
|
133
|
-
assert_equal "file integration.rb text/x-ruby #{File.size(__FILE__)}", resp.body
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_PUT_send_url_encoded_params
|
137
|
-
assert_equal %(put {"name"=>"zack"}), put('echo', :name => 'zack').body
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_PUT_send_url_encoded_nested_params
|
141
|
-
resp = put('echo', 'name' => {'first' => 'zack'})
|
142
|
-
assert_equal %(put {"name"=>{"first"=>"zack"}}), resp.body
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_PUT_retrieves_the_response_headers
|
146
|
-
assert_match(/text\/plain/, put('echo').headers['content-type'])
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_PATCH_send_url_encoded_params
|
150
|
-
assert_equal %(patch {"name"=>"zack"}), patch('echo', :name => 'zack').body
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_OPTIONS
|
154
|
-
resp = run_request(:options, 'echo', nil, {})
|
155
|
-
assert_equal 'options', resp.body
|
156
|
-
end
|
157
|
-
|
158
|
-
def test_HEAD_retrieves_no_response_body
|
159
|
-
assert_equal '', head('echo').body
|
160
|
-
end
|
161
|
-
|
162
|
-
def test_HEAD_retrieves_the_response_headers
|
163
|
-
assert_match(/text\/plain/, head('echo').headers['content-type'])
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_DELETE_retrieves_the_response_headers
|
167
|
-
assert_match(/text\/plain/, delete('echo').headers['content-type'])
|
168
|
-
end
|
169
|
-
|
170
|
-
def test_DELETE_retrieves_the_body
|
171
|
-
assert_equal %(delete), delete('echo').body
|
172
|
-
end
|
173
|
-
|
174
|
-
def test_timeout
|
175
|
-
conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
|
176
|
-
assert_raises Faraday::Error::TimeoutError do
|
177
|
-
conn.get '/slow'
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
def test_connection_error
|
182
|
-
assert_raises Faraday::Error::ConnectionFailed do
|
183
|
-
get 'http://localhost:4'
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_proxy
|
188
|
-
proxy_uri = URI(ENV['LIVE_PROXY'])
|
189
|
-
conn = create_connection(:proxy => proxy_uri)
|
190
|
-
|
191
|
-
res = conn.get '/echo'
|
192
|
-
assert_equal 'get', res.body
|
193
|
-
|
194
|
-
unless self.class.ssl_mode?
|
195
|
-
# proxy can't append "Via" header for HTTPS responses
|
196
|
-
assert_match(/:#{proxy_uri.port}$/, res['via'])
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
def test_proxy_auth_fail
|
201
|
-
proxy_uri = URI(ENV['LIVE_PROXY'])
|
202
|
-
proxy_uri.password = 'WRONG'
|
203
|
-
conn = create_connection(:proxy => proxy_uri)
|
204
|
-
|
205
|
-
err = assert_raises Faraday::Error::ConnectionFailed do
|
206
|
-
conn.get '/echo'
|
207
|
-
end
|
208
|
-
|
209
|
-
unless self.class.ssl_mode? && self.class.jruby?
|
210
|
-
# JRuby raises "End of file reached" which cannot be distinguished from a 407
|
211
|
-
assert_equal %{407 "Proxy Authentication Required "}, err.message
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
def test_empty_body_response_represented_as_blank_string
|
216
|
-
response = get('204')
|
217
|
-
assert_equal '', response.body
|
218
|
-
end
|
219
|
-
|
220
|
-
def adapter
|
221
|
-
raise NotImplementedError.new("Need to override #adapter")
|
222
|
-
end
|
223
|
-
|
224
|
-
# extra options to pass when building the adapter
|
225
|
-
def adapter_options
|
226
|
-
[]
|
227
|
-
end
|
228
|
-
|
229
|
-
def create_connection(options = {})
|
230
|
-
if adapter == :default
|
231
|
-
builder_block = nil
|
232
|
-
else
|
233
|
-
builder_block = Proc.new do |b|
|
234
|
-
b.request :multipart
|
235
|
-
b.request :url_encoded
|
236
|
-
b.adapter adapter, *adapter_options
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
server = self.class.live_server
|
241
|
-
url = '%s://%s:%d' % [server.scheme, server.host, server.port]
|
242
|
-
|
243
|
-
options[:ssl] ||= {}
|
244
|
-
options[:ssl][:ca_file] ||= ENV['SSL_FILE']
|
245
|
-
|
246
|
-
Faraday::Connection.new(url, options, &builder_block).tap do |conn|
|
247
|
-
conn.headers['X-Faraday-Adapter'] = adapter.to_s
|
248
|
-
adapter_handler = conn.builder.handlers.last
|
249
|
-
conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
254
|
-
end
|