forked from orijtech/otils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
With this change, setting CORS.AllowCredentials will permit set the "Access-Control-Allow-Credentials" header which when combined with a client XHR's withCredentials allows cookies to be used in subsequent Ajax requests. Fixes orijtech#16
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package otils | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestCORSHeader(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cors *CORS | ||
want http.Header | ||
}{ | ||
{ | ||
name: "with allowCredentials", | ||
cors: &CORS{ | ||
AllowCredentials: true, | ||
}, | ||
want: http.Header{ | ||
"Access-Control-Allow-Credentials": {"true"}, | ||
}, | ||
}, | ||
{ | ||
name: "all enabled", | ||
cors: CORSMiddlewareAllInclusive(nil).(*CORS), | ||
want: http.Header{ | ||
"Access-Control-Allow-Credentials": {"true"}, | ||
"Access-Control-Allow-Headers": {"*"}, | ||
"Access-Control-Allow-Methods": {"*"}, | ||
"Access-Control-Allow-Origin": {"*"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
tt.cors.setCORSForResponseWriter(rec) | ||
res := rec.Result() | ||
got := res.Header | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Fatalf("Mismatched end headers\nGot: %s\nWant: %s", asJSON(got), asJSON(tt.want)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func asJSON(v interface{}) []byte { | ||
blob, err := json.MarshalIndent(v, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
return blob | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters