@@ -120,3 +120,61 @@ pub async fn resolve_hostname(
120
120
) )
121
121
} )
122
122
}
123
+
124
+ #[ cfg( test) ]
125
+ mod test {
126
+
127
+ use std:: { io:: ErrorKind , sync:: Arc } ;
128
+
129
+ use rustls:: RootCertStore ;
130
+ use tokio_rustls:: TlsConnector ;
131
+
132
+ use super :: * ;
133
+
134
+ #[ tokio:: test]
135
+ async fn can_fetch_tls_content_with_proxy_connection ( ) {
136
+ let host = "api.turnkey.com" ;
137
+ let path = "/health" ;
138
+
139
+ let mut remote_connection = ProxyConnection :: new_from_name (
140
+ host. to_string ( ) ,
141
+ 443 ,
142
+ vec ! [ "8.8.8.8" . to_string( ) ] ,
143
+ 53 ,
144
+ )
145
+ . await
146
+ . unwrap ( ) ;
147
+
148
+ let root_store =
149
+ RootCertStore { roots : webpki_roots:: TLS_SERVER_ROOTS . into ( ) } ;
150
+
151
+ let server_name: rustls:: pki_types:: ServerName < ' _ > =
152
+ host. try_into ( ) . unwrap ( ) ;
153
+ let config: rustls:: ClientConfig = rustls:: ClientConfig :: builder ( )
154
+ . with_root_certificates ( root_store)
155
+ . with_no_client_auth ( ) ;
156
+ let conn = TlsConnector :: from ( Arc :: new ( config) ) ;
157
+ let stream = & mut remote_connection. tcp_stream ;
158
+ let mut tls = conn. connect ( server_name, stream) . await . unwrap ( ) ;
159
+
160
+ let http_request = format ! (
161
+ "GET {path} HTTP/1.1\r \n Host: {host}\r \n Connection: close\r \n \r \n "
162
+ ) ;
163
+
164
+ tls. write_all ( http_request. as_bytes ( ) ) . await . unwrap ( ) ;
165
+
166
+ let mut response_bytes = Vec :: new ( ) ;
167
+ let read_to_end_result = tls. read_to_end ( & mut response_bytes) . await ;
168
+
169
+ // Ignore eof errors: https://docs.rs/rustls/latest/rustls/manual/_03_howto/index.html#unexpected-eof
170
+ assert ! (
171
+ read_to_end_result. is_ok( )
172
+ || ( read_to_end_result
173
+ . is_err_and( |e| e. kind( ) == ErrorKind :: UnexpectedEof ) )
174
+ ) ;
175
+
176
+ let response_text = std:: str:: from_utf8 ( & response_bytes) . unwrap ( ) ;
177
+ assert ! ( response_text. contains( "HTTP/1.1 200 OK" ) ) ;
178
+ assert ! ( response_text. contains( "currentTime" ) ) ;
179
+ }
180
+ }
0 commit comments