(t *testing.T)
| 24 | ) |
| 25 | |
| 26 | func TestCORSMiddleware(t *testing.T) { |
| 27 | handlerCalled := false |
| 28 | inner := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 29 | handlerCalled = true |
| 30 | w.WriteHeader(http.StatusOK) |
| 31 | }) |
| 32 | |
| 33 | tests := []struct { |
| 34 | name string |
| 35 | allowedOrigins []string |
| 36 | method string |
| 37 | origin string |
| 38 | wantStatus int |
| 39 | wantACHeader string // expected Access-Control-Allow-Origin value, empty if absent |
| 40 | wantVary bool |
| 41 | wantHandler bool // whether inner handler should be called |
| 42 | }{ |
| 43 | { |
| 44 | name: "empty origins - passthrough", |
| 45 | allowedOrigins: nil, |
| 46 | method: http.MethodGet, |
| 47 | origin: "http://example.com", |
| 48 | wantStatus: http.StatusOK, |
| 49 | wantHandler: true, |
| 50 | }, |
| 51 | { |
| 52 | name: "no Origin header - passthrough", |
| 53 | allowedOrigins: []string{"http://example.com"}, |
| 54 | method: http.MethodGet, |
| 55 | origin: "", |
| 56 | wantStatus: http.StatusOK, |
| 57 | wantHandler: true, |
| 58 | }, |
| 59 | { |
| 60 | name: "matching origin GET", |
| 61 | allowedOrigins: []string{"http://example.com"}, |
| 62 | method: http.MethodGet, |
| 63 | origin: "http://example.com", |
| 64 | wantStatus: http.StatusOK, |
| 65 | wantACHeader: "http://example.com", |
| 66 | wantVary: true, |
| 67 | wantHandler: true, |
| 68 | }, |
| 69 | { |
| 70 | name: "matching origin OPTIONS preflight", |
| 71 | allowedOrigins: []string{"http://example.com"}, |
| 72 | method: http.MethodOptions, |
| 73 | origin: "http://example.com", |
| 74 | wantStatus: http.StatusNoContent, |
| 75 | wantACHeader: "http://example.com", |
| 76 | wantVary: true, |
| 77 | wantHandler: false, |
| 78 | }, |
| 79 | { |
| 80 | name: "non-matching origin", |
| 81 | allowedOrigins: []string{"http://example.com"}, |
| 82 | method: http.MethodGet, |
| 83 | origin: "http://evil.com", |
nothing calls this directly
no test coverage detected