Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
refactor preflight request test for the addition of middleware
func TestPreflight(t *testing.T) {
// prep test data
conf := ebs.NewConfig()
expectMethods := "POST, GET, OPTIONS, PUT"
expectHeaders := "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization"
expectOrigin := ""
req := newTestRequest("OPTIONS")
// run the handler logic
result, err := ebs.MiddlewareCORS(conf, handler)(req)
assert.IsType(t, nil, err)
assert.Equal(t, http.StatusOK, result.StatusCode)
// check for expected CORS
assert.Equal(t, expectMethods, result.Headers["Access-Control-Allow-Methods"])
assert.Equal(t, expectHeaders, result.Headers["Access-Control-Allow-Headers"])
assert.Equal(t, expectOrigin, result.Headers["Access-Control-Allow-Origin"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment