From cd6a4010332e1bd7d410c79ab8b9d301d952b66d Mon Sep 17 00:00:00 2001 From: Neenad Ingole Date: Sat, 10 Aug 2024 22:21:35 +0200 Subject: [PATCH] Add test for auth support in headers --- tc-wiremock-v3_test.go | 40 +++++++++++++++++++++++++++++++++ testdata/200-v3-basic-auth.json | 13 +++++++++++ 2 files changed, 53 insertions(+) create mode 100644 testdata/200-v3-basic-auth.json diff --git a/tc-wiremock-v3_test.go b/tc-wiremock-v3_test.go index f87bfc4..c2c9c02 100644 --- a/tc-wiremock-v3_test.go +++ b/tc-wiremock-v3_test.go @@ -398,7 +398,47 @@ func TestV3Faker(t *testing.T) { } } +func TestV3Auth(t *testing.T) { + ctx := context.Background() + container, err := RunContainer(ctx, + WithImage(defaultV3WireMockImage), + WithMappingFile("v3", filepath.Join("testdata", "200-v3-basic-auth.json")), + ) + if err != nil { + t.Fatal(err) + } + + // Clean up the container after the test is complete + t.Cleanup(func() { + if err := container.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) + + endpoint, err := GetURI(ctx, container) + if err != nil { + t.Fatal(err) + } + + req, err := http.NewRequestWithContext(ctx, "GET", endpoint+"/basic-auth", nil) + if err != nil { + t.Fatal(err) + } + + req.SetBasicAuth("user", "pass") + + statusCode, _, err := sendTestRequest(t, req) + if err != nil { + t.Fatal(err, "Failed to get a response") + } + if statusCode != 200 { + t.Fatalf("expected HTTP-200 but got %d", statusCode) + } +} + func sendTestRequest(t *testing.T, req *http.Request) (int, string, error) { + t.Helper() + resp, err := http.DefaultClient.Do(req) if err != nil { return 0, "", err diff --git a/testdata/200-v3-basic-auth.json b/testdata/200-v3-basic-auth.json new file mode 100644 index 0000000..e7d0c34 --- /dev/null +++ b/testdata/200-v3-basic-auth.json @@ -0,0 +1,13 @@ +{ + "request": { + "method": "GET", + "url": "/basic-auth", + "basicAuth": { + "username": "user", + "password": "pass" + } + }, + "response": { + "status": 200 + } +}