Skip to content

Commit

Permalink
fixed auth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoeg committed Nov 7, 2023
1 parent 8dd7dc0 commit 6c7cfff
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions cmd/server/http/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package http

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAuthenticate(t *testing.T) {
tt := []struct {
name string
serverToken string
authorization string
status int
}{
{
name: "empty authorization",
serverToken: "token",
authorization: "",
status: http.StatusUnauthorized,
},
{
name: "whitespace token",
serverToken: "token",
authorization: " ",
status: http.StatusUnauthorized,
},
{
name: "non-bearer authorization",
serverToken: "token",
authorization: "non-bearer-token",
status: http.StatusUnauthorized,
},
{
name: "empty bearer authorization",
serverToken: "token",
authorization: "Bearer ",
status: http.StatusUnauthorized,
},
{
name: "whitespace bearer authorization",
serverToken: "token",
authorization: "Bearer ",
status: http.StatusUnauthorized,
},
{
name: "wrong bearer authorization",
serverToken: "token",
authorization: "Bearer another-token",
status: http.StatusUnauthorized,
},
{
name: "correct bearer authorization",
serverToken: "token",
authorization: "Bearer token",
status: http.StatusOK,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
verifier := Verifier{}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Authorization", tc.authorization)
w := httptest.NewRecorder()
verifier.authentication(tc.serverToken)(handler).ServeHTTP(w, req)

assert.Equal(t, tc.status, w.Result().StatusCode, "status code not as expected")
})
}
}

0 comments on commit 6c7cfff

Please sign in to comment.