...

Source file src/github.com/golang-jwt/jwt/v5/types_test.go

Documentation: github.com/golang-jwt/jwt/v5

     1  package jwt_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"math"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/golang-jwt/jwt/v5"
    10  )
    11  
    12  func TestNumericDate(t *testing.T) {
    13  	var s struct {
    14  		Iat jwt.NumericDate `json:"iat"`
    15  		Exp jwt.NumericDate `json:"exp"`
    16  	}
    17  
    18  	oldPrecision := jwt.TimePrecision
    19  
    20  	jwt.TimePrecision = time.Microsecond
    21  
    22  	raw := `{"iat":1516239022.000000,"exp":1516239022.123450}`
    23  
    24  	if err := json.Unmarshal([]byte(raw), &s); err != nil {
    25  		t.Fatalf("Unexpected error: %s", err)
    26  	}
    27  
    28  	b, _ := json.Marshal(s)
    29  
    30  	if raw != string(b) {
    31  		t.Errorf("Serialized format of numeric date mismatch. Expecting: %s  Got: %s", raw, string(b))
    32  	}
    33  
    34  	jwt.TimePrecision = oldPrecision
    35  }
    36  
    37  func TestSingleArrayMarshal(t *testing.T) {
    38  	jwt.MarshalSingleStringAsArray = false
    39  
    40  	s := jwt.ClaimStrings{"test"}
    41  	expected := `"test"`
    42  
    43  	b, err := json.Marshal(s)
    44  	if err != nil {
    45  		t.Errorf("Unexpected error: %s", err)
    46  	}
    47  
    48  	if expected != string(b) {
    49  		t.Errorf("Serialized format of string array mismatch. Expecting: %s  Got: %s", expected, string(b))
    50  	}
    51  
    52  	jwt.MarshalSingleStringAsArray = true
    53  
    54  	expected = `["test"]`
    55  
    56  	b, err = json.Marshal(s)
    57  
    58  	if err != nil {
    59  		t.Errorf("Unexpected error: %s", err)
    60  	}
    61  
    62  	if expected != string(b) {
    63  		t.Errorf("Serialized format of string array mismatch. Expecting: %s  Got: %s", expected, string(b))
    64  	}
    65  }
    66  
    67  func TestNumericDate_MarshalJSON(t *testing.T) {
    68  	// Do not run this test in parallel because it's changing
    69  	// global state.
    70  	oldPrecision := jwt.TimePrecision
    71  	t.Cleanup(func() {
    72  		jwt.TimePrecision = oldPrecision
    73  	})
    74  
    75  	tt := []struct {
    76  		in        time.Time
    77  		want      string
    78  		precision time.Duration
    79  	}{
    80  		{time.Unix(5243700879, 0), "5243700879", time.Second},
    81  		{time.Unix(5243700879, 0), "5243700879.000", time.Millisecond},
    82  		{time.Unix(5243700879, 0), "5243700879.000000", time.Microsecond},
    83  		{time.Unix(5243700879, 0), "5243700879.000000000", time.Nanosecond},
    84  		//
    85  		{time.Unix(4239425898, 0), "4239425898", time.Second},
    86  		{time.Unix(4239425898, 0), "4239425898.000", time.Millisecond},
    87  		{time.Unix(4239425898, 0), "4239425898.000000", time.Microsecond},
    88  		{time.Unix(4239425898, 0), "4239425898.000000000", time.Nanosecond},
    89  		//
    90  		{time.Unix(253402271999, 0), "253402271999", time.Second},
    91  		{time.Unix(253402271999, 0), "253402271999.000", time.Millisecond},
    92  		{time.Unix(253402271999, 0), "253402271999.000000", time.Microsecond},
    93  		{time.Unix(253402271999, 0), "253402271999.000000000", time.Nanosecond},
    94  		//
    95  		{time.Unix(0, 1644285000210402000), "1644285000", time.Second},
    96  		{time.Unix(0, 1644285000210402000), "1644285000.210", time.Millisecond},
    97  		{time.Unix(0, 1644285000210402000), "1644285000.210402", time.Microsecond},
    98  		{time.Unix(0, 1644285000210402000), "1644285000.210402000", time.Nanosecond},
    99  		//
   100  		{time.Unix(0, 1644285315063096000), "1644285315", time.Second},
   101  		{time.Unix(0, 1644285315063096000), "1644285315.063", time.Millisecond},
   102  		{time.Unix(0, 1644285315063096000), "1644285315.063096", time.Microsecond},
   103  		{time.Unix(0, 1644285315063096000), "1644285315.063096000", time.Nanosecond},
   104  		// Maximum time that a go time.Time can represent
   105  		{time.Unix(math.MaxInt64, 999999999), "9223372036854775807", time.Second},
   106  		{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999", time.Millisecond},
   107  		{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999999", time.Microsecond},
   108  		{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999999999", time.Nanosecond},
   109  		// Strange precisions
   110  		{time.Unix(math.MaxInt64, 999999999), "9223372036854775807", time.Second},
   111  		{time.Unix(math.MaxInt64, 999999999), "9223372036854775756", time.Minute},
   112  		{time.Unix(math.MaxInt64, 999999999), "9223372036854774016", time.Hour},
   113  		{time.Unix(math.MaxInt64, 999999999), "9223372036854745216", 24 * time.Hour},
   114  	}
   115  
   116  	for i, tc := range tt {
   117  		jwt.TimePrecision = tc.precision
   118  		by, err := jwt.NewNumericDate(tc.in).MarshalJSON()
   119  		if err != nil {
   120  			t.Fatal(err)
   121  		}
   122  		if got := string(by); got != tc.want {
   123  			t.Errorf("[%d]: failed encoding: got %q want %q", i, got, tc.want)
   124  		}
   125  	}
   126  }
   127  

View as plain text