Skip to content

CORS ​

Setup().CORS(...) answers CORS requests, preflights included, with the headers of its configuration. mockhttp.CORS() builds one, and mockhttp.DefaultCORSConfig is the default: any origin, the methods GET, POST, HEAD, PUT, DELETE and PATCH, and 204 for a preflight.

go
m := mockhttp.MustNew(mockhttp.Setup().CORS(mockhttp.CORS().
	AllowOrigin("https://app.example.com").
	AllowCredentials(true).
	ExposeHeaders("X-Request-Id")))
GoSettingSets
AllowOrigin(origins...)cors.allowed_originAccess-Control-Allow-Origin.
AllowMethods(methods...)cors.allowed_methodsAccess-Control-Allow-Methods.
AllowedHeaders(headers...)cors.allowed_headersAccess-Control-Allow-Headers.
ExposeHeaders(headers...)cors.expose_headersAccess-Control-Expose-Headers.
AllowCredentials(v)cors.allow_credentialsAccess-Control-Allow-Credentials.
MaxAge(seconds)cors.max_ageAccess-Control-Max-Age.
SuccessStatusCode(code)cors.success_status_codeThe status of a preflight.

In settings, cors: true (--cors) turns CORS on with the defaults, and setting any cors key turns it on too.

An answer that sends a CORS header, such as a proxied one, answers CORS by itself: the server's CORS headers aren't sent with it. A Handler reply writes its answer on top of the server's CORS headers: it should Set its CORS headers, or Del the server's. Preflights the server answers aren't journaled.