Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ch3nnn authored and kevwan committed Aug 27, 2024
1 parent 7d33874 commit a01f817
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rest/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ Verbose: true
{
priority: true,
jwt: jwtSetting{
enabled: true,
enabled: true,
tokenKeys: []string{"Token", "X-Token"},
},
signature: signatureSetting{},
routes: []Route{{
Expand Down
26 changes: 26 additions & 0 deletions rest/handler/authhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ func TestAuthHandler(t *testing.T) {
assert.Equal(t, "content", resp.Body.String())
}

func TestAuthHandler_WithTokenKeys(t *testing.T) {
const key = "B63F477D-BBA3-4E52-96D3-C0034C27694A"
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
token, err := buildToken(key, map[string]any{
"key": "value",
}, 3600)
assert.Nil(t, err)
req.Header.Set("X-Token", token)
handler := Authorize(key, WithTokenKeys([]string{"Token", "X-Token"}))(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Test", "test")
_, err := w.Write([]byte("content"))
assert.Nil(t, err)

flusher, ok := w.(http.Flusher)
assert.True(t, ok)
flusher.Flush()
}))

resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "test", resp.Header().Get("X-Test"))
assert.Equal(t, "content", resp.Body.String())
}

func TestAuthHandlerWithPrevSecret(t *testing.T) {
const (
key = "14F17379-EB8F-411B-8F12-6929002DCA76"
Expand Down

0 comments on commit a01f817

Please sign in to comment.