wreq 1.2.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +41 -63
- data/Cargo.toml +1 -1
- data/Gemfile +1 -0
- data/README.md +7 -4
- data/build.rs +7 -0
- data/docs/windows-gnu-tokio-crash.md +70 -0
- data/lib/wreq_ruby/emulate.rb +1 -0
- data/script/build_windows_gnu.ps1 +257 -0
- data/src/arch.rs +33 -0
- data/src/client/body/stream.rs +12 -9
- data/src/client/req.rs +122 -110
- data/src/client/resp.rs +9 -17
- data/src/client.rs +13 -0
- data/src/emulate.rs +1 -0
- data/src/lib.rs +1 -0
- data/src/rt.rs +26 -9
- data/test/client_cookie_test.rb +6 -6
- data/test/client_test.rb +16 -16
- data/test/cookie_test.rb +8 -6
- data/test/error_handling_test.rb +4 -4
- data/test/header_test.rb +1 -1
- data/test/inspect_test.rb +4 -4
- data/test/module_methods_test.rb +11 -11
- data/test/request_parameters_test.rb +30 -30
- data/test/request_test.rb +22 -22
- data/test/response_test.rb +6 -6
- data/test/stream_test.rb +61 -35
- data/test/test_helper.rb +46 -3
- metadata +4 -1
data/src/rt.rs
CHANGED
|
@@ -5,25 +5,42 @@ use tokio::runtime::{Builder, Runtime};
|
|
|
5
5
|
use crate::{error::interrupt_error, gvl};
|
|
6
6
|
|
|
7
7
|
static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
|
|
8
|
-
Builder::new_multi_thread()
|
|
8
|
+
let mut builder = Builder::new_multi_thread();
|
|
9
|
+
|
|
10
|
+
builder
|
|
9
11
|
.enable_all()
|
|
10
12
|
.build()
|
|
11
13
|
.expect("Failed to initialize Tokio runtime")
|
|
12
14
|
});
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
enum BlockOnError<E> {
|
|
17
|
+
Interrupted,
|
|
18
|
+
Future(E),
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// Block on a future to completion on the global Tokio runtime.
|
|
22
|
+
///
|
|
23
|
+
/// The future runs without Ruby's GVL, so it must not construct Ruby objects or
|
|
24
|
+
/// Ruby exceptions. Convert Rust errors back into Ruby errors after the GVL has
|
|
25
|
+
/// been reacquired.
|
|
26
|
+
pub fn try_block_on<F, T, E, M>(future: F, map_err: M) -> Result<T, magnus::Error>
|
|
17
27
|
where
|
|
18
|
-
F: Future<Output = Result<T,
|
|
28
|
+
F: Future<Output = Result<T, E>>,
|
|
29
|
+
M: FnOnce(E) -> magnus::Error,
|
|
19
30
|
{
|
|
20
|
-
gvl::nogvl_cancellable(|flag| {
|
|
31
|
+
let result = gvl::nogvl_cancellable(|flag| {
|
|
21
32
|
RUNTIME.block_on(async move {
|
|
22
33
|
tokio::select! {
|
|
23
34
|
biased;
|
|
24
|
-
_ = flag.cancelled() => Err(
|
|
25
|
-
result = future => result,
|
|
35
|
+
_ = flag.cancelled() => Err(BlockOnError::Interrupted),
|
|
36
|
+
result = future => result.map_err(BlockOnError::Future),
|
|
26
37
|
}
|
|
27
38
|
})
|
|
28
|
-
})
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
match result {
|
|
42
|
+
Ok(value) => Ok(value),
|
|
43
|
+
Err(BlockOnError::Interrupted) => Err(interrupt_error()),
|
|
44
|
+
Err(BlockOnError::Future(err)) => Err(map_err(err)),
|
|
45
|
+
}
|
|
29
46
|
}
|
data/test/client_cookie_test.rb
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require "test_helper"
|
|
4
4
|
|
|
5
5
|
class ClientCookieProviderTest < Minitest::Test
|
|
6
|
-
HOST =
|
|
6
|
+
HOST = HTTPBIN_URL
|
|
7
7
|
|
|
8
8
|
def setup
|
|
9
9
|
@jar = Wreq::Jar.new
|
|
@@ -29,9 +29,9 @@ class ClientCookieProviderTest < Minitest::Test
|
|
|
29
29
|
# subsequent request should send the stored cookie automatically
|
|
30
30
|
res2 = @client.get("#{HOST}/cookies")
|
|
31
31
|
assert_equal 200, res2.code
|
|
32
|
-
|
|
33
|
-
assert_kind_of Hash,
|
|
34
|
-
assert_equal "bar",
|
|
32
|
+
cookies = httpbin_cookies(res2.json)
|
|
33
|
+
assert_kind_of Hash, cookies
|
|
34
|
+
assert_equal "bar", httpbin_fetch(cookies, "foo")
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def test_prepopulated_jar_is_used_by_client
|
|
@@ -40,7 +40,7 @@ class ClientCookieProviderTest < Minitest::Test
|
|
|
40
40
|
|
|
41
41
|
res = @client.get("#{HOST}/cookies")
|
|
42
42
|
assert_equal 200, res.code
|
|
43
|
-
cookies = res.json
|
|
44
|
-
assert_equal "1", cookies
|
|
43
|
+
cookies = httpbin_cookies(res.json)
|
|
44
|
+
assert_equal "1", httpbin_fetch(cookies, "pref")
|
|
45
45
|
end
|
|
46
46
|
end
|
data/test/client_test.rb
CHANGED
|
@@ -20,40 +20,40 @@ class ClientTest < Minitest::Test
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def test_get_request
|
|
23
|
-
response = @client.get("
|
|
23
|
+
response = @client.get("#{HTTPBIN_URL}/get")
|
|
24
24
|
refute_nil response
|
|
25
25
|
assert_equal 200, response.code
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def test_post_request
|
|
29
|
-
response = @client.post("
|
|
29
|
+
response = @client.post("#{HTTPBIN_URL}/post",
|
|
30
30
|
json: {test: "wreq-ruby"})
|
|
31
31
|
refute_nil response
|
|
32
32
|
assert_equal 200, response.code
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def test_put_request
|
|
36
|
-
response = @client.put("
|
|
36
|
+
response = @client.put("#{HTTPBIN_URL}/put",
|
|
37
37
|
json: {data: "test"})
|
|
38
38
|
refute_nil response
|
|
39
39
|
assert_equal 200, response.code
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def test_delete_request
|
|
43
|
-
response = @client.delete("
|
|
43
|
+
response = @client.delete("#{HTTPBIN_URL}/delete")
|
|
44
44
|
refute_nil response
|
|
45
45
|
assert_equal 200, response.code
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
def test_patch_request
|
|
49
|
-
response = @client.patch("
|
|
49
|
+
response = @client.patch("#{HTTPBIN_URL}/patch",
|
|
50
50
|
json: {update: "field"})
|
|
51
51
|
refute_nil response
|
|
52
52
|
assert_equal 200, response.code
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def test_request_with_query_params
|
|
56
|
-
response = @client.get("
|
|
56
|
+
response = @client.get("#{HTTPBIN_URL}/get",
|
|
57
57
|
query: {"param" => "value"})
|
|
58
58
|
refute_nil response
|
|
59
59
|
assert_equal 200, response.code
|
|
@@ -61,7 +61,7 @@ class ClientTest < Minitest::Test
|
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
def test_request_with_form_data
|
|
64
|
-
response = @client.post("
|
|
64
|
+
response = @client.post("#{HTTPBIN_URL}/post",
|
|
65
65
|
form: {"field" => "value"})
|
|
66
66
|
refute_nil response
|
|
67
67
|
assert_equal 200, response.code
|
|
@@ -69,7 +69,7 @@ class ClientTest < Minitest::Test
|
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
def test_request_with_raw_body
|
|
72
|
-
response = @client.post("
|
|
72
|
+
response = @client.post("#{HTTPBIN_URL}/post",
|
|
73
73
|
body: "raw content",
|
|
74
74
|
headers: {"Content-Type" => "text/plain"})
|
|
75
75
|
refute_nil response
|
|
@@ -78,14 +78,14 @@ class ClientTest < Minitest::Test
|
|
|
78
78
|
end
|
|
79
79
|
|
|
80
80
|
def test_basic_authentication
|
|
81
|
-
response = @client.get("
|
|
81
|
+
response = @client.get("#{HTTPBIN_URL}/basic-auth/user/pass",
|
|
82
82
|
basic_auth: ["user", "pass"])
|
|
83
83
|
refute_nil response
|
|
84
84
|
assert_equal 200, response.code
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
def test_bearer_authentication
|
|
88
|
-
response = @client.get("
|
|
88
|
+
response = @client.get("#{HTTPBIN_URL}/bearer",
|
|
89
89
|
bearer_auth: "test-token")
|
|
90
90
|
refute_nil response
|
|
91
91
|
assert_equal 200, response.code
|
|
@@ -93,21 +93,21 @@ class ClientTest < Minitest::Test
|
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
def test_redirect_following
|
|
96
|
-
response = @client.get("
|
|
96
|
+
response = @client.get("#{HTTPBIN_URL}/redirect/1",
|
|
97
97
|
allow_redirects: true)
|
|
98
98
|
refute_nil response
|
|
99
99
|
assert_equal 200, response.code
|
|
100
100
|
end
|
|
101
101
|
|
|
102
102
|
def test_redirect_blocking
|
|
103
|
-
response = @client.get("
|
|
103
|
+
response = @client.get("#{HTTPBIN_URL}/redirect/1",
|
|
104
104
|
allow_redirects: false)
|
|
105
105
|
refute_nil response
|
|
106
106
|
assert_equal 302, response.code
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
def test_gzip_compression
|
|
110
|
-
response = @client.get("
|
|
110
|
+
response = @client.get("#{HTTPBIN_URL}/gzip", gzip: true)
|
|
111
111
|
refute_nil response
|
|
112
112
|
assert_equal 200, response.code
|
|
113
113
|
end
|
|
@@ -115,12 +115,12 @@ class ClientTest < Minitest::Test
|
|
|
115
115
|
def test_timeout_handling
|
|
116
116
|
# Test that short timeouts properly raise exceptions
|
|
117
117
|
assert_raises(Wreq::TimeoutError) do
|
|
118
|
-
@client.get("
|
|
118
|
+
@client.get("#{HTTPBIN_URL}/delay/10", timeout: 1)
|
|
119
119
|
end
|
|
120
120
|
|
|
121
121
|
# Test that reasonable timeouts work normally
|
|
122
122
|
start_time = Time.now
|
|
123
|
-
response = @client.get("
|
|
123
|
+
response = @client.get("#{HTTPBIN_URL}/delay/1", timeout: 5)
|
|
124
124
|
elapsed = Time.now - start_time
|
|
125
125
|
|
|
126
126
|
refute_nil response
|
|
@@ -129,7 +129,7 @@ class ClientTest < Minitest::Test
|
|
|
129
129
|
end
|
|
130
130
|
|
|
131
131
|
def test_status_codes
|
|
132
|
-
response = @client.get("
|
|
132
|
+
response = @client.get("#{HTTPBIN_URL}/status/404")
|
|
133
133
|
refute_nil response
|
|
134
134
|
assert_equal 404, response.code
|
|
135
135
|
end
|
data/test/cookie_test.rb
CHANGED
|
@@ -157,24 +157,26 @@ class CookieTest < Minitest::Test
|
|
|
157
157
|
def test_request_uncompressed_cookies
|
|
158
158
|
client = Wreq::Client.new
|
|
159
159
|
resp = client.get(
|
|
160
|
-
"
|
|
160
|
+
"#{HTTPBIN_URL}/cookies",
|
|
161
161
|
cookies: {"foo" => "bar", "baz" => "qux"}
|
|
162
162
|
)
|
|
163
163
|
json = resp.json
|
|
164
164
|
assert_instance_of Hash, json
|
|
165
|
-
|
|
166
|
-
assert_equal "
|
|
165
|
+
cookies = json.fetch("cookies", json)
|
|
166
|
+
assert_equal "bar", cookies["foo"]
|
|
167
|
+
assert_equal "qux", cookies["baz"]
|
|
167
168
|
end
|
|
168
169
|
|
|
169
170
|
def test_request_compressed_cookies
|
|
170
171
|
client = Wreq::Client.new
|
|
171
172
|
resp = client.get(
|
|
172
|
-
"
|
|
173
|
+
"#{HTTPBIN_URL}/cookies",
|
|
173
174
|
cookies: "foo=bar; baz=qux"
|
|
174
175
|
)
|
|
175
176
|
json = resp.json
|
|
176
177
|
assert_instance_of Hash, json
|
|
177
|
-
|
|
178
|
-
assert_equal "
|
|
178
|
+
cookies = json.fetch("cookies", json)
|
|
179
|
+
assert_equal "bar", cookies["foo"]
|
|
180
|
+
assert_equal "qux", cookies["baz"]
|
|
179
181
|
end
|
|
180
182
|
end
|
data/test/error_handling_test.rb
CHANGED
|
@@ -21,7 +21,7 @@ class ErrorHandlingTest < Minitest::Test
|
|
|
21
21
|
def test_http_error_status_codes
|
|
22
22
|
# These should not raise errors, just return responses with error codes
|
|
23
23
|
[400, 401, 403, 404, 500, 502, 503].each do |status_code|
|
|
24
|
-
response = Wreq.get("
|
|
24
|
+
response = Wreq.get("#{HTTPBIN_URL}/status/#{status_code}")
|
|
25
25
|
assert_equal status_code, response.code
|
|
26
26
|
# Should not raise an exception for HTTP error codes
|
|
27
27
|
end
|
|
@@ -31,7 +31,7 @@ class ErrorHandlingTest < Minitest::Test
|
|
|
31
31
|
# Test timeout with a delay that should definitely cause timeout
|
|
32
32
|
|
|
33
33
|
# Request with a very short timeout that should fail
|
|
34
|
-
response = Wreq.get("
|
|
34
|
+
response = Wreq.get("#{HTTPBIN_URL}/delay/10", timeout: 1)
|
|
35
35
|
# If we get here, the request didn't timeout (unexpected)
|
|
36
36
|
flunk "Expected timeout error but got response: #{response.code}"
|
|
37
37
|
rescue => e
|
|
@@ -42,7 +42,7 @@ class ErrorHandlingTest < Minitest::Test
|
|
|
42
42
|
|
|
43
43
|
def test_invalid_json_response
|
|
44
44
|
# Get a non-JSON response and try to parse as JSON
|
|
45
|
-
response = Wreq.get("
|
|
45
|
+
response = Wreq.get("#{HTTPBIN_URL}/html")
|
|
46
46
|
assert_equal 200, response.code
|
|
47
47
|
|
|
48
48
|
# Should raise an error when trying to parse HTML as JSON
|
|
@@ -54,7 +54,7 @@ class ErrorHandlingTest < Minitest::Test
|
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
def test_empty_response_json
|
|
57
|
-
response = Wreq.get("
|
|
57
|
+
response = Wreq.get("#{HTTPBIN_URL}/status/204")
|
|
58
58
|
assert_equal 204, response.code
|
|
59
59
|
assert_equal "", response.text
|
|
60
60
|
|
data/test/header_test.rb
CHANGED
|
@@ -2,7 +2,7 @@ require "test_helper"
|
|
|
2
2
|
|
|
3
3
|
class HeadersTest < Minitest::Test
|
|
4
4
|
def setup
|
|
5
|
-
@response = Wreq.get("
|
|
5
|
+
@response = Wreq.get("#{HTTPBIN_URL}/response-headers",
|
|
6
6
|
query: {
|
|
7
7
|
"X-Custom-Header" => "custom-value",
|
|
8
8
|
"X-Multi-Header" => "value1"
|
data/test/inspect_test.rb
CHANGED
|
@@ -84,12 +84,12 @@ class InspectTest < Minitest::Test
|
|
|
84
84
|
# ---- Response ----
|
|
85
85
|
|
|
86
86
|
def test_response_to_s_returns_body
|
|
87
|
-
response = Wreq.get("
|
|
87
|
+
response = Wreq.get("#{HTTPBIN_URL}/json")
|
|
88
88
|
assert_equal response.text, response.to_s
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
def test_response_inspect_format
|
|
92
|
-
response = Wreq.get("
|
|
92
|
+
response = Wreq.get("#{HTTPBIN_URL}/json")
|
|
93
93
|
result = response.inspect
|
|
94
94
|
assert result.start_with?("#<Wreq::Response")
|
|
95
95
|
assert_includes result, "200"
|
|
@@ -99,7 +99,7 @@ class InspectTest < Minitest::Test
|
|
|
99
99
|
# ---- StatusCode ----
|
|
100
100
|
|
|
101
101
|
def test_status_code_inspect
|
|
102
|
-
response = Wreq.get("
|
|
102
|
+
response = Wreq.get("#{HTTPBIN_URL}/status/200")
|
|
103
103
|
result = response.status.inspect
|
|
104
104
|
assert result.start_with?("#<Wreq::StatusCode")
|
|
105
105
|
assert_includes result, response.status.to_s
|
|
@@ -117,7 +117,7 @@ class InspectTest < Minitest::Test
|
|
|
117
117
|
end
|
|
118
118
|
|
|
119
119
|
def test_version_inspect_from_response
|
|
120
|
-
response = Wreq.get("
|
|
120
|
+
response = Wreq.get("#{HTTPBIN_URL}/get")
|
|
121
121
|
result = response.version.inspect
|
|
122
122
|
assert result.start_with?("#<Wreq::Version")
|
|
123
123
|
assert result.end_with?(">")
|
data/test/module_methods_test.rb
CHANGED
|
@@ -2,33 +2,33 @@ require "test_helper"
|
|
|
2
2
|
|
|
3
3
|
class ModuleMethodsTest < Minitest::Test
|
|
4
4
|
def test_module_get
|
|
5
|
-
response = Wreq.get("
|
|
5
|
+
response = Wreq.get("#{HTTPBIN_URL}/get")
|
|
6
6
|
refute_nil response
|
|
7
7
|
assert_equal 200, response.code
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def test_module_post
|
|
11
|
-
response = Wreq.post("
|
|
11
|
+
response = Wreq.post("#{HTTPBIN_URL}/post",
|
|
12
12
|
json: {module: "test"})
|
|
13
13
|
refute_nil response
|
|
14
14
|
assert_equal 200, response.code
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def test_module_put
|
|
18
|
-
response = Wreq.put("
|
|
18
|
+
response = Wreq.put("#{HTTPBIN_URL}/put",
|
|
19
19
|
json: {data: "test"})
|
|
20
20
|
refute_nil response
|
|
21
21
|
assert_equal 200, response.code
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def test_module_delete
|
|
25
|
-
response = Wreq.delete("
|
|
25
|
+
response = Wreq.delete("#{HTTPBIN_URL}/delete")
|
|
26
26
|
refute_nil response
|
|
27
27
|
assert_equal 200, response.code
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
def test_module_patch
|
|
31
|
-
response = Wreq.patch("
|
|
31
|
+
response = Wreq.patch("#{HTTPBIN_URL}/patch",
|
|
32
32
|
json: {update: "field"})
|
|
33
33
|
refute_nil response
|
|
34
34
|
assert_equal 200, response.code
|
|
@@ -37,23 +37,23 @@ class ModuleMethodsTest < Minitest::Test
|
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def test_module_request_method
|
|
40
|
-
response = Wreq.request(Wreq::Method::GET, "
|
|
40
|
+
response = Wreq.request(Wreq::Method::GET, "#{HTTPBIN_URL}/get")
|
|
41
41
|
refute_nil response
|
|
42
42
|
assert_equal 200, response.code
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
def test_module_methods_with_parameters
|
|
46
|
-
response = Wreq.get("
|
|
46
|
+
response = Wreq.get("#{HTTPBIN_URL}/get",
|
|
47
47
|
headers: {"Accept" => "application/json"},
|
|
48
48
|
query: {"test" => "module"})
|
|
49
49
|
refute_nil response
|
|
50
|
-
assert_equal
|
|
51
|
-
|
|
50
|
+
assert_equal "#{HTTPBIN_URL}/get?test=module", response.url
|
|
51
|
+
assert_equal "module", httpbin_fetch(response.json["args"], "test")
|
|
52
52
|
assert_equal 200, response.code
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def test_module_post_with_json
|
|
56
|
-
response = Wreq.post("
|
|
56
|
+
response = Wreq.post("#{HTTPBIN_URL}/post",
|
|
57
57
|
json: {
|
|
58
58
|
string: "test",
|
|
59
59
|
number: 123,
|
|
@@ -65,7 +65,7 @@ class ModuleMethodsTest < Minitest::Test
|
|
|
65
65
|
end
|
|
66
66
|
|
|
67
67
|
def test_module_post_with_form
|
|
68
|
-
response = Wreq.post("
|
|
68
|
+
response = Wreq.post("#{HTTPBIN_URL}/post",
|
|
69
69
|
form: {"field1" => "value1", "field2" => "value2"})
|
|
70
70
|
refute_nil response
|
|
71
71
|
assert_equal 200, response.code
|
|
@@ -2,7 +2,7 @@ require "test_helper"
|
|
|
2
2
|
|
|
3
3
|
class RequestParametersTest < Minitest::Test
|
|
4
4
|
def test_query_parameters
|
|
5
|
-
response = Wreq.get("
|
|
5
|
+
response = Wreq.get("#{HTTPBIN_URL}/get",
|
|
6
6
|
query: {
|
|
7
7
|
"string" => "value",
|
|
8
8
|
"number" => "123",
|
|
@@ -12,9 +12,9 @@ class RequestParametersTest < Minitest::Test
|
|
|
12
12
|
|
|
13
13
|
json_data = response.json
|
|
14
14
|
args = json_data["args"]
|
|
15
|
-
assert_equal "value", args
|
|
16
|
-
assert_equal "123", args
|
|
17
|
-
assert_equal "true", args
|
|
15
|
+
assert_equal "value", httpbin_fetch(args, "string")
|
|
16
|
+
assert_equal "123", httpbin_fetch(args, "number") # Query params are strings
|
|
17
|
+
assert_equal "true", httpbin_fetch(args, "boolean")
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def test_headers
|
|
@@ -24,15 +24,15 @@ class RequestParametersTest < Minitest::Test
|
|
|
24
24
|
"Accept" => "application/json"
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
response = Wreq.get("
|
|
27
|
+
response = Wreq.get("#{HTTPBIN_URL}/get", headers: custom_headers)
|
|
28
28
|
assert_equal 200, response.code
|
|
29
29
|
|
|
30
30
|
json_data = response.json
|
|
31
31
|
request_headers = json_data["headers"]
|
|
32
32
|
|
|
33
|
-
assert_equal "custom-value", request_headers
|
|
34
|
-
assert_equal "wreq-ruby-test/1.0", request_headers
|
|
35
|
-
assert_equal "application/json", request_headers
|
|
33
|
+
assert_equal "custom-value", httpbin_fetch(request_headers, "X-Custom-Header")
|
|
34
|
+
assert_equal "wreq-ruby-test/1.0", httpbin_fetch(request_headers, "User-Agent")
|
|
35
|
+
assert_equal "application/json", httpbin_fetch(request_headers, "Accept")
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def test_json_body
|
|
@@ -44,7 +44,7 @@ class RequestParametersTest < Minitest::Test
|
|
|
44
44
|
"object" => {"nested" => "value"}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
response = Wreq.post("
|
|
47
|
+
response = Wreq.post("#{HTTPBIN_URL}/post", json: json_data)
|
|
48
48
|
assert_equal 200, response.code
|
|
49
49
|
|
|
50
50
|
response_data = response.json
|
|
@@ -64,19 +64,19 @@ class RequestParametersTest < Minitest::Test
|
|
|
64
64
|
"number" => "123"
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
response = Wreq.post("
|
|
67
|
+
response = Wreq.post("#{HTTPBIN_URL}/post", form: form_data)
|
|
68
68
|
assert_equal 200, response.code
|
|
69
69
|
|
|
70
70
|
response_data = response.json
|
|
71
71
|
sent_form = response_data["form"]
|
|
72
72
|
|
|
73
|
-
assert_equal "value1", sent_form
|
|
74
|
-
assert_equal "value2", sent_form
|
|
75
|
-
assert_equal "123", sent_form
|
|
73
|
+
assert_equal "value1", httpbin_fetch(sent_form, "field1")
|
|
74
|
+
assert_equal "value2", httpbin_fetch(sent_form, "field2")
|
|
75
|
+
assert_equal "123", httpbin_fetch(sent_form, "number")
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
def test_combined_parameters
|
|
79
|
-
response = Wreq.post("
|
|
79
|
+
response = Wreq.post("#{HTTPBIN_URL}/post",
|
|
80
80
|
query: {"q" => "search"},
|
|
81
81
|
headers: {"X-Test" => "combined"},
|
|
82
82
|
json: {"data" => "payload"})
|
|
@@ -86,10 +86,10 @@ class RequestParametersTest < Minitest::Test
|
|
|
86
86
|
json_data = response.json
|
|
87
87
|
|
|
88
88
|
# Check query parameters
|
|
89
|
-
assert_equal "search", json_data["args"]
|
|
89
|
+
assert_equal "search", httpbin_fetch(json_data["args"], "q")
|
|
90
90
|
|
|
91
91
|
# Check headers
|
|
92
|
-
assert_equal "combined", json_data["headers"]
|
|
92
|
+
assert_equal "combined", httpbin_fetch(json_data["headers"], "X-Test")
|
|
93
93
|
|
|
94
94
|
# Check JSON body
|
|
95
95
|
assert_equal "payload", json_data["json"]["data"]
|
|
@@ -97,7 +97,7 @@ class RequestParametersTest < Minitest::Test
|
|
|
97
97
|
|
|
98
98
|
def test_empty_parameters
|
|
99
99
|
# Test with no additional parameters
|
|
100
|
-
response = Wreq.get("
|
|
100
|
+
response = Wreq.get("#{HTTPBIN_URL}/get")
|
|
101
101
|
assert_equal 200, response.code
|
|
102
102
|
|
|
103
103
|
json_data = response.json
|
|
@@ -113,16 +113,16 @@ class RequestParametersTest < Minitest::Test
|
|
|
113
113
|
"url_chars" => "a=b&c=d"
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
response = Wreq.get("
|
|
116
|
+
response = Wreq.get("#{HTTPBIN_URL}/get", query: special_data)
|
|
117
117
|
assert_equal 200, response.code
|
|
118
118
|
|
|
119
119
|
json_data = response.json
|
|
120
120
|
args = json_data["args"]
|
|
121
121
|
|
|
122
|
-
assert_equal "value with spaces", args
|
|
123
|
-
assert_equal "!@#$%^&*()", args
|
|
124
|
-
assert_equal "测试中文", args
|
|
125
|
-
assert_equal "a=b&c=d", args
|
|
122
|
+
assert_equal "value with spaces", httpbin_fetch(args, "space")
|
|
123
|
+
assert_equal "!@#$%^&*()", httpbin_fetch(args, "symbols")
|
|
124
|
+
assert_equal "测试中文", httpbin_fetch(args, "unicode")
|
|
125
|
+
assert_equal "a=b&c=d", httpbin_fetch(args, "url_chars")
|
|
126
126
|
end
|
|
127
127
|
|
|
128
128
|
def test_nested_json_data
|
|
@@ -140,7 +140,7 @@ class RequestParametersTest < Minitest::Test
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
response = Wreq.post("
|
|
143
|
+
response = Wreq.post("#{HTTPBIN_URL}/post", json: nested_data)
|
|
144
144
|
assert_equal 200, response.code
|
|
145
145
|
|
|
146
146
|
response_data = response.json
|
|
@@ -153,11 +153,11 @@ class RequestParametersTest < Minitest::Test
|
|
|
153
153
|
|
|
154
154
|
def test_method_specific_parameters
|
|
155
155
|
methods_and_urls = {
|
|
156
|
-
get: "
|
|
157
|
-
post: "
|
|
158
|
-
put: "
|
|
159
|
-
patch: "
|
|
160
|
-
delete: "
|
|
156
|
+
get: "#{HTTPBIN_URL}/get",
|
|
157
|
+
post: "#{HTTPBIN_URL}/post",
|
|
158
|
+
put: "#{HTTPBIN_URL}/put",
|
|
159
|
+
patch: "#{HTTPBIN_URL}/patch",
|
|
160
|
+
delete: "#{HTTPBIN_URL}/delete"
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
methods_and_urls.each do |method, url|
|
|
@@ -168,8 +168,8 @@ class RequestParametersTest < Minitest::Test
|
|
|
168
168
|
assert_equal 200, response.code, "#{method} request failed"
|
|
169
169
|
|
|
170
170
|
json_data = response.json
|
|
171
|
-
assert_equal method.to_s, json_data["headers"]
|
|
172
|
-
assert_equal method.to_s, json_data["args"]
|
|
171
|
+
assert_equal method.to_s, httpbin_fetch(json_data["headers"], "X-Method")
|
|
172
|
+
assert_equal method.to_s, httpbin_fetch(json_data["args"], "method")
|
|
173
173
|
end
|
|
174
174
|
end
|
|
175
175
|
end
|