forked from steinfletcher/apitest-jsonpath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_test.go
36 lines (29 loc) · 1.13 KB
/
jwt_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
package jsonpath_test
import (
"net/http"
"testing"
"github.com/steinfletcher/apitest"
jsonpath "github.com/steinfletcher/apitest-jsonpath"
)
const jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
func TestApiTest_JWT(t *testing.T) {
handler := http.NewServeMux()
handler.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Authorization", jwt)
w.WriteHeader(http.StatusOK)
})
apitest.New().
Handler(handler).
Get("/hello").
Expect(t).
Assert(jsonpath.JWTPayloadEqual(fromAuthHeader, `$.name`, "John Doe")).
Assert(jsonpath.JWTPayloadEqual(fromAuthHeader, `$.sub`, "1234567890")).
Assert(jsonpath.JWTPayloadEqual(fromAuthHeader, `$.iat`, float64(1516239022))).
Assert(jsonpath.JWTHeaderEqual(fromAuthHeader, `$.alg`, "HS256")).
Assert(jsonpath.JWTHeaderEqual(fromAuthHeader, `$.typ`, "JWT")).
End()
}
func fromAuthHeader(response *http.Response) (string, error) {
return response.Header.Get("Authorization"), nil
}