Skip to content

Commit 2bee027

Browse files
authored
Merge pull request #2243 from gofr-dev/tests/auth-middleware
2 parents d9c45ed + 0a1d435 commit 2bee027

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
func main() {
99
a := gofr.New()
1010

11-
// For Basic Auth
11+
//For Basic Auth
1212
//setupBasicAuth(a)
1313

1414
// For APIKey Auth
15-
//setupAPIKeyAuth(a)
15+
setupAPIKeyAuth(a)
1616

1717
//For OAuth
1818
//a.EnableOAuth("<JWKS-Endpoint>", 10)
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package main
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"gofr.dev/pkg/gofr/testutil"
6+
"net/http"
7+
"testing"
8+
"time"
9+
)
10+
11+
func Test_setupAPIKeyAuthFailed(t *testing.T) {
12+
serverConfigs := testutil.NewServerConfigs(t)
13+
14+
// Run main() in a goroutine to avoid blocking
15+
go main()
16+
17+
// Allow time for server to start
18+
time.Sleep(100 * time.Millisecond)
19+
20+
client := &http.Client{Timeout: 200 * time.Millisecond}
21+
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")
27+
28+
resp, err := client.Do(req)
29+
if err != nil {
30+
t.Fatalf("Error making request: %v", err)
31+
}
32+
defer resp.Body.Close()
33+
34+
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
35+
})
36+
}
37+
38+
func Test_setupAPIKeyAuthSuccess(t *testing.T) {
39+
serverConfigs := testutil.NewServerConfigs(t)
40+
41+
// Run main() in a goroutine to avoid blocking
42+
go main()
43+
44+
// Allow time for server to start
45+
time.Sleep(100 * time.Millisecond)
46+
47+
client := &http.Client{Timeout: 200 * time.Millisecond}
48+
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")
54+
55+
resp, err := client.Do(req)
56+
if err != nil {
57+
t.Fatalf("Error making request: %v", err)
58+
}
59+
defer resp.Body.Close()
60+
61+
assert.Equal(t, http.StatusOK, resp.StatusCode)
62+
})
63+
64+
}
65+
66+
//func encodeBasicAuthorization(t *testing.T, arg string) string {
67+
// t.Helper()
68+
//
69+
// data := []byte(arg)
70+
//
71+
// dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
72+
//
73+
// base64.StdEncoding.Encode(dst, data)
74+
//
75+
// s := "Basic " + string(dst)
76+
//
77+
// return s
78+
//}
79+
80+
//func Test_setupBasicAuthSuccess(t *testing.T) {
81+
// serverConfigs := testutil.NewServerConfigs(t)
82+
//
83+
// app := gofr.New()
84+
//
85+
// setupBasicAuth(app)
86+
//
87+
// app.GET("/basic-auth-success", func(_ *gofr.Context) (any, error) {
88+
// return "success", nil
89+
// })
90+
//
91+
// go app.Run()
92+
//
93+
// time.Sleep(100 * time.Millisecond)
94+
//
95+
// var netClient = &http.Client{
96+
// Timeout: 200 * time.Millisecond,
97+
// }
98+
//
99+
// req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet,
100+
// serverConfigs.HTTPHost + "/basic-auth-success", http.NoBody)
101+
//
102+
// req.Header.Add("Authorization", encodeBasicAuthorization(t, "username:password"))
103+
//
104+
// // Send the request and check for successful response
105+
// resp, err := netClient.Do(req)
106+
// if err != nil {
107+
// t.Errorf("error while making HTTP request in Test_BasicAuthMiddleware. err: %v", err)
108+
// return
109+
// }
110+
//
111+
// defer resp.Body.Close()
112+
//
113+
// assert.Equal(t, http.StatusOK, resp.StatusCode, "Test_setupBasicAuthSuccess")
114+
//}
115+
116+
//func Test_setupBasicAuthFailed(t *testing.T) {
117+
// serverConfigs := testutil.NewServerConfigs(t)
118+
//
119+
// app := gofr.New()
120+
//
121+
// setupBasicAuth(app)
122+
//
123+
// app.GET("/basic-auth-failure", func(_ *gofr.Context) (any, error) {
124+
// return "success", nil
125+
// })
126+
//
127+
// go app.Run()
128+
//
129+
// time.Sleep(100 * time.Millisecond)
130+
//
131+
// var netClient = &http.Client{
132+
// Timeout: 200 * time.Millisecond,
133+
// }
134+
//
135+
// req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet,
136+
// serverConfigs.HTTPHost + "/basic-auth-failure", http.NoBody)
137+
//
138+
// req.Header.Add("Authorization", encodeBasicAuthorization(t, "username"))
139+
//
140+
// // Send the request and check for successful response
141+
// resp, err := netClient.Do(req)
142+
// if err != nil {
143+
// t.Errorf("error while making HTTP request in Test_BasicAuthMiddleware. err: %v", err)
144+
// return
145+
// }
146+
//
147+
// defer resp.Body.Close()
148+
//
149+
// assert.Equal(t, http.StatusUnauthorized, resp.StatusCode, "Test_setupBasicAuthFailed")
150+
//}

0 commit comments

Comments
 (0)