MCPcopy
hub / github.com/golang-jwt/jwt / TestParseRequest

Function TestParseRequest

request/request_test.go:57–104  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

55}
56
57func TestParseRequest(t *testing.T) {
58 // load keys from disk
59 privateKey := test.LoadRSAPrivateKeyFromDisk("../test/sample_key")
60 publicKey := test.LoadRSAPublicKeyFromDisk("../test/sample_key.pub")
61 keyfunc := func(*jwt.Token) (any, error) {
62 return publicKey, nil
63 }
64
65 // Bearer token request
66 for _, data := range requestTestData {
67 // Make token from claims
68 tokenString := test.MakeSampleToken(data.claims, jwt.SigningMethodRS256, privateKey)
69
70 // Make query string
71 for k, vv := range data.query {
72 for i, v := range vv {
73 if strings.Contains(v, "%v") {
74 data.query[k][i] = fmt.Sprintf(v, tokenString)
75 }
76 }
77 }
78
79 // Make request from test struct
80 r, _ := http.NewRequest("GET", fmt.Sprintf("/?%v", data.query.Encode()), nil)
81 for k, v := range data.headers {
82 if strings.Contains(v, "%v") {
83 r.Header.Set(k, fmt.Sprintf(v, tokenString))
84 } else {
85 r.Header.Set(k, tokenString)
86 }
87 }
88 token, err := ParseFromRequestWithClaims(r, data.extractor, jwt.MapClaims{}, keyfunc)
89
90 if token == nil {
91 t.Errorf("[%v] Token was not found: %v", data.name, err)
92 continue
93 }
94 if !reflect.DeepEqual(data.claims, token.Claims) {
95 t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims)
96 }
97 if data.valid && err != nil {
98 t.Errorf("[%v] Error while verifying token: %v", data.name, err)
99 }
100 if !data.valid && err == nil {
101 t.Errorf("[%v] Invalid token passed validation", data.name)
102 }
103 }
104}

Callers

nothing calls this directly

Calls 5

LoadRSAPublicKeyFromDiskFunction · 0.92
MakeSampleTokenFunction · 0.92
SetMethod · 0.80

Tested by

no test coverage detected