| 8 | ) |
| 9 | |
| 10 | func TestCheckOrigin(t *testing.T) { |
| 11 | cases := map[string]struct { |
| 12 | origin string |
| 13 | want bool |
| 14 | }{ |
| 15 | "no origin header (CLI/curl)": {origin: "", want: true}, |
| 16 | "localhost origin": {origin: "http://localhost:8080", want: true}, |
| 17 | "127.0.0.1 origin": {origin: "http://127.0.0.1:3000", want: true}, |
| 18 | "localhost no port": {origin: "http://localhost", want: true}, |
| 19 | "external origin": {origin: "http://bad.example.com", want: false}, |
| 20 | "invalid origin": {origin: "://bad-url", want: false}, |
| 21 | } |
| 22 | |
| 23 | for name, tc := range cases { |
| 24 | t.Run(name, func(t *testing.T) { |
| 25 | r := &http.Request{Header: http.Header{}} |
| 26 | if tc.origin != "" { |
| 27 | r.Header.Set("Origin", tc.origin) |
| 28 | } |
| 29 | got := upgrader.CheckOrigin(r) |
| 30 | assert.Equal(t, tc.want, got, name) |
| 31 | }) |
| 32 | } |
| 33 | } |