Skip to content
Snippets Groups Projects
Commit 1f30f965 authored by Will McCutchen's avatar Will McCutchen
Browse files

Group GET and CORS tests

parent b6dd9eca
No related branches found
No related tags found
No related merge requests found
......@@ -85,21 +85,39 @@ func TestUTF8(t *testing.T) {
assertBodyContains(t, w, `Hello world, Καλημέρα κόσμε, コンニチハ`)
}
func TestGet__Basic(t *testing.T) {
r, _ := http.NewRequest("GET", "/get", nil)
func TestGet(t *testing.T) {
makeGetRequest := func(params *url.Values, headers *http.Header, expectedStatus int) (*getResponse, *httptest.ResponseRecorder) {
urlStr := "/get"
if params != nil {
urlStr = fmt.Sprintf("%s?%s", urlStr, params.Encode())
}
r, _ := http.NewRequest("GET", urlStr, nil)
r.Host = "localhost"
r.Header.Set("User-Agent", "test")
if headers != nil {
for k, vs := range *headers {
for _, v := range vs {
r.Header.Set(k, v)
}
}
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, jsonContentType)
assertStatusCode(t, w, expectedStatus)
var resp *getResponse
if expectedStatus == http.StatusOK {
err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err)
}
}
return resp, w
}
t.Run("basic", func(t *testing.T) {
resp, _ := makeGetRequest(nil, nil, http.StatusOK)
if resp.Args.Encode() != "" {
t.Fatalf("expected empty args, got %s", resp.Args.Encode())
......@@ -123,59 +141,66 @@ func TestGet__Basic(t *testing.T) {
t.Fatalf("expected %s = %#v, got %#v", test.key, test.expected, resp.Headers.Get(test.key))
}
}
}
})
func TestGet__WithParams(t *testing.T) {
params := url.Values{}
t.Run("with_query_params", func(t *testing.T) {
params := &url.Values{}
params.Set("foo", "foo")
params.Add("bar", "bar1")
params.Add("bar", "bar2")
r, _ := http.NewRequest("GET", fmt.Sprintf("/get?%s", params.Encode()), nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusOK)
assertContentType(t, w, jsonContentType)
var resp *getResponse
err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err)
}
resp, _ := makeGetRequest(params, nil, http.StatusOK)
if resp.Args.Encode() != params.Encode() {
t.Fatalf("args mismatch: %s != %s", resp.Args.Encode(), params.Encode())
}
}
})
func TestGet__OnlyAllowsGets(t *testing.T) {
t.Run("only_allows_gets", func(t *testing.T) {
r, _ := http.NewRequest("POST", "/get", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusMethodNotAllowed)
assertContentType(t, w, "text/plain; charset=utf-8")
})
var protoTests = []struct {
key string
value string
}{
{"X-Forwarded-Proto", "https"},
{"X-Forwarded-Protocol", "https"},
{"X-Forwarded-Ssl", "on"},
}
for _, test := range protoTests {
t.Run(test.key, func(t *testing.T) {
headers := &http.Header{}
headers.Set(test.key, test.value)
resp, _ := makeGetRequest(nil, headers, http.StatusOK)
if !strings.HasPrefix(resp.URL, "https://") {
t.Fatalf("%s=%s should result in https URL", test.key, test.value)
}
})
}
}
func TestGet__CORSHeadersWithoutRequestOrigin(t *testing.T) {
func TestCORS(t *testing.T) {
t.Run("CORS/no_request_origin", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/get", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertHeader(t, w, "Access-Control-Allow-Origin", "*")
}
})
func TestGet__CORSHeadersWithRequestOrigin(t *testing.T) {
t.Run("CORS/with_request_origin", func(t *testing.T) {
r, _ := http.NewRequest("GET", "/get", nil)
r.Header.Set("Origin", "origin")
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertHeader(t, w, "Access-Control-Allow-Origin", "origin")
}
})
func TestGet__CORSHeadersWithOptionsVerb(t *testing.T) {
t.Run("CORS/options_request", func(t *testing.T) {
r, _ := http.NewRequest("OPTIONS", "/get", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
......@@ -193,9 +218,9 @@ func TestGet__CORSHeadersWithOptionsVerb(t *testing.T) {
for _, test := range headerTests {
assertHeader(t, w, test.key, test.expected)
}
}
})
func TestGet__CORSAllowHeaders(t *testing.T) {
t.Run("CORS/allow_headers", func(t *testing.T) {
r, _ := http.NewRequest("OPTIONS", "/get", nil)
r.Header.Set("Access-Control-Request-Headers", "X-Test-Header")
w := httptest.NewRecorder()
......@@ -210,34 +235,7 @@ func TestGet__CORSAllowHeaders(t *testing.T) {
for _, test := range headerTests {
assertHeader(t, w, test.key, test.expected)
}
}
func TestGet__XForwardedProto(t *testing.T) {
var tests = []struct {
key string
value string
}{
{"X-Forwarded-Proto", "https"},
{"X-Forwarded-Protocol", "https"},
{"X-Forwarded-Ssl", "on"},
}
for _, test := range tests {
r, _ := http.NewRequest("GET", "/get", nil)
r.Header.Set(test.key, test.value)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
var resp *getResponse
err := json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err)
}
if !strings.HasPrefix(resp.URL, "https://") {
t.Fatalf("%s=%s should result in https URL", test.key, test.value)
}
}
})
}
func TestIP(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment