wreq 1.3.2.2 → 1.3.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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +10 -10
  3. data/Cargo.toml +2 -2
  4. data/README.md +11 -4
  5. data/VERSION +1 -1
  6. data/crates/wreq-util/src/emulate/macros.rs +31 -197
  7. data/crates/wreq-util/src/emulate/profile/chromium/chrome.rs +731 -0
  8. data/crates/wreq-util/src/emulate/profile/chromium/edge.rs +418 -0
  9. data/crates/wreq-util/src/emulate/profile/chromium/header.rs +74 -0
  10. data/crates/wreq-util/src/emulate/profile/chromium/http2.rs +59 -0
  11. data/crates/wreq-util/src/emulate/profile/chromium/opera.rs +312 -0
  12. data/crates/wreq-util/src/emulate/profile/{chrome → chromium}/tls.rs +146 -137
  13. data/crates/wreq-util/src/emulate/profile/chromium.rs +9 -0
  14. data/crates/wreq-util/src/emulate/profile/firefox/header.rs +48 -54
  15. data/crates/wreq-util/src/emulate/profile/firefox/http2.rs +92 -102
  16. data/crates/wreq-util/src/emulate/profile/firefox/tls.rs +155 -146
  17. data/crates/wreq-util/src/emulate/profile/firefox.rs +312 -440
  18. data/crates/wreq-util/src/emulate/profile.rs +5 -7
  19. data/crates/wreq-util/src/emulate.rs +33 -1
  20. data/crates/wreq-util/tests/client.rs +187 -46
  21. data/crates/wreq-util/tests/emulate_chrome.rs +7 -0
  22. data/crates/wreq-util/tests/emulate_firefox.rs +15 -0
  23. data/crates/wreq-util/tests/emulate_opera.rs +24 -0
  24. data/lib/wreq_ruby/emulate.rb +27 -0
  25. data/src/emulate.rs +27 -0
  26. data/test/emulation_client_hello_test.rb +142 -0
  27. data/test/emulation_test.rb +21 -105
  28. metadata +9 -9
  29. data/crates/wreq-util/src/emulate/profile/chrome/header.rs +0 -116
  30. data/crates/wreq-util/src/emulate/profile/chrome/http2.rs +0 -65
  31. data/crates/wreq-util/src/emulate/profile/chrome.rs +0 -2304
  32. data/crates/wreq-util/src/emulate/profile/opera/header.rs +0 -29
  33. data/crates/wreq-util/src/emulate/profile/opera/http2.rs +0 -50
  34. data/crates/wreq-util/src/emulate/profile/opera/tls.rs +0 -103
  35. data/crates/wreq-util/src/emulate/profile/opera.rs +0 -299
@@ -1,120 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "test_helper"
4
- require "socket"
5
- require "stringio"
6
- require "timeout"
7
4
 
8
5
  class EmulationTest < Minitest::Test
9
- def test_reused_emulation_retains_trust_anchors_across_clients_and_requests
10
- [Wreq::Profile::Chrome152, Wreq::Profile::Chrome153, Wreq::Profile::Chrome154, Wreq::Profile::Chrome155, Wreq::Profile::Chrome156, Wreq::Profile::Chrome157].each do |profile|
11
- emulation = Wreq::Emulation.new(profile: profile)
12
- client = Wreq::Client.new(emulation: emulation, no_proxy: true)
13
- expected = capture_trust_anchors { |url| client.get(url, timeout: 5) }
14
- refute_empty expected, profile.to_s
15
-
16
- request_client = Wreq::Client.new(no_proxy: true)
17
- 2.times do
18
- actual = capture_trust_anchors do |url|
19
- request_client.get(url, emulation: emulation, timeout: 5)
20
- end
21
- assert_equal expected, actual, "#{profile}: reusing an object must retain its configuration"
22
- end
23
- end
24
- end
25
-
26
- def test_all_emulation_device_constants_are_non_nil
27
- Wreq::Profile.constants.each do |name|
28
- const = Wreq::Profile.const_get(name)
29
- assert_instance_of Wreq::Profile, const,
30
- "#{name} should be Profile, got #{const.inspect}"
31
- end
32
- end
33
-
34
- def test_chrome151_profile_is_available
35
- profile = Wreq::Profile::Chrome151
36
-
37
- assert_equal "Chrome151", profile.to_s
38
- assert_instance_of Wreq::Emulation, Wreq::Emulation.new(profile: profile)
39
- end
40
-
41
- def test_new_browser_profiles_are_available
42
- {
43
- Wreq::Profile::Chrome152 => "Chrome152",
44
- Wreq::Profile::Chrome153 => "Chrome153",
45
- Wreq::Profile::Chrome154 => "Chrome154",
46
- Wreq::Profile::Chrome155 => "Chrome155",
47
- Wreq::Profile::Chrome156 => "Chrome156",
48
- Wreq::Profile::Chrome157 => "Chrome157",
49
- Wreq::Profile::Chrome158 => "Chrome158",
50
- Wreq::Profile::Firefox152 => "Firefox152"
51
- }.each do |profile, name|
52
- assert_equal name, profile.to_s
53
- assert_instance_of Wreq::Emulation, Wreq::Emulation.new(profile: profile)
6
+ def test_expected_browser_profiles_are_registered
7
+ versions_by_browser = {
8
+ "Chrome" => 151..158,
9
+ "Edge" => [101, 122, 127, 131, *(134..158)],
10
+ "Firefox" => [109, 117, 128, 133, 135, 136, 139, *(142..160)],
11
+ "Opera" => 116..140
12
+ }
13
+ expected = versions_by_browser.flat_map do |browser, versions|
14
+ versions.map { |version| "#{browser}#{version}" }
54
15
  end
55
- end
16
+ expected.concat(%w[FirefoxPrivate135 FirefoxAndroid135 FirefoxPrivate136])
56
17
 
57
- def test_all_emulation_os_constants_are_non_nil
58
- Wreq::Platform.constants.each do |name|
59
- const = Wreq::Platform.const_get(name)
60
- assert_instance_of Wreq::Platform, const,
61
- "#{name} should be Platform, got #{const.inspect}"
62
- end
18
+ registered = Wreq::Profile.constants(false).map(&:to_s)
19
+ assert_empty expected - registered, "missing browser profiles"
63
20
  end
64
21
 
65
- private
66
-
67
- # Each listener forces a new connection. Stop after ClientHello so this test
68
- # inspects the extension bytes without depending on certificates or the network.
69
- def capture_trust_anchors
70
- server = TCPServer.new("127.0.0.1", 0)
71
- thread = Thread.new do
72
- socket = nil
73
- Timeout.timeout(5) do
74
- socket = server.accept
75
- handshake = +"".b
76
- loop do
77
- header = read_tls_bytes(socket, 5)
78
- raise "expected a TLS handshake record" unless header.getbyte(0) == 22
79
- handshake << read_tls_bytes(socket, header.byteslice(3, 2).unpack1("n"))
80
- next if handshake.bytesize < 4
81
- length = handshake.byteslice(1, 3).bytes.reduce(0) { |n, byte| (n << 8) | byte }
82
- break if handshake.bytesize >= length + 4
83
- end
84
- trust_anchors_from_client_hello(handshake)
85
- end
86
- ensure
87
- socket&.close
22
+ def test_registered_profiles_have_matching_names_and_build_emulation
23
+ Wreq::Profile.constants(false).each do |name|
24
+ profile = Wreq::Profile.const_get(name, false)
25
+ assert_instance_of Wreq::Profile, profile, name.to_s
26
+ assert_equal name.to_s, profile.to_s
27
+ assert_instance_of Wreq::Emulation, Wreq::Emulation.new(profile: profile), name.to_s
88
28
  end
89
- thread.report_on_exception = false
90
-
91
- assert_raises(Wreq::ConnectionError) { yield "https://127.0.0.1:#{server.addr[1]}/" }
92
- thread.value
93
- ensure
94
- server&.close
95
- thread&.kill if thread&.alive?
96
- thread&.join
97
- end
98
-
99
- def read_tls_bytes(io, length)
100
- bytes = io.read(length)
101
- raise EOFError, "truncated ClientHello" unless bytes&.bytesize == length
102
- bytes
103
29
  end
104
30
 
105
- def trust_anchors_from_client_hello(handshake)
106
- io = StringIO.new(handshake)
107
- raise "expected ClientHello" unless read_tls_bytes(io, 1).unpack1("C") == 1
108
- read_tls_bytes(io, 3 + 2 + 32) # Handshake length, legacy version and random.
109
- read_tls_bytes(io, read_tls_bytes(io, 1).unpack1("C")) # Session ID.
110
- read_tls_bytes(io, read_tls_bytes(io, 2).unpack1("n")) # Cipher suites.
111
- read_tls_bytes(io, read_tls_bytes(io, 1).unpack1("C")) # Compression methods.
112
- extensions = StringIO.new(read_tls_bytes(io, read_tls_bytes(io, 2).unpack1("n")))
113
- until extensions.eof?
114
- type, length = read_tls_bytes(extensions, 4).unpack("nn")
115
- data = read_tls_bytes(extensions, length)
116
- return data if type == 51764
31
+ def test_registered_platforms_are_platform_values
32
+ Wreq::Platform.constants(false).each do |name|
33
+ assert_instance_of Wreq::Platform, Wreq::Platform.const_get(name, false), name.to_s
117
34
  end
118
- raise "missing trust_anchors extension"
119
35
  end
120
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wreq
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2.2
4
+ version: 1.3.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - SearchApi
@@ -50,19 +50,18 @@ files:
50
50
  - crates/wreq-util/src/emulate/compress.rs
51
51
  - crates/wreq-util/src/emulate/macros.rs
52
52
  - crates/wreq-util/src/emulate/profile.rs
53
- - crates/wreq-util/src/emulate/profile/chrome.rs
54
- - crates/wreq-util/src/emulate/profile/chrome/header.rs
55
- - crates/wreq-util/src/emulate/profile/chrome/http2.rs
56
- - crates/wreq-util/src/emulate/profile/chrome/tls.rs
53
+ - crates/wreq-util/src/emulate/profile/chromium.rs
54
+ - crates/wreq-util/src/emulate/profile/chromium/chrome.rs
55
+ - crates/wreq-util/src/emulate/profile/chromium/edge.rs
56
+ - crates/wreq-util/src/emulate/profile/chromium/header.rs
57
+ - crates/wreq-util/src/emulate/profile/chromium/http2.rs
58
+ - crates/wreq-util/src/emulate/profile/chromium/opera.rs
59
+ - crates/wreq-util/src/emulate/profile/chromium/tls.rs
57
60
  - crates/wreq-util/src/emulate/profile/firefox.rs
58
61
  - crates/wreq-util/src/emulate/profile/firefox/header.rs
59
62
  - crates/wreq-util/src/emulate/profile/firefox/http2.rs
60
63
  - crates/wreq-util/src/emulate/profile/firefox/tls.rs
61
64
  - crates/wreq-util/src/emulate/profile/okhttp.rs
62
- - crates/wreq-util/src/emulate/profile/opera.rs
63
- - crates/wreq-util/src/emulate/profile/opera/header.rs
64
- - crates/wreq-util/src/emulate/profile/opera/http2.rs
65
- - crates/wreq-util/src/emulate/profile/opera/tls.rs
66
65
  - crates/wreq-util/src/emulate/profile/safari.rs
67
66
  - crates/wreq-util/src/emulate/profile/safari/header.rs
68
67
  - crates/wreq-util/src/emulate/profile/safari/http2.rs
@@ -153,6 +152,7 @@ files:
153
152
  - test/client_cookie_test.rb
154
153
  - test/client_test.rb
155
154
  - test/cookie_test.rb
155
+ - test/emulation_client_hello_test.rb
156
156
  - test/emulation_test.rb
157
157
  - test/error_handling_test.rb
158
158
  - test/fork_test.rb
@@ -1,116 +0,0 @@
1
- use super::*;
2
-
3
- pub fn header_initializer(
4
- sec_ch_ua: &'static str,
5
- ua: &'static str,
6
- emulation_os: Platform,
7
- ) -> HeaderMap {
8
- let mut headers = HeaderMap::new();
9
- header_chrome_sec_ch_ua!(
10
- headers,
11
- sec_ch_ua,
12
- emulation_os.platform(),
13
- emulation_os.is_mobile()
14
- );
15
- headers.insert(
16
- HeaderName::from_static("upgrade-insecure-requests"),
17
- HeaderValue::from_static("1"),
18
- );
19
- header_chrome_ua!(headers, ua);
20
- header_chrome_accept!(headers);
21
- header_chrome_sec_fetch!(headers);
22
- header_chrome_accept_encoding!(headers);
23
- headers
24
- }
25
-
26
- pub fn header_initializer_with_zstd(
27
- sec_ch_ua: &'static str,
28
- ua: &'static str,
29
- emulation_os: Platform,
30
- ) -> HeaderMap {
31
- let mut headers = HeaderMap::new();
32
- header_chrome_sec_ch_ua!(
33
- headers,
34
- sec_ch_ua,
35
- emulation_os.platform(),
36
- emulation_os.is_mobile()
37
- );
38
- headers.insert(
39
- HeaderName::from_static("upgrade-insecure-requests"),
40
- HeaderValue::from_static("1"),
41
- );
42
- header_chrome_ua!(headers, ua);
43
- header_chrome_accept!(headers);
44
- header_chrome_sec_fetch!(headers);
45
- header_chrome_accept_encoding!(zstd, headers);
46
- headers
47
- }
48
-
49
- pub fn header_initializer_with_zstd_priority(
50
- sec_ch_ua: &'static str,
51
- ua: &'static str,
52
- emulation_os: Platform,
53
- ) -> HeaderMap {
54
- let mut headers = HeaderMap::new();
55
- header_chrome_sec_ch_ua!(
56
- headers,
57
- sec_ch_ua,
58
- emulation_os.platform(),
59
- emulation_os.is_mobile()
60
- );
61
- headers.insert(
62
- HeaderName::from_static("upgrade-insecure-requests"),
63
- HeaderValue::from_static("1"),
64
- );
65
- header_chrome_ua!(headers, ua);
66
- header_chrome_accept!(headers);
67
- header_chrome_sec_fetch!(headers);
68
- header_chrome_accept_encoding!(zstd, headers);
69
- headers.insert(
70
- HeaderName::from_static("priority"),
71
- HeaderValue::from_static("u=0, i"),
72
- );
73
- headers
74
- }
75
-
76
- pub fn header_initializer_chrome155(
77
- sec_ch_ua: &'static str,
78
- ua: &'static str,
79
- emulation_os: Platform,
80
- ) -> HeaderMap {
81
- let mut headers = header_initializer_with_zstd_priority(sec_ch_ua, ua, emulation_os);
82
- // Chrome 155 enables JPEG XL in Blink; the iOS profile keeps its WebKit headers.
83
- // https://chromium.googlesource.com/chromium/src/+/cfaadc5a132d78e1828635aa8405a499f3e14864/content/common/content_constants_internal.h
84
- if !matches!(emulation_os, Platform::IOS) {
85
- headers.insert(ACCEPT, HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,image/jxl,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"));
86
- }
87
- headers
88
- }
89
-
90
- pub fn header_initializer_chrome152(
91
- sec_ch_ua: &'static str,
92
- ua: &'static str,
93
- emulation_os: Platform,
94
- ) -> HeaderMap {
95
- let mut headers = HeaderMap::new();
96
- header_chrome_sec_ch_ua!(
97
- headers,
98
- sec_ch_ua,
99
- emulation_os.platform(),
100
- emulation_os.is_mobile()
101
- );
102
- headers.insert(ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.9"));
103
- headers.insert(
104
- HeaderName::from_static("upgrade-insecure-requests"),
105
- HeaderValue::from_static("1"),
106
- );
107
- header_chrome_ua!(headers, ua);
108
- header_chrome_accept!(headers);
109
- header_chrome_sec_fetch!(headers);
110
- header_chrome_accept_encoding!(zstd_only, headers);
111
- headers.insert(
112
- HeaderName::from_static("priority"),
113
- HeaderValue::from_static("u=0, i"),
114
- );
115
- headers
116
- }
@@ -1,65 +0,0 @@
1
- macro_rules! headers_priority {
2
- () => {
3
- HeadersPriority::new(HeadersDependency::Root, 255, true)
4
- };
5
- }
6
-
7
- macro_rules! pseudo_order {
8
- () => {
9
- PseudoOrder::builder()
10
- .extend([
11
- PseudoId::Method,
12
- PseudoId::Authority,
13
- PseudoId::Scheme,
14
- PseudoId::Path,
15
- ])
16
- .build()
17
- };
18
- }
19
-
20
- macro_rules! settings_order {
21
- () => {
22
- SettingsOrder::builder()
23
- .extend([
24
- SettingId::HeaderTableSize,
25
- SettingId::EnablePush,
26
- SettingId::MaxConcurrentStreams,
27
- SettingId::InitialWindowSize,
28
- SettingId::MaxFrameSize,
29
- SettingId::MaxHeaderListSize,
30
- SettingId::EnableConnectProtocol,
31
- SettingId::NoRfc7540Priorities,
32
- ])
33
- .build()
34
- };
35
- }
36
-
37
- macro_rules! http2_options {
38
- (@base $builder:expr) => {
39
- $builder
40
- .initial_window_size(6291456)
41
- .initial_connection_window_size(15728640)
42
- .max_header_list_size(262144)
43
- .header_table_size(65536)
44
- .headers_priority(headers_priority!())
45
- .headers_pseudo_order(pseudo_order!())
46
- .settings_order(settings_order!())
47
- };
48
-
49
- (1) => {
50
- http2_options!(@base Http2Options::builder())
51
- .max_concurrent_streams(1000)
52
- .build()
53
- };
54
- (2) => {
55
- http2_options!(@base Http2Options::builder())
56
- .max_concurrent_streams(1000)
57
- .enable_push(false)
58
- .build()
59
- };
60
- (3) => {
61
- http2_options!(@base Http2Options::builder())
62
- .enable_push(false)
63
- .build()
64
- };
65
- }