|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestCanonicalIP(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + ip string |
| 11 | + want string |
| 12 | + }{ |
| 13 | + {"::ffff:83.90.47.30", "83.90.47.30"}, |
| 14 | + {"83.90.47.30", "83.90.47.30"}, |
| 15 | + {"fe80::676d:489:1f16:2c1", "fe80::676d:489:1f16:2c1"}, |
| 16 | + } |
| 17 | + for _, tt := range tests { |
| 18 | + t.Run(tt.ip, func(t *testing.T) { |
| 19 | + if got := canonicalIP(tt.ip); got != tt.want { |
| 20 | + t.Errorf("canonicalIP() = %v, want %v", got, tt.want) |
| 21 | + } |
| 22 | + }) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestVerifyDate(t *testing.T) { |
| 27 | + now := time.Date(2023, 1, 13, 17, 51, 0, 0, time.UTC) |
| 28 | + tests := []struct { |
| 29 | + date string |
| 30 | + now time.Time |
| 31 | + want bool |
| 32 | + }{ |
| 33 | + { |
| 34 | + date: "2023-01-13T17:51:09.583Z", |
| 35 | + want: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + date: "2023-01-13T17:59:09.583Z", |
| 39 | + want: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + date: "2020-01-01T17:59:09.583Z", |
| 43 | + want: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + date: "invalid date", |
| 47 | + want: false, |
| 48 | + }, |
| 49 | + } |
| 50 | + for _, tt := range tests { |
| 51 | + t.Run(tt.date, func(t *testing.T) { |
| 52 | + if got := VerifyDate(tt.date, now); got != tt.want { |
| 53 | + t.Errorf("verifyDate() = %v, want %v", got, tt.want) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestSign(t *testing.T) { |
| 60 | + secret := []byte("secret") |
| 61 | + fields := []string{"GET", "2023-01-13T17:51:09.583Z", "/api/v1/urls"} |
| 62 | + want := "R0VUCjIwMjMtMDEtMTNUMTc6NTE6MDkuNTgzWgovYXBpL3YxL3VybHP55m4Xm2dHrlQQj4L4reizwl12/TCv3mw5WCLFMBlhaQ==" |
| 63 | + if got := sign(secret, fields...); got != want { |
| 64 | + t.Errorf("sign() = %v, want %v", got, want) |
| 65 | + } |
| 66 | +} |
0 commit comments