You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* More sensical CORSMethodMiddleware
* Only sets Access-Control-Allow-Methods on valid preflight requests
* Does not return after setting the Access-Control-Allow-Methods header
* Does not append OPTIONS header to Access-Control-Allow-Methods
regardless of whether there is an OPTIONS method matcher
* Adds tests for the listed behavior
* Add example for CORSMethodMiddleware
* Do not check for preflight and add documentation to the README
* Use http.MethodOptions instead of "OPTIONS"
* Add link to CORSMethodMiddleware section to readme
* Add test for unmatching route methods
* Rename CORS Method Middleware to Handling CORS Requests in README
* Link CORSMethodMiddleware in README to godoc
* Break CORSMethodMiddleware doc into bullets for readability
* Add comment about specifying OPTIONS to example in README for CORSMethodMiddleware
* Document cURL command used for testing CORS Method Middleware
* Update comment in example to "Handle the request"
* Add explicit comment about OPTIONS matchers to CORSMethodMiddleware doc
* Update circleci config to only check gofmt diff on latest go version
* Break up gofmt and go vet checks into separate steps.
* Use canonical circleci config
Copy file name to clipboardExpand all lines: README.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,7 @@ The name mux stands for "HTTP request multiplexer". Like the standard `http.Serv
30
30
*[Walking Routes](#walking-routes)
31
31
*[Graceful Shutdown](#graceful-shutdown)
32
32
*[Middleware](#middleware)
33
+
*[Handling CORS Requests](#handling-cors-requests)
33
34
*[Testing Handlers](#testing-handlers)
34
35
*[Full Example](#full-example)
35
36
@@ -492,6 +493,73 @@ r.Use(amw.Middleware)
492
493
493
494
Note: The handler chain will be stopped if your middleware doesn't call `next.ServeHTTP()` with the corresponding parameters. This can be used to abort a request if the middleware writer wants to. Middlewares _should_ write to `ResponseWriter` if they _are_ going to terminate the request, and they _should not_ write to `ResponseWriter` if they _are not_ going to terminate it.
494
495
496
+
### Handling CORS Requests
497
+
498
+
[CORSMethodMiddleware](https://godoc.org/github.com/gorilla/mux#CORSMethodMiddleware) intends to make it easier to strictly set the `Access-Control-Allow-Methods` response header.
499
+
500
+
* You will still need to use your own CORS handler to set the other CORS headers such as `Access-Control-Allow-Origin`
501
+
* The middleware will set the `Access-Control-Allow-Methods` header to all the method matchers (e.g. `r.Methods(http.MethodGet, http.MethodPut, http.MethodOptions)` -> `Access-Control-Allow-Methods: GET,PUT,OPTIONS`) on a route
502
+
* If you do not specify any methods, then:
503
+
> _Important_: there must be an `OPTIONS` method matcher for the middleware to set the headers.
504
+
505
+
Here is an example of using `CORSMethodMiddleware` along with a custom `OPTIONS` handler to set all the required CORS headers:
506
+
507
+
```go
508
+
package main
509
+
510
+
import (
511
+
"net/http"
512
+
"github.com/gorilla/mux"
513
+
)
514
+
515
+
funcmain() {
516
+
r:= mux.NewRouter()
517
+
518
+
// IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headers
Testing handlers in a Go web application is straightforward, and _mux_ doesn't complicate this any further. Given two files: `endpoints.go` and `endpoints_test.go`, here's how we'd test an application using _mux_.
0 commit comments