jwt-go
jwt-go copied to clipboard
v4 preview: *jwt.Time does not always unmarshal back to its initial value.
This test fails when using v4.0.0-preview1
:
func TestJWTTime(t *testing.T) {
when, _ := time.Parse(time.RFC3339Nano, "2017-07-31T12:59:59.999999999Z")
expected := jwt.At(when)
js, _ := json.Marshal(expected)
var got jwt.Time
json.Unmarshal(js, &got)
t.Logf("Initial time.Time: %s", when.Format(time.RFC3339Nano))
t.Logf("Expected *jwt.Time: %s", expected.Format(time.RFC3339Nano))
t.Logf("JSON: %s", js)
t.Logf("Unmarshaled *jwt.Time: %s", got.UTC().Format(time.RFC3339Nano))
if expected.Format(time.RFC3339Nano) != got.UTC().Format(time.RFC3339Nano) {
t.Fatalf("Did not get expected %T after unmarshaling", expected)
}
}
If you run it (playground), you'll get this failure:
=== RUN TestJWTTime
TestJWTTime: prog.go:18: Initial time.Time: 2017-07-31T12:59:59.999999999Z
TestJWTTime: prog.go:19: Expected *jwt.Time: 2017-07-31T12:59:59.999999Z
TestJWTTime: prog.go:20: JSON: 1501505999.999999
TestJWTTime: prog.go:21: Unmarshaled *jwt.Time: 2017-07-31T12:59:59.999998Z
TestJWTTime: prog.go:24: Did not get expected *jwt.Time after unmarshaling
--- FAIL: TestJWTTime (0.00s)
FAIL
I ran into this while doing a test where I needed a JWT and found that my parsed JWT didn't match my initial JWT. Tracked it back to this.
This problem is caused by the deserialization calculation precision.