Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e733304

Browse files
committed
Move test app into test cases
1 parent 1cb650a commit e733304

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

coderd/httpmw/cors_test.go

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ func TestWorkspaceAppCors(t *testing.T) {
1717
regex, err := httpapi.CompileHostnamePattern("*--apps.dev.coder.com")
1818
require.NoError(t, err)
1919

20-
app := httpapi.ApplicationURL{
21-
AppSlugOrPort: "3000",
22-
AgentName: "agent",
23-
WorkspaceName: "ws",
24-
Username: "user",
25-
}
26-
27-
handler := httpmw.WorkspaceAppCors(regex, app)(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
28-
rw.WriteHeader(http.StatusNoContent)
29-
}))
3020
methods := []string{
3121
http.MethodOptions,
3222
http.MethodHead,
@@ -40,26 +30,62 @@ func TestWorkspaceAppCors(t *testing.T) {
4030
tests := []struct {
4131
name string
4232
origin string
33+
app httpapi.ApplicationURL
4334
allowed bool
4435
}{
4536
{
46-
name: "Self",
47-
origin: "https://3000--agent--ws--user--apps.dev.coder.com",
37+
name: "Self",
38+
origin: "https://3000--agent--ws--user--apps.dev.coder.com",
39+
app: httpapi.ApplicationURL{
40+
AppSlugOrPort: "3000",
41+
AgentName: "agent",
42+
WorkspaceName: "ws",
43+
Username: "user",
44+
},
4845
allowed: true,
4946
},
5047
{
51-
name: "SameWorkspace",
52-
origin: "https://8000--agent--ws--user--apps.dev.coder.com",
48+
name: "SameWorkspace",
49+
origin: "https://8000--agent--ws--user--apps.dev.coder.com",
50+
app: httpapi.ApplicationURL{
51+
AppSlugOrPort: "3000",
52+
AgentName: "agent",
53+
WorkspaceName: "ws",
54+
Username: "user",
55+
},
5356
allowed: true,
5457
},
5558
{
56-
name: "SameUser",
57-
origin: "https://8000--agent2--ws2--user--apps.dev.coder.com",
59+
name: "SameUser",
60+
origin: "https://8000--agent2--ws2--user--apps.dev.coder.com",
61+
app: httpapi.ApplicationURL{
62+
AppSlugOrPort: "3000",
63+
AgentName: "agent",
64+
WorkspaceName: "ws",
65+
Username: "user",
66+
},
5867
allowed: true,
5968
},
6069
{
61-
name: "DifferentUser",
62-
origin: "https://3000--agent--ws--user2--apps.dev.coder.com",
70+
name: "DifferentOriginOwner",
71+
origin: "https://3000--agent--ws--user2--apps.dev.coder.com",
72+
app: httpapi.ApplicationURL{
73+
AppSlugOrPort: "3000",
74+
AgentName: "agent",
75+
WorkspaceName: "ws",
76+
Username: "user",
77+
},
78+
allowed: false,
79+
},
80+
{
81+
name: "DifferentHostOwner",
82+
origin: "https://3000--agent--ws--user--apps.dev.coder.com",
83+
app: httpapi.ApplicationURL{
84+
AppSlugOrPort: "3000",
85+
AgentName: "agent",
86+
WorkspaceName: "ws",
87+
Username: "user2",
88+
},
6389
allowed: false,
6490
},
6591
}
@@ -79,6 +105,10 @@ func TestWorkspaceAppCors(t *testing.T) {
79105
r.Header.Set("Access-Control-Request-Method", method)
80106
}
81107

108+
handler := httpmw.WorkspaceAppCors(regex, test.app)(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
109+
rw.WriteHeader(http.StatusNoContent)
110+
}))
111+
82112
handler.ServeHTTP(rw, r)
83113

84114
if test.allowed {

0 commit comments

Comments
 (0)