|
| 1 | +package media |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestMatchCORSOrigin(t *testing.T) { |
| 8 | + cases := []struct { |
| 9 | + allowed []string |
| 10 | + origin string |
| 11 | + expected string |
| 12 | + }{ |
| 13 | + { |
| 14 | + allowed: []string{"https://example.com"}, |
| 15 | + origin: "https://example.com", |
| 16 | + expected: "https://example.com", |
| 17 | + }, |
| 18 | + { |
| 19 | + allowed: []string{"https://example2.com", "https://example.com"}, |
| 20 | + origin: "https://example.com", |
| 21 | + expected: "https://example.com", |
| 22 | + }, |
| 23 | + { |
| 24 | + allowed: []string{"*"}, |
| 25 | + origin: "https://example.com", |
| 26 | + expected: "https://example.com", |
| 27 | + }, |
| 28 | + { |
| 29 | + allowed: []string{""}, |
| 30 | + origin: "https://example.com", |
| 31 | + expected: "", |
| 32 | + }, |
| 33 | + { |
| 34 | + allowed: []string{}, |
| 35 | + origin: "", |
| 36 | + expected: "", |
| 37 | + }, |
| 38 | + { |
| 39 | + allowed: []string{"https://example.com"}, |
| 40 | + origin: "", |
| 41 | + expected: "", |
| 42 | + }, |
| 43 | + { |
| 44 | + allowed: []string{}, |
| 45 | + origin: "https://example.com", |
| 46 | + expected: "", |
| 47 | + }, |
| 48 | + { |
| 49 | + allowed: []string{"http://example.com"}, |
| 50 | + origin: "https://example.com", |
| 51 | + expected: "", |
| 52 | + }, |
| 53 | + { |
| 54 | + allowed: []string{"https://example.com"}, |
| 55 | + origin: "http://example.com", |
| 56 | + expected: "", |
| 57 | + }, |
| 58 | + { |
| 59 | + allowed: []string{"http://example.com:8000"}, |
| 60 | + origin: "http://example.com:8000", |
| 61 | + expected: "http://example.com:8000", |
| 62 | + }, |
| 63 | + { |
| 64 | + allowed: []string{"http://localhost:8090"}, |
| 65 | + origin: "http://localhost:8090", |
| 66 | + expected: "http://localhost:8090", |
| 67 | + }, |
| 68 | + { |
| 69 | + allowed: []string{"http://localhost:8090"}, |
| 70 | + origin: "http://localhost:8080", |
| 71 | + expected: "", |
| 72 | + }, |
| 73 | + { |
| 74 | + allowed: []string{"https://example.com"}, |
| 75 | + origin: "https://sub.example.com", |
| 76 | + expected: "", |
| 77 | + }, |
| 78 | + { |
| 79 | + allowed: []string{"https://*.example.com"}, |
| 80 | + origin: "https://sub.example.com", |
| 81 | + expected: "https://sub.example.com", |
| 82 | + }, |
| 83 | + { |
| 84 | + allowed: []string{"https://*.example.com"}, |
| 85 | + origin: "https://pre.sub.example.com", |
| 86 | + expected: "", |
| 87 | + }, |
| 88 | + { |
| 89 | + allowed: []string{"https://*.example.com", "https://*.sub.example.com"}, |
| 90 | + origin: "https://pre.sub.example.com", |
| 91 | + expected: "https://pre.sub.example.com", |
| 92 | + }, |
| 93 | + { |
| 94 | + allowed: []string{"https://*.*.example.com"}, |
| 95 | + origin: "https://pre.sub.example.com", |
| 96 | + expected: "https://pre.sub.example.com", |
| 97 | + }, |
| 98 | + { |
| 99 | + allowed: []string{"https://*.sub.example.com"}, |
| 100 | + origin: "https://pre.asd.example.com", |
| 101 | + expected: "", |
| 102 | + }, |
| 103 | + { |
| 104 | + allowed: []string{"https://pre.*.example.com"}, |
| 105 | + origin: "https://pre.sub.example.com", |
| 106 | + expected: "https://pre.sub.example.com", |
| 107 | + }, |
| 108 | + { |
| 109 | + allowed: []string{"https://*.*.*.example.com"}, |
| 110 | + origin: "https://www.pre.sub.example.com", |
| 111 | + expected: "https://www.pre.sub.example.com", |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + for _, tc := range cases { |
| 116 | + result := matchCORSOrigin(tc.allowed, tc.origin) |
| 117 | + if result != tc.expected { |
| 118 | + t.Errorf("Match CORS origin got wrong result. Expected %s, got %s", tc.expected, result) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments