Skip to content

Commit c5a7f41

Browse files
committed
implement #1023
1 parent 3f3eed1 commit c5a7f41

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

docs/modules/mockserver.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ npm install @testcontainers/mockserver --save-dev
1313
<!--codeinclude-->
1414
[Start container:](../../packages/modules/mockserver/src/mockserver-container.test.ts) inside_block:startContainer
1515
<!--/codeinclude-->
16+
17+
MockServer includes built-in TLS support. To obtain an HTTPS URL, use the `getSecureUrl` method. Keep in mind that MockServer uses a self-signed certificate.
18+
19+
<!--codeinclude-->
20+
[Using TLS:](../../packages/modules/mockserver/src/mockserver-container.test.ts) inside_block:httpsRequests
21+
<!--/codeinclude-->

packages/modules/mockserver/src/mockserver-container.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,39 @@ describe("MockserverContainer", { timeout: 240_000 }, () => {
3131
expect(response.text).toBe("bar");
3232
});
3333
// }
34+
35+
it("should return an https url", async () => {
36+
const container = await new MockserverContainer(IMAGE).start();
37+
const secureUrl = container.getSecureUrl()
38+
await container.stop()
39+
expect(secureUrl.startsWith("https://")).to.equal(true, `${secureUrl} does not start with https://`)
40+
})
41+
42+
// httpsRequests {
43+
it("should respond to https requests", async () => {
44+
const container = await new MockserverContainer(IMAGE).start();
45+
const client = mockServerClient(container.getHost(), container.getMockserverPort());
46+
47+
await client.mockAnyResponse({
48+
httpRequest: {
49+
method: "GET",
50+
path: "/foo",
51+
},
52+
httpResponse: {
53+
body: {
54+
string: "bar",
55+
},
56+
statusCode: 200,
57+
},
58+
});
59+
60+
const secureUrl = container.getSecureUrl()
61+
const response = await superagent.get(`${secureUrl}/foo`).disableTLSCerts();
62+
63+
expect(response.statusCode).toBe(200);
64+
expect(response.text).toBe("bar");
65+
66+
await container.stop()
67+
})
68+
// }
3469
});

packages/modules/mockserver/src/mockserver-container.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export class StartedMockserverContainer extends AbstractStartedContainer {
88
getUrl(): string {
99
return `http://${this.getHost()}:${this.getFirstMappedPort()}`;
1010
}
11+
12+
getSecureUrl(): string {
13+
return `https://${this.getHost()}:${this.getFirstMappedPort()}`
14+
}
1115
}
1216

1317
const MOCKSERVER_PORT = 1080;

0 commit comments

Comments
 (0)