Skip to content

Commit

Permalink
Merge pull request #25 from krakend/default_status
Browse files Browse the repository at this point in the history
Change back the default status code to 204
  • Loading branch information
dhontecillas authored Jul 18, 2024
2 parents 705666f + ab03d08 commit 6375cb3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
5 changes: 0 additions & 5 deletions mux/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func NewWithLogger(e config.ExtraConfig, l logging.Logger) mux.HandlerMiddleware
if len(cfg.AllowHeaders) == 0 {
cfg.AllowHeaders = []string{"*"}
}
// Maintain the old default value to not change behaviour
// the rs/cors new default is to return a 204
if cfg.OptionsSuccessStatus == 0 {
cfg.OptionsSuccessStatus = 200
}

c := cors.New(cors.Options{
AllowedOrigins: cfg.AllowOrigins,
Expand Down
50 changes: 35 additions & 15 deletions mux/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func TestNew(t *testing.T) {
"max_age": "2h"
}
}`)
json.Unmarshal(serialized, &sampleCfg)
if err := json.Unmarshal(serialized, &sampleCfg); err != nil {
t.Error(err)
return
}
h := New(sampleCfg)
res := httptest.NewRecorder()
req, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
Expand Down Expand Up @@ -61,14 +64,17 @@ func TestNewWithLogger(t *testing.T) {
"max_age": "2h"
}
}`)
json.Unmarshal(serialized, &sampleCfg)
if err := json.Unmarshal(serialized, &sampleCfg); err != nil {
t.Error(err)
return
}
h := NewWithLogger(sampleCfg, logger)
res := httptest.NewRecorder()
req, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
req.Header.Add("Origin", "http://foobar.com")
handler := h.Handler(testHandler)
handler.ServeHTTP(res, req)
if res.Code != 200 {
if res.Code != http.StatusOK {
t.Errorf("Invalid status code: %d should be 200", res.Code)
}

Expand All @@ -91,7 +97,10 @@ func TestAllowOriginEmpty(t *testing.T) {
serialized := []byte(`{ "github_com/devopsfaith/krakend-cors": {
}
}`)
json.Unmarshal(serialized, &sampleCfg)
if err := json.Unmarshal(serialized, &sampleCfg); err != nil {
t.Error(err)
return
}
h := New(sampleCfg)
res := httptest.NewRecorder()
req, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
Expand All @@ -100,8 +109,8 @@ func TestAllowOriginEmpty(t *testing.T) {
req.Header.Add("Origin", "http://foobar.com")
handler := h.Handler(testHandler)
handler.ServeHTTP(res, req)
if res.Code != 200 {
t.Errorf("Invalid status code: %d should be 200", res.Code)
if res.Code != http.StatusNoContent {
t.Errorf("Invalid status code: %d should be 204", res.Code)
}

assertHeaders(t, res.Header(), map[string]string{
Expand All @@ -115,10 +124,13 @@ func TestAllowOriginEmpty(t *testing.T) {
func TestOptionsSuccess(t *testing.T) {
sampleCfg := map[string]interface{}{}
serialized := []byte(`{ "github_com/devopsfaith/krakend-cors": {
"options_success_status": 205
"options_success_status": 200
}
}`)
json.Unmarshal(serialized, &sampleCfg)
if err := json.Unmarshal(serialized, &sampleCfg); err != nil {
t.Error(err)
return
}
h := New(sampleCfg)
res := httptest.NewRecorder()
req, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
Expand All @@ -127,8 +139,8 @@ func TestOptionsSuccess(t *testing.T) {
req.Header.Add("Origin", "http://foobar.com")
handler := h.Handler(testHandler)
handler.ServeHTTP(res, req)
if res.Code != 205 {
t.Errorf("Invalid status code: %d should be 205", res.Code)
if res.Code != http.StatusOK {
t.Errorf("Invalid status code: %d should be 200", res.Code)
}

assertHeaders(t, res.Header(), map[string]string{
Expand All @@ -145,7 +157,10 @@ func TestAllowPrivateNetwork(t *testing.T) {
"allow_private_network": true
}
}`)
json.Unmarshal(serialized, &sampleCfg)
if err := json.Unmarshal(serialized, &sampleCfg); err != nil {
t.Error(err)
return
}
h := New(sampleCfg)
res := httptest.NewRecorder()
req, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
Expand All @@ -154,8 +169,8 @@ func TestAllowPrivateNetwork(t *testing.T) {
req.Header.Add("Origin", "http://foobar.com")
handler := h.Handler(testHandler)
handler.ServeHTTP(res, req)
if res.Code != 200 {
t.Errorf("Invalid status code: %d should be 200", res.Code)
if res.Code != http.StatusNoContent {
t.Errorf("Invalid status code: %d should be 204", res.Code)
}

assertHeaders(t, res.Header(), map[string]string{
Expand All @@ -172,15 +187,20 @@ func TestOptionPasstrough(t *testing.T) {
"options_passthrough": true
}
}`)
json.Unmarshal(serialized, &sampleCfg)

if err := json.Unmarshal(serialized, &sampleCfg); err != nil {
t.Error(err)
return
}

h := New(sampleCfg)
res := httptest.NewRecorder()
req, _ := http.NewRequest("OPTIONS", "http://example.com/foo", nil)
req.Header.Add("Access-Control-Request-Method", "GET")
req.Header.Add("Origin", "http://foobar.com")
handler := h.Handler(testHandler)
handler.ServeHTTP(res, req)
if res.Code != 200 {
if res.Code != http.StatusOK {
t.Errorf("Invalid status code: %d should be 200", res.Code)
}

Expand Down

0 comments on commit 6375cb3

Please sign in to comment.