Skip to content
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

CORS: allow users to disable preflight caching #250

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type cors struct {
allowedOrigins []string
allowedOriginValidator OriginValidator
exposedHeaders []string
maxAge int
maxAge *int
ignoreOptions bool
allowCredentials bool
optionStatusCode int
Expand Down Expand Up @@ -94,8 +94,8 @@ func (ch *cors) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(corsAllowHeadersHeader, strings.Join(allowedHeaders, ","))
}

if ch.maxAge > 0 {
w.Header().Set(corsMaxAgeHeader, strconv.Itoa(ch.maxAge))
if ch.maxAge != nil {
w.Header().Set(corsMaxAgeHeader, strconv.Itoa(*ch.maxAge))
}

if !ch.isMatch(method, defaultCorsMethods) {
Expand Down Expand Up @@ -295,7 +295,7 @@ func MaxAge(age int) CORSOption {
age = 600
}

ch.maxAge = age
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 @@ -418,3 +418,47 @@ func TestCORSAllowStar(t *testing.T) {
t.Fatalf("bad header: expected %q to be %q, got %q.", corsAllowOriginHeader, want, got)
}
}

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)
}
}