-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathiri_test.go
405 lines (381 loc) · 8.33 KB
/
iri_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package activitypub
import (
"errors"
"fmt"
"net/url"
"reflect"
"testing"
)
func TestIRI_GetLink(t *testing.T) {
val := "http://example.com"
u := IRI(val)
if u.GetLink() != IRI(val) {
t.Errorf("IRI %q should equal %q", u, val)
}
}
func TestIRI_String(t *testing.T) {
val := "http://example.com"
u := IRI(val)
if u.String() != val {
t.Errorf("IRI %q should equal %q", u, val)
}
}
func TestIRI_GetID(t *testing.T) {
i := IRI("http://example.com")
if id := i.GetID(); !id.IsValid() || id != ID(i) {
t.Errorf("ID %q (%T) should equal %q (%T)", id, id, i, ID(i))
}
}
func TestIRI_GetType(t *testing.T) {
i := IRI("http://example.com")
if i.GetType() != IRIType {
t.Errorf("Invalid type for %T object %s, expected %s", i, i.GetType(), IRIType)
}
}
func TestIRI_IsLink(t *testing.T) {
i := IRI("http://example.com")
if i.IsLink() != true {
t.Errorf("%T.IsLink() returned %t, expected %t", i, i.IsLink(), true)
}
}
func TestIRI_IsObject(t *testing.T) {
i := IRI("http://example.com")
if i.IsObject() {
t.Errorf("%T.IsObject() returned %t, expected %t", i, i.IsObject(), false)
}
ii := IRI([]byte("https://example.com"))
if ii.IsObject() {
t.Errorf("%T.IsObject() returned %t, expected %t", ii, ii.IsObject(), false)
}
iii := &ii
if iii.IsObject() {
t.Errorf("%T.IsObject() returned %t, expected %t", iii, iii.IsObject(), false)
}
}
func TestIRI_UnmarshalJSON(t *testing.T) {
val := "http://example.com"
i := IRI("")
err := i.UnmarshalJSON([]byte(val))
if err != nil {
t.Error(err)
}
if val != i.String() {
t.Errorf("%T invalid value after Unmarshal %q, expected %q", i, i, val)
}
}
func TestIRI_MarshalJSON(t *testing.T) {
value := []byte("http://example.com")
i := IRI(value)
v, err := i.MarshalJSON()
if err != nil {
t.Error(err)
}
expected := fmt.Sprintf("%q", value)
if expected != string(v) {
t.Errorf("Invalid value after MarshalJSON: %s, expected %s", v, expected)
}
}
func TestFlattenToIRI(t *testing.T) {
t.Skipf("TODO")
}
func TestIRI_URL(t *testing.T) {
tests := []struct {
name string
i IRI
want *url.URL
wantErr bool
}{
{
name: "empty",
i: "",
want: nil,
wantErr: true, // empty IRI
},
{
name: "example with fragment",
i: "https://example.com/#fragment",
want: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "/",
Fragment: "fragment",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.i.URL()
if (err != nil) != tt.wantErr {
t.Errorf("URL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("URL() got = %v, want %v", got, tt.want)
}
})
}
}
func TestIRIs_Contains(t *testing.T) {
t.Skipf("TODO")
}
func TestIRI_Contains(t *testing.T) {
t.Skip("TODO")
}
func TestIRI_IsCollection(t *testing.T) {
t.Skip("TODO")
}
func TestIRIs_UnmarshalJSON(t *testing.T) {
type args struct {
d []byte
}
tests := []struct {
name string
args args
obj IRIs
want IRIs
err error
}{
{
name: "empty",
args: args{[]byte{'{', '}'}},
want: nil,
err: nil,
},
{
name: "IRI",
args: args{[]byte("\"http://example.com\"")},
want: IRIs{IRI("http://example.com")},
err: nil,
},
{
name: "IRIs",
args: args{[]byte(fmt.Sprintf("[%q, %q, %q]", "http://example.com", "http://example.net", "http://example.org"))},
want: IRIs{
IRI("http://example.com"),
IRI("http://example.net"),
IRI("http://example.org"),
},
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.obj.UnmarshalJSON(tt.args.d)
if (err != nil && tt.err == nil) || (err == nil && tt.err != nil) {
if !errors.Is(err, tt.err) {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.err)
}
return
}
if !assertDeepEquals(t.Errorf, tt.obj, tt.want) {
t.Errorf("UnmarshalJSON() got = %#v, want %#v", tt.obj, tt.want)
}
})
}
}
func TestIRIs_MarshalJSON(t *testing.T) {
value1 := []byte("http://example.com")
value2 := []byte("http://example.net")
value3 := []byte("http://example.org")
i := IRIs{
IRI(value1),
IRI(value2),
IRI(value3),
}
v, err := i.MarshalJSON()
if err != nil {
t.Error(err)
}
expected := fmt.Sprintf("[%q, %q, %q]", value1, value2, value3)
if expected == string(v) {
t.Errorf("Invalid value after MarshalJSON: %s, expected %s", v, expected)
}
}
func TestIRI_AddPath(t *testing.T) {
t.Skip("TODO")
}
func TestIRI_ItemMatches(t *testing.T) {
t.Skip("TODO")
}
func TestIRI_GobDecode(t *testing.T) {
tests := []struct {
name string
i IRI
data []byte
wantErr bool
}{
{
name: "empty",
i: "",
data: []byte{},
wantErr: false,
},
{
name: "some iri",
i: "https://example.com",
data: gobValue([]byte("https://example.com")),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.i.GobDecode(tt.data); (err != nil) != tt.wantErr {
t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestIRI_GobEncode(t *testing.T) {
tests := []struct {
name string
i IRI
want []byte
wantErr bool
}{
{
name: "empty",
i: "",
want: []byte{},
wantErr: false,
},
{
name: "some iri",
i: "https://example.com",
want: []byte("https://example.com"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.i.GobEncode()
if (err != nil) != tt.wantErr {
t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GobEncode() got = %v, want %v", got, tt.want)
}
})
}
}
func TestIRI_Equals(t *testing.T) {
type args struct {
with IRI
check bool
}
tests := []struct {
name string
i IRI
args args
want bool
}{
{
name: "just host",
i: "http://example.com",
args: args{
with: IRI("http://example.com"),
check: true,
},
want: true,
},
{
name: "host and path",
i: "http://example.com/ana/are/mere",
args: args{
with: IRI("http://example.com/ana/are/mere"),
check: true,
},
want: true,
},
{
name: "different schemes check scheme",
i: "https://example.com/ana/are/mere",
args: args{
with: IRI("http://example.com/ana/are/mere"),
check: true,
},
want: false,
},
{
name: "different schemes, don't check scheme",
i: "https://example.com/ana/are/mere",
args: args{
with: IRI("http://example.com/ana/are/mere"),
check: false,
},
want: true,
},
{
name: "same host different scheme, same query - different order",
i: "https://example.com?ana=mere&foo=bar",
args: args{
with: "http://example.com?foo=bar&ana=mere",
check: false,
},
want: true,
},
{
name: "same host, different scheme and same path, same query different order",
i: "http://example.com/ana/are/mere?foo=bar&ana=mere",
args: args{
with: "https://example.com/ana/are/mere?ana=mere&foo=bar",
check: false,
},
want: true,
},
{
name: "same host different scheme, same query",
i: "https://example.com?ana=mere",
args: args{
with: "http://example.com?ana=mere",
check: false,
},
want: true,
},
{
name: "different host same scheme",
i: "http://example1.com",
args: args{
with: "http://example.com",
check: true,
},
want: false,
},
{
name: "same host, same scheme and different path",
i: "same host, same scheme and different path",
args: args{
with: "http://example.com/ana/are/mere",
check: true,
},
want: false,
},
{
name: "same host same scheme, different query key",
i: "http://example.com?ana1=mere",
args: args{
with: "http://example.com?ana=mere",
check: false,
},
want: false,
},
{
name: "same host same scheme, different query value",
i: "http://example.com?ana=mere",
args: args{
with: "http://example.com?ana=mere1",
check: false,
},
// This was true in the url.Parse implementation
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.i.Equals(tt.args.with, tt.args.check); got != tt.want {
t.Errorf("Equals() = %v, want %v", got, tt.want)
}
})
}
}