@@ -40,6 +40,19 @@ func performRequest(r http.Handler, method, origin string) *httptest.ResponseRec
4040 return w
4141}
4242
43+ func performRequestWithHeaders (r http.Handler , method , origin string , headers map [string ]string ) * httptest.ResponseRecorder {
44+ req , _ := http .NewRequest (method , "/" , nil )
45+ for k , v := range headers {
46+ req .Header .Set (k , v )
47+ }
48+ if len (origin ) > 0 {
49+ req .Header .Set ("Origin" , origin )
50+ }
51+ w := httptest .NewRecorder ()
52+ r .ServeHTTP (w , req )
53+ return w
54+ }
55+
4356func TestConfigAddAllow (t * testing.T ) {
4457 config := Config {}
4558 config .AddAllowMethods ("POST" )
@@ -231,6 +244,13 @@ func TestPassesAllowedOrigins(t *testing.T) {
231244 assert .Empty (t , w .Header ().Get ("Access-Control-Allow-Credentials" ))
232245 assert .Empty (t , w .Header ().Get ("Access-Control-Expose-Headers" ))
233246
247+ // no CORS request, origin == host
248+ w = performRequestWithHeaders (router , "GET" , "http://facebook.com" , map [string ]string {"Host" : "facebook.com" })
249+ assert .Equal (t , "get" , w .Body .String ())
250+ assert .Empty (t , w .Header ().Get ("Access-Control-Allow-Origin" ))
251+ assert .Empty (t , w .Header ().Get ("Access-Control-Allow-Credentials" ))
252+ assert .Empty (t , w .Header ().Get ("Access-Control-Expose-Headers" ))
253+
234254 // allowed CORS request
235255 w = performRequest (router , "GET" , "http://google.com" )
236256 assert .Equal (t , "get" , w .Body .String ())
0 commit comments