-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rest/cors_test: tests for empty Access-Control-Request-Headers in pre…
…flight requests Signed-off-by: Maciej Borzecki <[email protected]>
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package rest | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/ant0ine/go-json-rest/rest/test" | ||
) | ||
|
||
func TestCorsMiddlewareEmptyAccessControlRequestHeaders(t *testing.T) { | ||
api := NewApi() | ||
|
||
// the middleware to test | ||
api.Use(&CorsMiddleware{ | ||
OriginValidator: func(_ string, _ *Request) bool { | ||
return true | ||
}, | ||
AllowedMethods: []string{ | ||
"GET", | ||
"POST", | ||
"PUT", | ||
}, | ||
AllowedHeaders: []string{ | ||
"Origin", | ||
"Referer", | ||
}, | ||
}) | ||
|
||
// wrap all | ||
handler := api.MakeHandler() | ||
|
||
req, _ := http.NewRequest("OPTIONS", "http://localhost", nil) | ||
req.Header.Set("Origin", "http://another.host") | ||
req.Header.Set("Access-Control-Request-Method", "PUT") | ||
req.Header.Set("Access-Control-Request-Headers", "") | ||
|
||
recorded := test.RunRequest(t, handler, req) | ||
t.Logf("recorded: %+v\n", recorded.Recorder) | ||
recorded.CodeIs(200) | ||
recorded.HeaderIs("Access-Control-Allow-Methods", "GET,POST,PUT") | ||
recorded.HeaderIs("Access-Control-Allow-Headers", "Origin,Referer") | ||
recorded.HeaderIs("Access-Control-Allow-Origin", "http://another.host") | ||
} |