Debian Codegraph @

crt/s2n/bindings/rust/standard/integration/src/network/https_client.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use bytes::Bytes;
5 use http::{Response, StatusCode, Uri};
6 use http_body_util::{BodyExt, Empty};
7 use hyper::body::Incoming;
8 use hyper_util::{client::legacy::Client, rt::TokioExecutor};
9 use s2n_tls::{
10 config::Config,
11 security::{self, Policy},
12 };
13 use s2n_tls_hyper::connector::HttpsConnector;
14 use std::str::FromStr;
15
16 #[derive(Debug)]
17 struct TestCase {
18 pub query_target: &'static str,
19 /// We accept multiple possible results because some websites frequently change
20 /// behavior, possibly as a result of throttling the IP ranges of our CI
21 /// providers.
22 pub expected_status_codes: &'static [u16],
23 }
24
25 impl TestCase {
26 const fn new(domain: &'static str, expected_status_codes: &'static [u16]) -> Self {
27 TestCase {
28 query_target: domain,
29 expected_status_codes,
30 }
31 }
32 }
33
34 const TEST_CASES: &[TestCase] = &[
35 // this is a link to the s2n-tls unit test coverage report, hosted on cloudfront
36 TestCase::new(
37 "https://dx1inn44oyl7n.cloudfront.net/main/index.html",
38 &[200],
39 ),
40 // this is a link to a non-existent S3 item
41 TestCase::new(
42 "https://notmybucket.s3.amazonaws.com/folder/afile.jpg",
43 &[403],
44 ),
45 TestCase::new("https://www.amazon.com", &[200]),
46 TestCase::new("https://www.apple.com", &[200]),
47 TestCase::new("https://www.att.com", &[200]),
48 TestCase::new("https://www.cloudflare.com", &[200]),
49 // 2026-04-28: ebay.com returns 400 under
50 // `cargo test --no-default-features --features pq`.
51 // 2026-06-16: ebay.com also returns 307 (redirect).
52 TestCase::new("https://www.ebay.com", &[200, 307, 400]),
53 TestCase::new("https://www.google.com", &[200]),
54 TestCase::new("https://www.mozilla.org", &[200]),
55 TestCase::new("https://www.netflix.com", &[200]),
56 TestCase::new("https://www.openssl.org", &[200]),
57 TestCase::new("https://www.t-mobile.com", &[200]),
58 TestCase::new("https://www.verizon.com", &[200]),
59 TestCase::new("https://www.wikipedia.org", &[200, 403]),
60 TestCase::new("https://www.yahoo.com", &[200, 429]),
61 TestCase::new("https://www.youtube.com", &[200]),
62 TestCase::new("https://www.github.com", &[301]),
63 TestCase::new("https://www.samsung.com", &[301]),
64 TestCase::new("https://www.twitter.com", &[301]),
65 TestCase::new("https://www.facebook.com", &[302]),
66 // 2024-11-21: Microsoft had been consistently returning a 302. It then started
67 // returning 403 codes in CI, but was returning 200 codes when run locally.
68 TestCase::new("https://www.microsoft.com", &[200, 302, 403]),
69 TestCase::new("https://www.ibm.com", &[303]),
70 TestCase::new("https://www.f5.com", &[403]),
71 ];
72
73 /// perform an HTTP GET request against `uri` using an s2n-tls config with
74 /// `security_policy`.
75 async fn https_get(
76 uri: &str,
77 security_policy: &Policy,
78 ) -> Result<Response<Incoming>, hyper_util::client::legacy::Error> {
79 let mut config = Config::builder();
80 config.set_security_policy(security_policy).unwrap();
81
82 let connector = HttpsConnector::new(config.build().unwrap());
83 let client: Client<_, Empty<Bytes>> = Client::builder(TokioExecutor::new()).build(connector);
84
85 let uri = Uri::from_str(uri).unwrap();
86 client.get(uri).await
87 }
88
89 /// Ensure that s2n-tls is compatible with other http/TLS implementations.
90 ///
91 /// This test uses s2n-tls-hyper to make http requests over a TLS connection to
92 /// a number of well known http sites.
93 #[test_log::test(tokio::test)]
94 async fn http_get_test() -> Result<(), Box<dyn std::error::Error>> {
95 for test_case in TEST_CASES {
96 for policy in [security::DEFAULT, security::DEFAULT_TLS13] {
97 tracing::info!("executing test case {:#?} with {:?}", test_case, policy);
98
99 let response = https_get(test_case.query_target, &policy).await?;
100 let status_code = response.status().as_u16();
101
102 let status_was_expected = test_case.expected_status_codes.contains(&status_code);
103 if !status_was_expected {
104 tracing::error!("unexpected status code: {status_code}");
105 }
106 assert!(
107 status_was_expected,
108 "Unexpected status code \"{status_code}\" for test case {:#?}",
109 test_case
110 );
111
112 if status_code == StatusCode::OK.as_u16() {
113 let body = response.into_body().collect().await?.to_bytes();
114 assert!(!body.is_empty());
115 }
116 }
117 }
118
119 Ok(())
120 }