Skip to content

Commit

Permalink
Add test for auth support in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadingole committed Aug 11, 2024
1 parent c114fff commit cd6a401
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tc-wiremock-v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions testdata/200-v3-basic-auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"request": {
"method": "GET",
"url": "/basic-auth",
"basicAuth": {
"username": "user",
"password": "pass"
}
},
"response": {
"status": 200
}
}

0 comments on commit cd6a401

Please sign in to comment.