...

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

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

     1  package jwt
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  func Test_joinErrors(t *testing.T) {
    10  	type args struct {
    11  		errs []error
    12  	}
    13  	tests := []struct {
    14  		name        string
    15  		args        args
    16  		wantErrors  []error
    17  		wantMessage string
    18  	}{
    19  		{
    20  			name: "multiple errors",
    21  			args: args{
    22  				errs: []error{ErrTokenNotValidYet, ErrTokenExpired},
    23  			},
    24  			wantErrors:  []error{ErrTokenNotValidYet, ErrTokenExpired},
    25  			wantMessage: "token is not valid yet, token is expired",
    26  		},
    27  	}
    28  	for _, tt := range tests {
    29  		t.Run(tt.name, func(t *testing.T) {
    30  			err := joinErrors(tt.args.errs...)
    31  			for _, wantErr := range tt.wantErrors {
    32  				if !errors.Is(err, wantErr) {
    33  					t.Errorf("joinErrors() error = %v, does not contain %v", err, wantErr)
    34  				}
    35  			}
    36  
    37  			if err.Error() != tt.wantMessage {
    38  				t.Errorf("joinErrors() error.Error() = %v, wantMessage %v", err, tt.wantMessage)
    39  			}
    40  		})
    41  	}
    42  }
    43  
    44  func Test_newError(t *testing.T) {
    45  	type args struct {
    46  		message string
    47  		err     error
    48  		more    []error
    49  	}
    50  	tests := []struct {
    51  		name        string
    52  		args        args
    53  		wantErrors  []error
    54  		wantMessage string
    55  	}{
    56  		{
    57  			name:        "single error",
    58  			args:        args{message: "something is wrong", err: ErrTokenMalformed},
    59  			wantMessage: "token is malformed: something is wrong",
    60  			wantErrors:  []error{ErrTokenMalformed},
    61  		},
    62  		{
    63  			name:        "two errors",
    64  			args:        args{message: "something is wrong", err: ErrTokenMalformed, more: []error{io.ErrUnexpectedEOF}},
    65  			wantMessage: "token is malformed: something is wrong: unexpected EOF",
    66  			wantErrors:  []error{ErrTokenMalformed},
    67  		},
    68  		{
    69  			name:        "two errors, no detail",
    70  			args:        args{message: "", err: ErrTokenInvalidClaims, more: []error{ErrTokenExpired}},
    71  			wantMessage: "token has invalid claims: token is expired",
    72  			wantErrors:  []error{ErrTokenInvalidClaims, ErrTokenExpired},
    73  		},
    74  		{
    75  			name:        "two errors, no detail and join error",
    76  			args:        args{message: "", err: ErrTokenInvalidClaims, more: []error{joinErrors(ErrTokenExpired, ErrTokenNotValidYet)}},
    77  			wantMessage: "token has invalid claims: token is expired, token is not valid yet",
    78  			wantErrors:  []error{ErrTokenInvalidClaims, ErrTokenExpired, ErrTokenNotValidYet},
    79  		},
    80  	}
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			err := newError(tt.args.message, tt.args.err, tt.args.more...)
    84  			for _, wantErr := range tt.wantErrors {
    85  				if !errors.Is(err, wantErr) {
    86  					t.Errorf("newError() error = %v, does not contain %v", err, wantErr)
    87  				}
    88  			}
    89  
    90  			if err.Error() != tt.wantMessage {
    91  				t.Errorf("newError() error.Error() = %v, wantMessage %v", err, tt.wantMessage)
    92  			}
    93  		})
    94  	}
    95  }
    96  

View as plain text