|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func Test_matchScheme(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + domain, pattern string |
| 12 | + expected bool |
| 13 | + }{ |
| 14 | + { |
| 15 | + domain: "http://example.com", |
| 16 | + pattern: "http://example.com", |
| 17 | + expected: true, |
| 18 | + }, |
| 19 | + { |
| 20 | + domain: "https://example.com", |
| 21 | + pattern: "https://example.com", |
| 22 | + expected: true, |
| 23 | + }, |
| 24 | + { |
| 25 | + domain: "http://example.com", |
| 26 | + pattern: "https://example.com", |
| 27 | + expected: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + domain: "https://example.com", |
| 31 | + pattern: "http://example.com", |
| 32 | + expected: false, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + for _, v := range tests { |
| 37 | + assert.Equal(t, v.expected, matchScheme(v.domain, v.pattern)) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func Test_matchSubdomain(t *testing.T) { |
| 42 | + tests := []struct { |
| 43 | + domain, pattern string |
| 44 | + expected bool |
| 45 | + }{ |
| 46 | + { |
| 47 | + domain: "http://aaa.example.com", |
| 48 | + pattern: "http://*.example.com", |
| 49 | + expected: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + domain: "http://bbb.aaa.example.com", |
| 53 | + pattern: "http://*.example.com", |
| 54 | + expected: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + domain: "http://bbb.aaa.example.com", |
| 58 | + pattern: "http://*.aaa.example.com", |
| 59 | + expected: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + domain: "http://aaa.example.com:8080", |
| 63 | + pattern: "http://*.example.com:8080", |
| 64 | + expected: true, |
| 65 | + }, |
| 66 | + |
| 67 | + { |
| 68 | + domain: "http://fuga.hoge.com", |
| 69 | + pattern: "http://*.example.com", |
| 70 | + expected: false, |
| 71 | + }, |
| 72 | + { |
| 73 | + domain: "http://ccc.bbb.example.com", |
| 74 | + pattern: "http://*.aaa.example.com", |
| 75 | + expected: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + domain: `http://1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\ |
| 79 | + .1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\ |
| 80 | + .1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890\ |
| 81 | + .1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.1234567890.example.com`, |
| 82 | + pattern: "http://*.example.com", |
| 83 | + expected: false, |
| 84 | + }, |
| 85 | + { |
| 86 | + domain: "http://ccc.bbb.example.com", |
| 87 | + pattern: "http://example.com", |
| 88 | + expected: false, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + for _, v := range tests { |
| 93 | + assert.Equal(t, v.expected, matchSubdomain(v.domain, v.pattern)) |
| 94 | + } |
| 95 | +} |
0 commit comments