Skip to content

Commit 1891565

Browse files
CopilotCBenoit
andcommitted
fix(test): address code review feedback for ai_gateway tests
Co-authored-by: CBenoit <3809077+CBenoit@users.noreply.github.com>
1 parent ddd9c58 commit 1891565

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

testsuite/tests/cli/dgw/ai_gateway.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,23 @@ async fn spawn_mock_openai_server(response_body: &str) -> SocketAddr {
2727
let mut content_length = 0;
2828
let mut received_auth_header = String::new();
2929

30-
// Read HTTP headers.
30+
// Read HTTP headers (malformed headers are silently ignored in this test helper).
3131
loop {
3232
let mut line = String::new();
33-
if reader.read_line(&mut line).await.unwrap() == 0 {
33+
if reader.read_line(&mut line).await.unwrap_or(0) == 0 {
3434
break;
3535
}
3636
if line.to_lowercase().starts_with("content-length:") {
37-
content_length = line.trim().split(':').nth(1).unwrap().trim().parse().unwrap_or(0);
37+
content_length = line
38+
.split_once(':')
39+
.and_then(|(_, v)| v.trim().parse().ok())
40+
.unwrap_or(0);
3841
}
3942
if line.to_lowercase().starts_with("authorization:") {
40-
received_auth_header = line.trim().split(':').nth(1).unwrap().trim().to_owned();
43+
received_auth_header = line
44+
.split_once(':')
45+
.map(|(_, v)| v.trim().to_owned())
46+
.unwrap_or_default();
4147
}
4248
if line.trim().is_empty() {
4349
break;
@@ -150,9 +156,6 @@ async fn make_ai_request(
150156
auth_header: Option<&str>,
151157
body: Option<&str>,
152158
) -> anyhow::Result<(u16, String)> {
153-
let listener = TcpListener::bind("127.0.0.1:0").await?;
154-
drop(listener); // Just to check port binding works.
155-
156159
let stream = tokio::net::TcpStream::connect(format!("127.0.0.1:{gateway_port}")).await?;
157160
let (mut reader, mut writer) = stream.into_split();
158161

@@ -197,7 +200,10 @@ async fn make_ai_request(
197200
.lines()
198201
.find(|l| l.to_lowercase().starts_with("content-length:"))
199202
{
200-
let cl: usize = cl_line.split(':').nth(1).unwrap().trim().parse().unwrap_or(0);
203+
let cl: usize = cl_line
204+
.split_once(':')
205+
.and_then(|(_, v)| v.trim().parse().ok())
206+
.unwrap_or(0);
201207
let body_start = headers_end + 4;
202208
if response.len() >= body_start + cl {
203209
break;

0 commit comments

Comments
 (0)