@@ -24,7 +24,9 @@ func TestWorkspaceAppCors(t *testing.T) {
24
24
Username :"user" ,
25
25
}
26
26
27
- handler := httpmw .WorkspaceAppCors (regex ,app )
27
+ handler := httpmw .WorkspaceAppCors (regex ,app )(http .HandlerFunc (func (rw http.ResponseWriter ,r * http.Request ) {
28
+ rw .WriteHeader (http .StatusNoContent )
29
+ }))
28
30
methods := []string {
29
31
http .MethodOptions ,
30
32
http .MethodHead ,
@@ -72,15 +74,26 @@ func TestWorkspaceAppCors(t *testing.T) {
72
74
r .Header .Set ("Origin" ,test .origin )
73
75
rw := httptest .NewRecorder ()
74
76
75
- handler (http .HandlerFunc (func (rw http.ResponseWriter ,r * http.Request ) {
76
- rw .WriteHeader (http .StatusOK )
77
- })).ServeHTTP (rw ,r )
77
+ // Preflight requests need to know what method will be requested.
78
+ if method == http .MethodOptions {
79
+ r .Header .Set ("Access-Control-Request-Method" ,method )
80
+ }
81
+
82
+ handler .ServeHTTP (rw ,r )
78
83
79
84
if test .allowed {
80
85
require .Equal (t ,test .origin ,rw .Header ().Get ("Access-Control-Allow-Origin" ))
81
86
}else {
82
87
require .Equal (t ,"" ,rw .Header ().Get ("Access-Control-Allow-Origin" ))
83
88
}
89
+
90
+ // For options we should never get to our handler as the middleware
91
+ // short-circuits with a 200.
92
+ if method == http .MethodOptions {
93
+ require .Equal (t ,http .StatusOK ,rw .Code )
94
+ }else {
95
+ require .Equal (t ,http .StatusNoContent ,rw .Code )
96
+ }
84
97
}
85
98
})
86
99
}