Skip to content

Commit 5fccd0b

Browse files
authored
chore(cors): add CORS tests
1 parent 57087b2 commit 5fccd0b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

cmd/arduino-app-cli/daemon/daemon.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ func httpHandler(ctx context.Context, cfg config.Configuration, daemonPort, vers
6161
Origins: []string{
6262
"wails://wails",
6363
"wails://wails.localhost:*",
64-
"http://wails.localhost",
6564
"http://wails.localhost:*",
6665
"http://localhost:*",
6766
"https://localhost:*",

internal/e2e/daemon/cors_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)