go test -run -v TestSubdomainMatch
(t *testing.T)
| 59 | |
| 60 | // go test -run -v TestSubdomainMatch |
| 61 | func TestSubdomainMatch(t *testing.T) { |
| 62 | t.Parallel() |
| 63 | |
| 64 | tests := []struct { |
| 65 | name string |
| 66 | sub subdomain |
| 67 | origin string |
| 68 | expected bool |
| 69 | }{ |
| 70 | { |
| 71 | name: "match with different scheme", |
| 72 | sub: subdomain{prefix: "http://api.", suffix: "example.com"}, |
| 73 | origin: "https://api.service.example.com", |
| 74 | expected: false, |
| 75 | }, |
| 76 | { |
| 77 | name: "match with different scheme", |
| 78 | sub: subdomain{prefix: "https://", suffix: "example.com"}, |
| 79 | origin: "http://api.service.example.com", |
| 80 | expected: false, |
| 81 | }, |
| 82 | { |
| 83 | name: "match with valid subdomain", |
| 84 | sub: subdomain{prefix: "https://", suffix: "example.com"}, |
| 85 | origin: "https://api.service.example.com", |
| 86 | expected: true, |
| 87 | }, |
| 88 | { |
| 89 | name: "match with valid nested subdomain", |
| 90 | sub: subdomain{prefix: "https://", suffix: "example.com"}, |
| 91 | origin: "https://1.2.api.service.example.com", |
| 92 | expected: true, |
| 93 | }, |
| 94 | |
| 95 | { |
| 96 | name: "no match with invalid prefix", |
| 97 | sub: subdomain{prefix: "https://abc.", suffix: "example.com"}, |
| 98 | origin: "https://service.example.com", |
| 99 | expected: false, |
| 100 | }, |
| 101 | { |
| 102 | name: "no match with invalid suffix", |
| 103 | sub: subdomain{prefix: "https://", suffix: "example.com"}, |
| 104 | origin: "https://api.example.org", |
| 105 | expected: false, |
| 106 | }, |
| 107 | { |
| 108 | name: "no match with empty origin", |
| 109 | sub: subdomain{prefix: "https://", suffix: "example.com"}, |
| 110 | origin: "", |
| 111 | expected: false, |
| 112 | }, |
| 113 | { |
| 114 | name: "no match with malformed subdomain", |
| 115 | sub: subdomain{prefix: "https://", suffix: "example.com"}, |
| 116 | origin: "https://evil.comexample.com", |
| 117 | expected: false, |
| 118 | }, |