Skip to content

[cors]: allow users to disable preflight caching #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type cors struct {
allowedOriginValidator OriginValidator
exposedHeaders []string
maxAge int
customMaxAge bool
ignoreOptions bool
allowCredentials bool
optionStatusCode int
Expand Down Expand Up @@ -94,7 +95,7 @@ func (ch *cors) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(corsAllowHeadersHeader, strings.Join(allowedHeaders, ","))
}

if ch.maxAge > 0 {
if ch.customMaxAge {
w.Header().Set(corsMaxAgeHeader, strconv.Itoa(ch.maxAge))
}

Expand Down Expand Up @@ -295,6 +296,7 @@ func MaxAge(age int) CORSOption {
age = 600
}

ch.customMaxAge = true
ch.maxAge = age
return nil
}
Expand Down
44 changes: 44 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,50 @@ func TestCORSHandlerMaxAgeForPreflight(t *testing.T) {
}
}

func TestCORSHandlerZeroMaxAgeForPreflight(t *testing.T) {
r := newRequest(http.MethodOptions, "http://www.example.com")
r.Header.Set("Origin", r.URL.String())
r.Header.Set(corsRequestMethodHeader, http.MethodPost)

rr := httptest.NewRecorder()

testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

CORS(MaxAge(0))(testHandler).ServeHTTP(rr, r)
resp := rr.Result()

if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Fatalf("bad status: got %v want %v", got, want)
}

header := resp.Header.Get(corsMaxAgeHeader)
if got, want := header, "0"; got != want {
t.Fatalf("bad header: expected %q to be %q, got %q.", corsMaxAgeHeader, want, got)
}
}

func TestCORSHandlerNoMaxAgeForPreflight(t *testing.T) {
r := newRequest(http.MethodOptions, "http://www.example.com")
r.Header.Set("Origin", r.URL.String())
r.Header.Set(corsRequestMethodHeader, http.MethodPost)

rr := httptest.NewRecorder()

testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

CORS()(testHandler).ServeHTTP(rr, r)
resp := rr.Result()

if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Fatalf("bad status: got %v want %v", got, want)
}

header := resp.Header.Get(corsMaxAgeHeader)
if header != "" {
t.Fatalf("unexpected header %q", corsMaxAgeHeader)
}
}

func TestCORSHandlerAllowedCredentials(t *testing.T) {
r := newRequest(http.MethodGet, "http://www.example.com/")
r.Header.Set("Origin", r.URL.String())
Expand Down