Skip to content

Commit

Permalink
Improve parse tests (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored May 9, 2020
1 parent 378bc1f commit eebcc12
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func ParseAndVerify(raw []byte, verifier Verifier) (*Token, error) {
if token.Header().Algorithm != verifier.Algorithm() {
return nil, ErrAlgorithmMismatch
}
if err := verifier.Verify(token.payload, token.signature); err != nil {
if err := verifier.Verify(token.Payload(), token.Signature()); err != nil {
return nil, err
}
return token, nil
Expand Down
48 changes: 42 additions & 6 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,48 @@ import (
)

func TestParseString(t *testing.T) {
f := func(got string, header Header, payload, signature string) {
f := func(token string, header Header, payload, signature string) {
t.Helper()

_, err := ParseString(got)
if err == nil {
t.Error("got nil want nil")
tk, err := ParseString(token)
if err != nil {
t.Errorf("want nil, got %#v", err)
}
if tk.Header() != header {
t.Errorf("want %#v, got %#v", header, tk.Header())
}
headerStr := toBase64(headerString(header))
if string(tk.RawHeader()) != headerStr {
t.Errorf("want %#v, got %#v", headerStr, string(tk.RawHeader()))
}
if string(tk.Payload()) != payload {
t.Errorf("want %#v, got %#v", payload, string(tk.Payload()))
}
sign := toBase64(string(tk.Signature()))
if sign != signature {
t.Errorf("want %#v, got %#v", signature, sign)
}
}

// TODO
f(``, Header{}, "", "")
f(
`eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ.t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo`,
Header{
Algorithm: HS256,
Type: "JWT",
},
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ",
"t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo",
)
f(
`eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCIsImN0eSI6InRva2VuIn0.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ.t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo`,
Header{
Algorithm: HS512,
Type: "JWT",
ContentType: "token",
},
"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCIsImN0eSI6InRva2VuIn0.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ",
"t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo",
)
}

func TestParseMalformed(t *testing.T) {
Expand All @@ -35,3 +66,8 @@ func TestParseMalformed(t *testing.T) {
f(`x/z.ab_c.xyz`)
f(`ab_c.xyz.xyz`)
}

func headerString(header Header) string {
raw, _ := header.MarshalJSON()
return string(raw)
}

0 comments on commit eebcc12

Please sign in to comment.