| 1 |
package activitypub |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestIRI_GetLink(t *testing.T) { |
| 12 |
val := "http://example.com" |
| 13 |
u := IRI(val) |
| 14 |
if u.GetLink() != IRI(val) { |
| 15 |
t.Errorf("IRI %q should equal %q", u, val) |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
func TestIRI_String(t *testing.T) { |
| 20 |
val := "http://example.com" |
| 21 |
u := IRI(val) |
| 22 |
if u.String() != val { |
| 23 |
t.Errorf("IRI %q should equal %q", u, val) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestIRI_GetID(t *testing.T) { |
| 28 |
i := IRI("http://example.com") |
| 29 |
if id := i.GetID(); !id.IsValid() || id != ID(i) { |
| 30 |
t.Errorf("ID %q (%T) should equal %q (%T)", id, id, i, ID(i)) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestIRI_GetType(t *testing.T) { |
| 35 |
i := IRI("http://example.com") |
| 36 |
if !i.Match(IRIType) { |
| 37 |
t.Errorf("Invalid type for %T object %s, expected %s", i, i.GetType(), IRIType) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestIRI_IsLink(t *testing.T) { |
| 42 |
i := IRI("http://example.com") |
| 43 |
if i.IsLink() != true { |
| 44 |
t.Errorf("%T.IsLink() returned %t, expected %t", i, i.IsLink(), true) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestIRI_IsObject(t *testing.T) { |
| 49 |
i := IRI("http://example.com") |
| 50 |
if i.IsObject() { |
| 51 |
t.Errorf("%T.IsObject() returned %t, expected %t", i, i.IsObject(), false) |
| 52 |
} |
| 53 |
ii := IRI([]byte("https://example.com")) |
| 54 |
if ii.IsObject() { |
| 55 |
t.Errorf("%T.IsObject() returned %t, expected %t", ii, ii.IsObject(), false) |
| 56 |
} |
| 57 |
iii := &ii |
| 58 |
if iii.IsObject() { |
| 59 |
t.Errorf("%T.IsObject() returned %t, expected %t", iii, iii.IsObject(), false) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestIRI_UnmarshalJSON(t *testing.T) { |
| 64 |
val := "http://example.com" |
| 65 |
i := IRI("") |
| 66 |
|
| 67 |
err := i.UnmarshalJSON([]byte(val)) |
| 68 |
if err != nil { |
| 69 |
t.Error(err) |
| 70 |
} |
| 71 |
if val != i.String() { |
| 72 |
t.Errorf("%T invalid value after Unmarshal %q, expected %q", i, i, val) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestIRI_MarshalJSON(t *testing.T) { |
| 77 |
value := []byte("http://example.com") |
| 78 |
i := IRI(value) |
| 79 |
|
| 80 |
v, err := i.MarshalJSON() |
| 81 |
if err != nil { |
| 82 |
t.Error(err) |
| 83 |
} |
| 84 |
expected := fmt.Sprintf("%q", value) |
| 85 |
if expected != string(v) { |
| 86 |
t.Errorf("Invalid value after MarshalJSON: %s, expected %s", v, expected) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestFlattenToIRI(t *testing.T) { |
| 91 |
t.Skipf("TODO") |
| 92 |
} |
| 93 |
|
| 94 |
func TestIRI_URL(t *testing.T) { |
| 95 |
tests := []struct { |
| 96 |
name string |
| 97 |
i IRI |
| 98 |
want *url.URL |
| 99 |
wantErr bool |
| 100 |
}{ |
| 101 |
{ |
| 102 |
name: "empty", |
| 103 |
i: "", |
| 104 |
want: nil, |
| 105 |
wantErr: true, |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "example with fragment", |
| 109 |
i: "https://example.com/#fragment", |
| 110 |
want: &url.URL{ |
| 111 |
Scheme: "https", |
| 112 |
Host: "example.com", |
| 113 |
Path: "/", |
| 114 |
Fragment: "fragment", |
| 115 |
}, |
| 116 |
wantErr: false, |
| 117 |
}, |
| 118 |
} |
| 119 |
for _, tt := range tests { |
| 120 |
t.Run(tt.name, func(t *testing.T) { |
| 121 |
got, err := tt.i.URL() |
| 122 |
if (err != nil) != tt.wantErr { |
| 123 |
t.Errorf("URL() error = %v, wantErr %v", err, tt.wantErr) |
| 124 |
return |
| 125 |
} |
| 126 |
if !reflect.DeepEqual(got, tt.want) { |
| 127 |
t.Errorf("URL() got = %v, want %v", got, tt.want) |
| 128 |
} |
| 129 |
}) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestIRIs_Contains(t *testing.T) { |
| 134 |
t.Skipf("TODO") |
| 135 |
} |
| 136 |
|
| 137 |
func TestIRI_Contains(t *testing.T) { |
| 138 |
type args struct { |
| 139 |
what IRI |
| 140 |
checkScheme bool |
| 141 |
} |
| 142 |
tests := []struct { |
| 143 |
name string |
| 144 |
i IRI |
| 145 |
args args |
| 146 |
want bool |
| 147 |
}{ |
| 148 |
{ |
| 149 |
name: "empty", |
| 150 |
want: true, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "equal", |
| 154 |
i: "http://example.com", |
| 155 |
args: args{what: "http://example.com"}, |
| 156 |
want: true, |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "i has /", |
| 160 |
i: "http://example.com/", |
| 161 |
args: args{what: "http://example.com"}, |
| 162 |
want: true, |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "what has /", |
| 166 |
i: "http://example.com", |
| 167 |
args: args{what: "http://example.com/"}, |
| 168 |
want: true, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "different protocol w/ no check", |
| 172 |
i: "http://example.com", |
| 173 |
args: args{what: "https://example.com/"}, |
| 174 |
want: true, |
| 175 |
}, |
| 176 |
{ |
| 177 |
name: "different protocol", |
| 178 |
i: "http://example.com", |
| 179 |
args: args{what: "https://example.com/", checkScheme: true}, |
| 180 |
want: false, |
| 181 |
}, |
| 182 |
} |
| 183 |
for _, tt := range tests { |
| 184 |
t.Run(tt.name, func(t *testing.T) { |
| 185 |
if got := tt.i.Contains(tt.args.what, tt.args.checkScheme); got != tt.want { |
| 186 |
t.Errorf("Contains() = %v, want %v", got, tt.want) |
| 187 |
} |
| 188 |
}) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func TestIRI_IsCollection(t *testing.T) { |
| 193 |
t.Skip("TODO") |
| 194 |
} |
| 195 |
|
| 196 |
func TestIRIs_UnmarshalJSON(t *testing.T) { |
| 197 |
type args struct { |
| 198 |
d []byte |
| 199 |
} |
| 200 |
tests := []struct { |
| 201 |
name string |
| 202 |
args args |
| 203 |
obj IRIs |
| 204 |
want IRIs |
| 205 |
err error |
| 206 |
}{ |
| 207 |
{ |
| 208 |
name: "empty", |
| 209 |
args: args{[]byte{'{', '}'}}, |
| 210 |
want: nil, |
| 211 |
err: nil, |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "IRI", |
| 215 |
args: args{[]byte("\"http://example.com\"")}, |
| 216 |
want: IRIs{IRI("http://example.com")}, |
| 217 |
err: nil, |
| 218 |
}, |
| 219 |
{ |
| 220 |
name: "IRIs", |
| 221 |
args: args{[]byte(fmt.Sprintf("[%q, %q, %q]", "http://example.com", "http://example.net", "http://example.org"))}, |
| 222 |
want: IRIs{ |
| 223 |
IRI("http://example.com"), |
| 224 |
IRI("http://example.net"), |
| 225 |
IRI("http://example.org"), |
| 226 |
}, |
| 227 |
err: nil, |
| 228 |
}, |
| 229 |
} |
| 230 |
for _, tt := range tests { |
| 231 |
t.Run(tt.name, func(t *testing.T) { |
| 232 |
err := tt.obj.UnmarshalJSON(tt.args.d) |
| 233 |
if (err != nil && tt.err == nil) || (err == nil && tt.err != nil) { |
| 234 |
if !errors.Is(err, tt.err) { |
| 235 |
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.err) |
| 236 |
} |
| 237 |
return |
| 238 |
} |
| 239 |
if !assertDeepEquals(t.Errorf, tt.obj, tt.want) { |
| 240 |
t.Errorf("UnmarshalJSON() got = %#v, want %#v", tt.obj, tt.want) |
| 241 |
} |
| 242 |
}) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func TestIRIs_MarshalJSON(t *testing.T) { |
| 247 |
value1 := []byte("http://example.com") |
| 248 |
value2 := []byte("http://example.net") |
| 249 |
value3 := []byte("http://example.org") |
| 250 |
i := IRIs{ |
| 251 |
IRI(value1), |
| 252 |
IRI(value2), |
| 253 |
IRI(value3), |
| 254 |
} |
| 255 |
|
| 256 |
v, err := i.MarshalJSON() |
| 257 |
if err != nil { |
| 258 |
t.Error(err) |
| 259 |
} |
| 260 |
expected := fmt.Sprintf("[%q, %q, %q]", value1, value2, value3) |
| 261 |
if expected == string(v) { |
| 262 |
t.Errorf("Invalid value after MarshalJSON: %s, expected %s", v, expected) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestIRI_AddPath(t *testing.T) { |
| 267 |
t.Skip("TODO") |
| 268 |
} |
| 269 |
|
| 270 |
func TestIRI_ItemMatches(t *testing.T) { |
| 271 |
t.Skip("TODO") |
| 272 |
} |
| 273 |
|
| 274 |
func TestIRI_GobDecode(t *testing.T) { |
| 275 |
tests := []struct { |
| 276 |
name string |
| 277 |
i IRI |
| 278 |
data []byte |
| 279 |
wantErr bool |
| 280 |
}{ |
| 281 |
{ |
| 282 |
name: "empty", |
| 283 |
i: "", |
| 284 |
data: []byte{}, |
| 285 |
wantErr: false, |
| 286 |
}, |
| 287 |
{ |
| 288 |
name: "some iri", |
| 289 |
i: "https://example.com", |
| 290 |
data: gobValue([]byte("https://example.com")), |
| 291 |
wantErr: false, |
| 292 |
}, |
| 293 |
} |
| 294 |
for _, tt := range tests { |
| 295 |
t.Run(tt.name, func(t *testing.T) { |
| 296 |
if err := tt.i.GobDecode(tt.data); (err != nil) != tt.wantErr { |
| 297 |
t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr) |
| 298 |
} |
| 299 |
}) |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
func TestIRI_GobEncode(t *testing.T) { |
| 304 |
tests := []struct { |
| 305 |
name string |
| 306 |
i IRI |
| 307 |
want []byte |
| 308 |
wantErr bool |
| 309 |
}{ |
| 310 |
{ |
| 311 |
name: "empty", |
| 312 |
i: "", |
| 313 |
want: []byte{}, |
| 314 |
wantErr: false, |
| 315 |
}, |
| 316 |
{ |
| 317 |
name: "some iri", |
| 318 |
i: "https://example.com", |
| 319 |
want: []byte("https://example.com"), |
| 320 |
wantErr: false, |
| 321 |
}, |
| 322 |
} |
| 323 |
for _, tt := range tests { |
| 324 |
t.Run(tt.name, func(t *testing.T) { |
| 325 |
got, err := tt.i.GobEncode() |
| 326 |
if (err != nil) != tt.wantErr { |
| 327 |
t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr) |
| 328 |
return |
| 329 |
} |
| 330 |
if !reflect.DeepEqual(got, tt.want) { |
| 331 |
t.Errorf("GobEncode() got = %v, want %v", got, tt.want) |
| 332 |
} |
| 333 |
}) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func TestIRI_Equals(t *testing.T) { |
| 338 |
type args struct { |
| 339 |
with IRI |
| 340 |
check bool |
| 341 |
} |
| 342 |
tests := []struct { |
| 343 |
name string |
| 344 |
i IRI |
| 345 |
args args |
| 346 |
want bool |
| 347 |
}{ |
| 348 |
{ |
| 349 |
name: "just host", |
| 350 |
i: "http://example.com", |
| 351 |
args: args{ |
| 352 |
with: IRI("http://example.com"), |
| 353 |
check: true, |
| 354 |
}, |
| 355 |
want: true, |
| 356 |
}, |
| 357 |
{ |
| 358 |
name: "host and path", |
| 359 |
i: "http://example.com/ana/are/mere", |
| 360 |
args: args{ |
| 361 |
with: IRI("http://example.com/ana/are/mere"), |
| 362 |
check: true, |
| 363 |
}, |
| 364 |
want: true, |
| 365 |
}, |
| 366 |
{ |
| 367 |
name: "different schemes check scheme", |
| 368 |
i: "https://example.com/ana/are/mere", |
| 369 |
args: args{ |
| 370 |
with: IRI("http://example.com/ana/are/mere"), |
| 371 |
check: true, |
| 372 |
}, |
| 373 |
want: false, |
| 374 |
}, |
| 375 |
{ |
| 376 |
name: "different schemes, don't check scheme", |
| 377 |
i: "https://example.com/ana/are/mere", |
| 378 |
args: args{ |
| 379 |
with: IRI("http://example.com/ana/are/mere"), |
| 380 |
check: false, |
| 381 |
}, |
| 382 |
want: true, |
| 383 |
}, |
| 384 |
{ |
| 385 |
name: "same host different scheme, same query - different order", |
| 386 |
i: "https://example.com?ana=mere&foo=bar", |
| 387 |
args: args{ |
| 388 |
with: "http://example.com?foo=bar&ana=mere", |
| 389 |
check: false, |
| 390 |
}, |
| 391 |
want: true, |
| 392 |
}, |
| 393 |
{ |
| 394 |
name: "same host, different scheme and same path, same query different order", |
| 395 |
i: "http://example.com/ana/are/mere?foo=bar&ana=mere", |
| 396 |
args: args{ |
| 397 |
with: "https://example.com/ana/are/mere?ana=mere&foo=bar", |
| 398 |
check: false, |
| 399 |
}, |
| 400 |
want: true, |
| 401 |
}, |
| 402 |
{ |
| 403 |
name: "same host different scheme, same query", |
| 404 |
i: "https://example.com?ana=mere", |
| 405 |
args: args{ |
| 406 |
with: "http://example.com?ana=mere", |
| 407 |
check: false, |
| 408 |
}, |
| 409 |
want: true, |
| 410 |
}, |
| 411 |
{ |
| 412 |
name: "different host same scheme", |
| 413 |
i: "http://example1.com", |
| 414 |
args: args{ |
| 415 |
with: "http://example.com", |
| 416 |
check: true, |
| 417 |
}, |
| 418 |
want: false, |
| 419 |
}, |
| 420 |
{ |
| 421 |
name: "same host, same scheme and different path", |
| 422 |
i: "same host, same scheme and different path", |
| 423 |
args: args{ |
| 424 |
with: "http://example.com/ana/are/mere", |
| 425 |
check: true, |
| 426 |
}, |
| 427 |
want: false, |
| 428 |
}, |
| 429 |
{ |
| 430 |
name: "same host same scheme, different query key", |
| 431 |
i: "http://example.com?ana1=mere", |
| 432 |
args: args{ |
| 433 |
with: "http://example.com?ana=mere", |
| 434 |
check: false, |
| 435 |
}, |
| 436 |
want: false, |
| 437 |
}, |
| 438 |
{ |
| 439 |
name: "same host same scheme, different query value", |
| 440 |
i: "http://example.com?ana=mere", |
| 441 |
args: args{ |
| 442 |
with: "http://example.com?ana=mere1", |
| 443 |
check: false, |
| 444 |
}, |
| 445 |
|
| 446 |
want: false, |
| 447 |
}, |
| 448 |
{ |
| 449 |
name: "i has /", |
| 450 |
i: "http://example.com/", |
| 451 |
args: args{with: "http://example.com"}, |
| 452 |
want: true, |
| 453 |
}, |
| 454 |
{ |
| 455 |
name: "what has /", |
| 456 |
i: "http://example.com", |
| 457 |
args: args{with: "http://example.com/"}, |
| 458 |
want: true, |
| 459 |
}, |
| 460 |
} |
| 461 |
for _, tt := range tests { |
| 462 |
t.Run(tt.name, func(t *testing.T) { |
| 463 |
if got := tt.i.Equals(tt.args.with, tt.args.check); got != tt.want { |
| 464 |
t.Errorf("Equal() = %v, want %v", got, tt.want) |
| 465 |
} |
| 466 |
}) |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
func TestIRIs_Remove(t *testing.T) { |
| 471 |
tests := []struct { |
| 472 |
name string |
| 473 |
items IRIs |
| 474 |
toRemove ItemCollection |
| 475 |
remaining IRIs |
| 476 |
}{ |
| 477 |
{ |
| 478 |
name: "empty_collection_nil_item", |
| 479 |
items: IRIs{}, |
| 480 |
toRemove: nil, |
| 481 |
remaining: IRIs{}, |
| 482 |
}, |
| 483 |
{ |
| 484 |
name: "empty_collection_non_nil_item", |
| 485 |
items: IRIs{}, |
| 486 |
toRemove: ItemCollection{}, |
| 487 |
remaining: IRIs{}, |
| 488 |
}, |
| 489 |
{ |
| 490 |
name: "non_empty_collection_nil_item", |
| 491 |
items: IRIs{"test"}, |
| 492 |
toRemove: nil, |
| 493 |
remaining: IRIs{"test"}, |
| 494 |
}, |
| 495 |
{ |
| 496 |
name: "non_empty_collection_non_contained_item_empty_ID", |
| 497 |
items: IRIs{"test"}, |
| 498 |
toRemove: ItemCollection{}, |
| 499 |
remaining: IRIs{"test"}, |
| 500 |
}, |
| 501 |
{ |
| 502 |
name: "non_empty_collection_non_contained_item", |
| 503 |
items: IRIs{"test"}, |
| 504 |
toRemove: ItemCollection{IRI("test123")}, |
| 505 |
remaining: IRIs{"test"}, |
| 506 |
}, |
| 507 |
{ |
| 508 |
name: "non_empty_collection_just_contained_item", |
| 509 |
items: IRIs{"test"}, |
| 510 |
toRemove: ItemCollection{IRI("test")}, |
| 511 |
remaining: IRIs{}, |
| 512 |
}, |
| 513 |
{ |
| 514 |
name: "non_empty_collection_contained_item_first_pos", |
| 515 |
items: IRIs{"test", "test123"}, |
| 516 |
toRemove: ItemCollection{IRI("test")}, |
| 517 |
remaining: IRIs{"test123"}, |
| 518 |
}, |
| 519 |
{ |
| 520 |
name: "non_empty_collection_contained_item_not_first_pos", |
| 521 |
items: IRIs{"test123", "test", "test321"}, |
| 522 |
toRemove: ItemCollection{IRI("test")}, |
| 523 |
remaining: IRIs{"test123", "test321"}, |
| 524 |
}, |
| 525 |
{ |
| 526 |
name: "non_empty_collection_contained_item_last_pos", |
| 527 |
items: IRIs{"test123", "test"}, |
| 528 |
toRemove: ItemCollection{&Object{ID: "test"}}, |
| 529 |
remaining: IRIs{"test123"}, |
| 530 |
}, |
| 531 |
} |
| 532 |
for _, tt := range tests { |
| 533 |
t.Run(tt.name, func(t *testing.T) { |
| 534 |
tt.items.Remove(tt.toRemove...) |
| 535 |
|
| 536 |
if tt.remaining.Count() != tt.items.Count() { |
| 537 |
t.Errorf("Post Remove() %T has count %d different than expected %d", tt.items, tt.items.Count(), tt.remaining.Count()) |
| 538 |
} |
| 539 |
for _, it := range tt.toRemove { |
| 540 |
if tt.items.Contains(it) { |
| 541 |
t.Errorf("Post Remove() was still able to find %s in %T Items %v", it.GetLink(), tt.items, tt.items) |
| 542 |
} |
| 543 |
} |
| 544 |
for _, it := range tt.remaining { |
| 545 |
if !tt.items.Contains(it) { |
| 546 |
t.Errorf("Post Remove() unable to find %s in %T Items %v", it.GetLink(), tt.items, tt.items) |
| 547 |
} |
| 548 |
} |
| 549 |
}) |
| 550 |
} |
| 551 |
} |