Skip to content

Commit 977da7e

Browse files
committed
Added test for CORS and app config updates
1 parent f774465 commit 977da7e

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

internal/app/app.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package app
55

66
import (
7+
"cmp"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -510,6 +511,29 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
510511
http.Error(w, a.reloadError.Error(), http.StatusInternalServerError)
511512
return
512513
}
514+
515+
if a.appConfig.CORS.Setting == "strict" || a.appConfig.CORS.Setting == "lax" {
516+
origin := "*"
517+
if a.appConfig.CORS.Setting == "strict" {
518+
origin = getRequestUrl(r)
519+
}
520+
if r.Method == http.MethodOptions {
521+
w.Header().Set("Access-Control-Allow-Origin", cmp.Or(a.appConfig.CORS.AllowOrigin, origin))
522+
w.Header().Set("Access-Control-Allow-Methods", a.appConfig.CORS.AllowMethods)
523+
w.Header().Set("Access-Control-Allow-Headers", a.appConfig.CORS.AllowHeaders)
524+
w.Header().Set("Access-Control-Allow-Credentials", a.appConfig.CORS.AllowCredentials)
525+
w.Header().Set("Access-Control-Max-Age", a.appConfig.CORS.MaxAge)
526+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
527+
w.Header().Set("Content-Length", "0")
528+
w.WriteHeader(http.StatusNoContent)
529+
return
530+
} else {
531+
w.Header().Set("Access-Control-Allow-Origin", cmp.Or(a.appConfig.CORS.AllowOrigin, origin))
532+
w.Header().Set("Access-Control-Allow-Methods", a.appConfig.CORS.AllowMethods)
533+
w.Header().Set("Access-Control-Allow-Headers", a.appConfig.CORS.AllowHeaders)
534+
}
535+
}
536+
513537
a.appRouter.ServeHTTP(w, r)
514538
}
515539

internal/app/setup.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package app
55

66
import (
77
"bytes"
8-
"cmp"
98
"encoding/json"
109
"errors"
1110
"fmt"
@@ -607,28 +606,6 @@ func (a *App) addProxyConfig(count int, router *chi.Mux, proxyDef *starlarkstruc
607606
permsHandler := func(p *httputil.ReverseProxy) http.Handler {
608607
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
609608

610-
if a.appConfig.CORS.Setting == "strict" || a.appConfig.CORS.Setting == "lax" {
611-
origin := "*"
612-
if a.appConfig.CORS.Setting == "strict" {
613-
origin = getRequestUrl(r)
614-
}
615-
if r.Method == http.MethodOptions {
616-
w.Header().Set("Access-Control-Allow-Origin", cmp.Or(a.appConfig.CORS.AllowOrigin, origin))
617-
w.Header().Set("Access-Control-Allow-Methods", a.appConfig.CORS.AllowMethods)
618-
w.Header().Set("Access-Control-Allow-Headers", a.appConfig.CORS.AllowHeaders)
619-
w.Header().Set("Access-Control-Allow-Credentials", a.appConfig.CORS.AllowCredentials)
620-
w.Header().Set("Access-Control-Max-Age", a.appConfig.CORS.MaxAge)
621-
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
622-
w.Header().Set("Content-Length", "0")
623-
w.WriteHeader(http.StatusNoContent)
624-
return
625-
} else {
626-
w.Header().Set("Access-Control-Allow-Origin", cmp.Or(a.appConfig.CORS.AllowOrigin, origin))
627-
w.Header().Set("Access-Control-Allow-Methods", a.appConfig.CORS.AllowMethods)
628-
w.Header().Set("Access-Control-Allow-Headers", a.appConfig.CORS.AllowHeaders)
629-
}
630-
}
631-
632609
// If write API, check if preview/stage app is allowed access
633610
isWriteReques := r.Method == http.MethodPost || r.Method == http.MethodPut || r.Method == http.MethodDelete
634611
if isWriteReques {

tests/commander/test_versions.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,36 @@ tests:
160160
exit-code: 1
161161
stderr: "error: version commands not supported for dev app"
162162

163+
# Test CORS
164+
versions0300: # default is strict origin
165+
command: curl -Iu "admin:qwerty" localhost:25222/versions_local1 | grep -i access-control-allow-origin | cut -f2- -d':'
166+
stdout: "http://localhost:25222"
167+
versions0301: # change to lax setting
168+
command: ../clace app update-metadata conf --promote cors.setting=lax /versions_local1
169+
versions0302:
170+
command: curl -Iu "admin:qwerty" localhost:25222/versions_local1 | grep -i access-control-allow-origin | cut -f2- -d':'
171+
stdout: "*"
172+
versions0303: # custom origin
173+
command: ../clace app update-metadata conf --promote cors.allow_origin=abc /versions_local1
174+
versions0304:
175+
command: curl -Iu "admin:qwerty" localhost:25222/versions_local1 | grep -i access-control-allow-origin | cut -f2- -d':'
176+
stdout: "abc"
177+
versions0305: # custom headers
178+
command: ../clace app update-metadata conf --promote cors.allow_headers="aa,bb" /versions_local1
179+
versions0306:
180+
command: curl -Iu "admin:qwerty" localhost:25222/versions_local1 | grep -i access-control-allow-headers | cut -f2- -d':'
181+
stdout: "aa,bb"
182+
versions0307: # delete custom headers
183+
command: ../clace app update-metadata conf --promote cors.allow_headers=- /versions_local1
184+
versions0308:
185+
command: curl -Iu "admin:qwerty" localhost:25222/versions_local1 | grep -i access-control-allow-headers | cut -f2- -d':'
186+
stdout: "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Requested-With"
187+
versions0309: # disable CORS
188+
command: ../clace app update-metadata conf --promote cors.setting=disable /versions_local1
189+
versions0310:
190+
command: curl -Iu "admin:qwerty" localhost:25222/versions_local1 | grep -i access-control-allow-origin | cut -f2- -d':'
191+
stdout:
192+
line-count: 0
193+
163194
versions99999: # Cleanup
164195
command: (rm -rf ./versionstest; ../clace app delete "*:versions**"; ../clace app delete "versions*:**") || true

0 commit comments

Comments
 (0)