Skip to content

Commit f701426

Browse files
Fixes for undici
1 parent c12a2f1 commit f701426

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

packages/testcontainers/src/wait-strategies/http-wait-strategy.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Dockerode from "dockerode";
2-
import { Agent } from "undici";
2+
import { Agent, Dispatcher, request } from "undici";
33
import { IntervalRetry, log } from "../common";
44
import { getContainerRuntimeClient } from "../container-runtime";
55
import { BoundPorts } from "../utils/bound-ports";
@@ -91,17 +91,18 @@ export class HttpWaitStrategy extends AbstractWaitStrategy {
9191

9292
if (containerStatus === exitStatus) {
9393
containerExited = true;
94-
9594
return;
9695
}
9796
}
9897

99-
return await fetch(url, {
100-
method: this.method,
101-
signal: AbortSignal.timeout(this.readTimeout),
102-
headers: this.headers,
103-
dispatcher: this.getAgent(),
104-
});
98+
return this.undiciResponseToFetchResponse(
99+
await request(url, {
100+
method: this.method,
101+
signal: AbortSignal.timeout(this.readTimeout),
102+
headers: this.headers,
103+
dispatcher: this.getAgent(),
104+
})
105+
);
105106
} catch {
106107
return undefined;
107108
}
@@ -163,6 +164,31 @@ export class HttpWaitStrategy extends AbstractWaitStrategy {
163164
throw new Error(message);
164165
}
165166

167+
/**
168+
* Converts an undici response to a fetch response.
169+
* This is necessary because node's fetch does not support disabling SSL validation (https://github.com/orgs/nodejs/discussions/44038).
170+
*
171+
* @param undiciResponse The undici response to convert.
172+
* @returns The fetch response.
173+
*/
174+
private undiciResponseToFetchResponse(undiciResponse: Dispatcher.ResponseData): Response {
175+
const headers = new Headers();
176+
for (const [key, value] of Object.entries(undiciResponse.headers)) {
177+
if (Array.isArray(value)) {
178+
for (const v of value) {
179+
headers.append(key, v);
180+
}
181+
} else if (value !== undefined) {
182+
headers.set(key, value);
183+
}
184+
}
185+
186+
return new Response(undiciResponse.body, {
187+
status: undiciResponse.statusCode,
188+
headers,
189+
});
190+
}
191+
166192
private getAgent(): Agent | undefined {
167193
if (this._allowInsecure) {
168194
return new Agent({

0 commit comments

Comments
 (0)