-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_test.go
70 lines (65 loc) · 1.85 KB
/
http_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bearerware
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/dgrijalva/jwt-go"
)
func TestJWTFromHeader(t *testing.T) {
var (
jwtKeyFunc = func(token *jwt.Token) (interface{}, error) {
return []byte(" "), nil
}
tests = []struct {
req *http.Request
signingMethod jwt.SigningMethod
err error
}{
{
req: &http.Request{
Header: http.Header{
"Authorization": {
"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJpYXQiOm51bGwsImV4cCI6bnVsbCwiYXVkIjoiIiwic3ViIjoiIn0.IlffGJz3IyFX1ADQ6-jOTQ_0D-K0kuKq5SpB_oirCrk",
},
},
},
signingMethod: jwt.SigningMethodHS256,
err: nil,
},
{
req: &http.Request{
Header: http.Header{
"Authorization": {
"Beare eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJpYXQiOm51bGwsImV4cCI6bnVsbCwiYXVkIjoiIiwic3ViIjoiIn0.IlffGJz3IyFX1ADQ6-jOTQ_0D-K0kuKq5SpB_oirCrk",
},
},
},
signingMethod: jwt.SigningMethodHS256,
err: errors.New(`Bearer realm=Restricted,error="invalid_request",error_description="Authorization header format must be Bearer {token}"`),
},
{
req: &http.Request{},
signingMethod: jwt.SigningMethodHS256,
err: errors.New(`Bearer realm=Restricted`),
},
}
)
for _, test := range tests {
token, err := JWTFromHeader(test.req, jwtKeyFunc, test.signingMethod)
if e := testJWTFrom(token, err, test.err); e != nil {
t.Error(e)
}
}
}
func TestWriteHeader(t *testing.T) {
w := httptest.NewRecorder()
WriteAuthError(w, errors.New("foo"))
if w.Code != http.StatusUnauthorized {
t.Fatalf("Expected %d, got %d", http.StatusUnauthorized, w.Code)
}
authStatus := w.Header().Get(http.CanonicalHeaderKey("WWW-Authenticate"))
if authStatus != "foo" {
t.Fatalf("Expected 'foo', Got %s", authStatus)
}
}