Skip to content

Commit 6c2fe70

Browse files
committed
updates tests inline with proposed requests
1 parent 78b10b0 commit 6c2fe70

File tree

1 file changed

+34
-51
lines changed

1 file changed

+34
-51
lines changed

examples/using-http-auth-middleware/main_test.go

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"github.com/stretchr/testify/assert"
5-
"gofr.dev/pkg/gofr"
65
"gofr.dev/pkg/gofr/testutil"
76
"net/http"
87
"testing"
@@ -12,72 +11,56 @@ import (
1211
func Test_setupAPIKeyAuthFailed(t *testing.T) {
1312
serverConfigs := testutil.NewServerConfigs(t)
1413

15-
app := gofr.New()
14+
// Run main() in a goroutine to avoid blocking
15+
go main()
1616

17-
setupAPIKeyAuth(app)
17+
// Allow time for server to start
18+
time.Sleep(100 * time.Millisecond)
1819

19-
app.GET("/api-key-failure", func(_ *gofr.Context) (any, error) {
20-
return "success", nil
21-
})
22-
23-
go app.Run()
24-
25-
time.Sleep(10 * time.Millisecond)
26-
27-
var netClient = &http.Client{
28-
Timeout: 20 * time.Millisecond,
29-
}
30-
31-
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet,
32-
serverConfigs.HTTPHost+"/api-key-failure", http.NoBody)
33-
34-
req.Header.Set("X-Api-Key", "test-key")
20+
client := &http.Client{Timeout: 200 * time.Millisecond}
3521

36-
// Send the request and check for successful response
37-
resp, err := netClient.Do(req)
38-
if err != nil {
39-
t.Errorf("error while making HTTP request in Test_APIKeyAuthMiddleware. err: %v", err)
40-
return
41-
}
22+
// Test invalid API key
23+
t.Run("Invalid API Key", func(t *testing.T) {
24+
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet,
25+
serverConfigs.HTTPHost+"/test-auth", http.NoBody)
26+
req.Header.Set("X-Api-Key", "test-key")
4227

43-
defer resp.Body.Close()
28+
resp, err := client.Do(req)
29+
if err != nil {
30+
t.Fatalf("Error making request: %v", err)
31+
}
32+
defer resp.Body.Close()
4433

45-
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode, "Test_setupAPIKeyAuthFailed")
34+
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
35+
})
4636
}
4737

4838
func Test_setupAPIKeyAuthSuccess(t *testing.T) {
4939
serverConfigs := testutil.NewServerConfigs(t)
5040

51-
app := gofr.New()
52-
53-
setupAPIKeyAuth(app)
54-
55-
app.GET("/api-key-success", func(_ *gofr.Context) (any, error) {
56-
return "success", nil
57-
})
58-
59-
go app.Run()
41+
// Run main() in a goroutine to avoid blocking
42+
go main()
6043

61-
time.Sleep(10 * time.Millisecond)
44+
// Allow time for server to start
45+
time.Sleep(100 * time.Millisecond)
6246

63-
var netClient = &http.Client{
64-
Timeout: 20 * time.Millisecond,
65-
}
47+
client := &http.Client{Timeout: 200 * time.Millisecond}
6648

67-
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet,
68-
serverConfigs.HTTPHost+"/api-key-success", http.NoBody)
69-
req.Header.Set("X-Api-Key", "valid-api-key")
49+
// Test valid API key
50+
t.Run("Valid API Key", func(t *testing.T) {
51+
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet,
52+
serverConfigs.HTTPHost+"/test-auth", http.NoBody)
53+
req.Header.Set("X-Api-Key", "valid-api-key")
7054

71-
// Send the request and check for successful response
72-
resp, err := netClient.Do(req)
73-
if err != nil {
74-
t.Errorf("error while making HTTP request in Test_APIKeyAuthMiddleware. err: %v", err)
75-
return
76-
}
55+
resp, err := client.Do(req)
56+
if err != nil {
57+
t.Fatalf("Error making request: %v", err)
58+
}
59+
defer resp.Body.Close()
7760

78-
defer resp.Body.Close()
61+
assert.Equal(t, http.StatusOK, resp.StatusCode)
62+
})
7963

80-
assert.Equal(t, http.StatusOK, resp.StatusCode, "Test_setupAPIKeyAuthSuccess")
8164
}
8265

8366
//func encodeBasicAuthorization(t *testing.T, arg string) string {

0 commit comments

Comments
 (0)