-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hoeg
committed
Nov 7, 2023
1 parent
8dd7dc0
commit 6c7cfff
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} | ||
} |