This repository has been archived by the owner on Mar 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_test.go
194 lines (186 loc) · 6.6 KB
/
encode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package jwt
import (
"reflect"
"testing"
)
func Test_encode(t *testing.T) {
type args struct {
data interface{}
}
tests := []struct {
name string
args args
wantOut []byte
wantErr bool
}{
{"String", args{"Hello world!"}, []byte("IkhlbGxvIHdvcmxkISI"), false},
{"Map", args{map[string]string{"name": "test", "use": "testing"}}, []byte("eyJuYW1lIjoidGVzdCIsInVzZSI6InRlc3RpbmcifQ"), false},
{"ShouldFail", args{func(test int) bool {
return test == 20
}}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOut, err := encode(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotOut, tt.wantOut) {
t.Errorf("encode() = %v, want %v", gotOut, tt.wantOut)
}
})
}
}
func Test_join(t *testing.T) {
type args struct {
b [][]byte
}
tests := []struct {
name string
args args
wantResult []byte
}{
{"Zero", args{nil}, nil},
{"Single", args{[][]byte{[]byte("test")}}, []byte("test")},
{"Two", args{[][]byte{[]byte("test1"), []byte("test2")}}, []byte("test1.test2")},
{"Three", args{[][]byte{[]byte("test1"), []byte("test2"), []byte("test3")}}, []byte("test1.test2.test3")},
{"Empty", args{[][]byte{[]byte("test1"), []byte(""), []byte("test3")}}, []byte("test1..test3")},
{"Empty_Beginning", args{[][]byte{[]byte(""), []byte("test2"), []byte("test3")}}, []byte(".test2.test3")},
{"Empty_End", args{[][]byte{[]byte("test1"), []byte("test2"), []byte("")}}, []byte("test1.test2.")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotResult := join(tt.args.b...); !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("join() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}
func Test_b64encode(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
wantOut []byte
}{
{"Data", args{[]byte("{\"testing\": \"JSON\", \"with\": 3.0, \"elements\": true}")}, []byte("eyJ0ZXN0aW5nIjogIkpTT04iLCAid2l0aCI6IDMuMCwgImVsZW1lbnRzIjogdHJ1ZX0")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOut := b64encode(tt.args.data); !reflect.DeepEqual(gotOut, tt.wantOut) {
t.Errorf("b64encode() = %v, want %v", gotOut, tt.wantOut)
}
})
}
}
func TestNew(t *testing.T) {
type args struct {
content interface{}
}
tests := []struct {
name string
args args
want JWT
wantErr bool
}{
{"Normal", args{map[string]interface{}{"test": "normal"}}, JWT{Header{Typ: "JWT", Alg: "EdDSA"}, map[string]interface{}{"test": "normal"}, nil}, false},
{"ContentString", args{"test"}, JWT{}, true},
{"ContentInt", args{123456789}, JWT{}, true},
{"ContentIntMap", args{map[int]string{123: "test"}}, JWT{}, true},
{"ContentStringMapOfStrings", args{map[string]string{"test": "normal"}}, JWT{Header{Typ: "JWT", Alg: "EdDSA"}, map[string]string{"test": "normal"}, nil}, false},
{"ContentStruct", args{Header{Typ: "none", Alg: "none"}}, JWT{Header{Typ: "JWT", Alg: "EdDSA"}, Header{Typ: "none", Alg: "none"}, nil}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.content)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewWithKeyID(t *testing.T) {
type args struct {
content interface{}
keyID string
}
tests := []struct {
name string
args args
wantOut JWT
wantErr bool
}{
{"Normal", args{map[string]interface{}{"test": "normal"}, "unique_key_id"}, JWT{Header{Typ: "JWT", Alg: "EdDSA", Kid: "unique_key_id"}, map[string]interface{}{"test": "normal"}, nil}, false},
{"InvalidContent", args{"test", "unique_key_id"}, JWT{}, true},
{"InvalidKeyID", args{map[string]interface{}{"test": "normal"}, ""}, JWT{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOut, err := NewWithKeyID(tt.args.content, tt.args.keyID)
if (err != nil) != tt.wantErr {
t.Errorf("NewWithKeyID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotOut, tt.wantOut) {
t.Errorf("NewWithKeyID() = %v, want %v", gotOut, tt.wantOut)
}
})
}
}
func TestNewWithKeyIDAndKeyURL(t *testing.T) {
type args struct {
content interface{}
keyID string
keyURL string
}
tests := []struct {
name string
args args
wantOut JWT
wantErr bool
}{
{"Normal", args{map[string]interface{}{"test": "normal"}, "unique_key_id", "https://example.com/get_keys"}, JWT{Header{Typ: "JWT", Alg: "EdDSA", Kid: "unique_key_id", Jku: "https://example.com/get_keys"}, map[string]interface{}{"test": "normal"}, nil}, false},
{"ContentInvalid", args{"test", "unique_key_id", "https://example.com/get_keys"}, JWT{}, true},
{"EmptyKeyIDNotAllowed", args{map[string]interface{}{"test": "normal"}, "", "https://example.com/get_keys"}, JWT{}, true},
{"KeyURLTooShort", args{map[string]interface{}{"test": "normal"}, "unique_key_id", "https://a.b"}, JWT{}, true},
{"KeyURLMustBeHTTPS", args{map[string]interface{}{"test": "normal"}, "unique_key_id", "ftps://example.com/get_keys"}, JWT{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOut, err := NewWithKeyIDAndKeyURL(tt.args.content, tt.args.keyID, tt.args.keyURL)
if (err != nil) != tt.wantErr {
t.Errorf("NewWithKeyIDAndKeyURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotOut, tt.wantOut) {
t.Errorf("NewWithKeyIDAndKeyURL() = %v, want %v", gotOut, tt.wantOut)
}
})
}
}
func Test_encodeHeader(t *testing.T) {
tests := []struct {
name string
h Header
want []byte
}{
{"Normal", Header{Typ: "JWT", Alg: "EdDSA"}, []byte("eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSJ9")},
{"WithKeyID", Header{Typ: "JWT", Alg: "EdDSA", Kid: "unique_key_id"}, []byte("eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6InVuaXF1ZV9rZXlfaWQifQ")},
{"WithKeyURL", Header{Typ: "JWT", Alg: "EdDSA", Jku: "https://example.com/get_key"}, []byte("eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImprdSI6Imh0dHBzOi8vZXhhbXBsZS5jb20vZ2V0X2tleSJ9")},
{"WithKeyIDAndURL", Header{Typ: "JWT", Alg: "EdDSA", Kid: "unique_key_id", Jku: "https://example.com/get_key"}, []byte("eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSIsImtpZCI6InVuaXF1ZV9rZXlfaWQiLCJqa3UiOiJodHRwczovL2V4YW1wbGUuY29tL2dldF9rZXkifQ")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := encodeHeader(tt.h); !reflect.DeepEqual(got, tt.want) {
t.Errorf("encodeHeader() = %v, want %v", got, tt.want)
}
})
}
}