Skip to content

Commit 6499909

Browse files
jjantunexge
authored andcommitted
Fix rewrite_base_url: don't discard query params (#2599)
## Description The previous version of this function discarded query params, which meant that if our model had a `@httpQuery` bound input, those would not be sent to the server and the requests would fail. ## Testing Ran existing tests ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 3e02edc commit 6499909

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

examples/pokemon-service-common/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ use std::{
1717
use async_stream::stream;
1818
use aws_smithy_http::operation::Request;
1919
use aws_smithy_http_server::Extension;
20+
use http::{
21+
uri::{Authority, Scheme},
22+
Uri,
23+
};
2024
use pokemon_service_server_sdk::{
2125
error, input, model, model::CapturingPayload, output, types::Blob,
2226
};
@@ -33,11 +37,16 @@ const PIKACHU_JAPANESE_FLAVOR_TEXT: &str =
3337
"ほっぺたの りょうがわに ちいさい でんきぶくろを もつ。ピンチのときに ほうでんする。";
3438

3539
/// Rewrites the base URL of a request
36-
pub fn rewrite_base_url(base_url: String) -> impl Fn(Request) -> Request + Clone {
40+
pub fn rewrite_base_url(
41+
scheme: Scheme,
42+
authority: Authority,
43+
) -> impl Fn(Request) -> Request + Clone {
3744
move |mut req| {
3845
let http_req = req.http_mut();
39-
let uri = format!("{base_url}{}", http_req.uri().path());
40-
*http_req.uri_mut() = uri.parse().unwrap();
46+
let mut uri_parts = http_req.uri().clone().into_parts();
47+
uri_parts.authority = Some(authority.clone());
48+
uri_parts.scheme = Some(scheme.clone());
49+
*http_req.uri_mut() = Uri::from_parts(uri_parts).expect("failed to create uri from parts");
4150
req
4251
}
4352
}

examples/pokemon-service-common/tests/plugins_execution_order.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ struct SentinelPlugin {
6464

6565
impl SentinelPlugin {
6666
pub fn new(name: &'static str, output: Arc<Mutex<Vec<&'static str>>>) -> Self {
67-
Self {
68-
name,
69-
output: output,
70-
}
67+
Self { name, output }
7168
}
7269
}
7370

examples/pokemon-service-tls/tests/common/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
use std::{fs::File, io::BufReader, process::Command, time::Duration};
6+
use std::{fs::File, io::BufReader, process::Command, str::FromStr, time::Duration};
77

88
use assert_cmd::prelude::*;
99
use aws_smithy_client::{
1010
erase::{DynConnector, DynMiddleware},
1111
hyper_ext::Adapter,
1212
};
13+
use hyper::http::uri::{Authority, Scheme};
1314
use tokio::time::sleep;
1415

1516
use pokemon_service_client::{Builder, Client, Config};
@@ -48,10 +49,11 @@ pub fn client_http2_only() -> Client<DynConnector, DynMiddleware<DynConnector>>
4849
.enable_http2()
4950
.build();
5051

51-
let base_url = format!("https://{DEFAULT_DOMAIN}:{DEFAULT_PORT}");
52+
let authority = Authority::from_str(&format!("{DEFAULT_DOMAIN}:{DEFAULT_PORT}"))
53+
.expect("could not parse authority");
5254
let raw_client = Builder::new()
5355
.connector(DynConnector::new(Adapter::builder().build(connector)))
54-
.middleware_fn(rewrite_base_url(base_url))
56+
.middleware_fn(rewrite_base_url(Scheme::HTTPS, authority))
5557
.build_dyn();
5658
let config = Config::builder().build();
5759
Client::with_config(raw_client, config)

examples/pokemon-service/tests/common/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
use std::{process::Command, time::Duration};
6+
use std::{process::Command, str::FromStr, time::Duration};
77

88
use assert_cmd::prelude::*;
99
use aws_smithy_client::erase::{DynConnector, DynMiddleware};
10+
use hyper::http::uri::{Authority, Scheme};
1011
use tokio::time::sleep;
1112

1213
use pokemon_service::{DEFAULT_ADDRESS, DEFAULT_PORT};
@@ -25,10 +26,11 @@ pub async fn run_server() -> ChildDrop {
2526
}
2627

2728
pub fn client() -> Client<DynConnector, DynMiddleware<DynConnector>> {
28-
let base_url = format!("http://{DEFAULT_ADDRESS}:{DEFAULT_PORT}");
29+
let authority = Authority::from_str(&format!("{DEFAULT_ADDRESS}:{DEFAULT_PORT}"))
30+
.expect("could not parse authority");
2931
let raw_client = Builder::new()
3032
.rustls_connector(Default::default())
31-
.middleware_fn(rewrite_base_url(base_url))
33+
.middleware_fn(rewrite_base_url(Scheme::HTTP, authority))
3234
.build_dyn();
3335
let config = Config::builder().build();
3436
Client::with_config(raw_client, config)

0 commit comments

Comments
 (0)