Browse Source

Modify the OPTIONS response status code (#41)

Using 204 is better than 200 when the request status is OPTIONS
李科笠 7 years ago
parent
commit
3e8a85107f
2 changed files with 3 additions and 3 deletions
  1. 1 1
      config.go
  2. 2 2
      cors_test.go

+ 1 - 1
config.go

@@ -71,7 +71,7 @@ func (cors *cors) applyCors(c *gin.Context) {
 
 	if c.Request.Method == "OPTIONS" {
 		cors.handlePreflight(c)
-		defer c.AbortWithStatus(200)
+		defer c.AbortWithStatus(204) // Using 204 is better than 200 when the request status is OPTIONS
 	} else {
 		cors.handleNormal(c)
 	}

+ 2 - 2
cors_test.go

@@ -301,7 +301,7 @@ func TestPassesAllowedOrigins(t *testing.T) {
 
 	// allowed CORS prefligh request
 	w = performRequest(router, "OPTIONS", "http://github.com")
-	assert.Equal(t, 200, w.Code)
+	assert.Equal(t, 204, w.Code)
 	assert.Equal(t, "http://github.com", w.Header().Get("Access-Control-Allow-Origin"))
 	assert.Equal(t, "", w.Header().Get("Access-Control-Allow-Credentials"))
 	assert.Equal(t, "GET,POST,PUT,HEAD", w.Header().Get("Access-Control-Allow-Methods"))
@@ -346,7 +346,7 @@ func TestPassesAllowedAllOrigins(t *testing.T) {
 
 	// allowed CORS prefligh request
 	w = performRequest(router, "OPTIONS", "https://facebook.com")
-	assert.Equal(t, 200, w.Code)
+	assert.Equal(t, 204, w.Code)
 	assert.Equal(t, "*", w.Header().Get("Access-Control-Allow-Origin"))
 	assert.Equal(t, "PATCH,GET,POST", w.Header().Get("Access-Control-Allow-Methods"))
 	assert.Equal(t, "Content-Type,Testheader", w.Header().Get("Access-Control-Allow-Headers"))