|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCors(t *testing.T) { |
| 12 | + httpClient := GetHttpclient(t) |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + origin string |
| 16 | + shouldAllow bool |
| 17 | + }{ |
| 18 | + {"wails://wails", true}, |
| 19 | + {"wails://wails.localhost", true}, |
| 20 | + {"wails://wails.localhost:8000", true}, |
| 21 | + |
| 22 | + {"http://wails.localhost", true}, |
| 23 | + {"http://wails.localhost:8001", true}, |
| 24 | + |
| 25 | + {"http://localhost", true}, |
| 26 | + {"http://localhost:8002", true}, |
| 27 | + {"https://localhost", true}, |
| 28 | + |
| 29 | + // not valid, should not be allowed |
| 30 | + {"http://randomsite.com", false}, |
| 31 | + } |
| 32 | + |
| 33 | + for _, tc := range tests { |
| 34 | + t.Run(tc.origin, func(t *testing.T) { |
| 35 | + addHeaders := func(ctx context.Context, req *http.Request) error { |
| 36 | + req.Header.Set("origin", tc.origin) |
| 37 | + return nil |
| 38 | + } |
| 39 | + resp, err := httpClient.GetVersions(t.Context(), addHeaders) |
| 40 | + require.NoError(t, err) |
| 41 | + defer resp.Body.Close() |
| 42 | + |
| 43 | + require.Equal(t, 200, resp.StatusCode) |
| 44 | + if tc.shouldAllow { |
| 45 | + require.Equal(t, tc.origin, resp.Header.Get("Access-Control-Allow-Origin")) |
| 46 | + } else { |
| 47 | + require.Empty(t, resp.Header.Get("Access-Control-Allow-Origin")) |
| 48 | + } |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
0 commit comments