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

Stop manually parsing query params like a caveman

parent 5b7dab93
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
......@@ -35,13 +34,8 @@ func (h *HTTPBin) UTF8(w http.ResponseWriter, r *http.Request) {
// Get handles HTTP GET requests
func (h *HTTPBin) Get(w http.ResponseWriter, r *http.Request) {
args, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
http.Error(w, fmt.Sprintf("error parsing query params: %s", err), http.StatusBadRequest)
return
}
resp := &getResponse{
Args: args,
Args: r.URL.Query(),
Headers: r.Header,
Origin: getOrigin(r),
URL: getURL(r).String(),
......@@ -52,20 +46,14 @@ func (h *HTTPBin) Get(w http.ResponseWriter, r *http.Request) {
// RequestWithBody handles POST, PUT, and PATCH requests
func (h *HTTPBin) RequestWithBody(w http.ResponseWriter, r *http.Request) {
args, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
http.Error(w, fmt.Sprintf("error parsing query params: %s", err), http.StatusBadRequest)
return
}
resp := &bodyResponse{
Args: args,
Args: r.URL.Query(),
Headers: r.Header,
Origin: getOrigin(r),
URL: getURL(r).String(),
}
err = parseBody(w, r, resp, h.options.MaxMemory)
err := parseBody(w, r, resp, h.options.MaxMemory)
if err != nil {
http.Error(w, fmt.Sprintf("error parsing request body: %s", err), http.StatusBadRequest)
}
......@@ -179,12 +167,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
// ResponseHeaders responds with a map of header values
func (h *HTTPBin) ResponseHeaders(w http.ResponseWriter, r *http.Request) {
args, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
http.Error(w, fmt.Sprintf("error parsing query params: %s", err), http.StatusBadRequest)
return
}
args := r.URL.Query()
for k, vs := range args {
for _, v := range vs {
w.Header().Add(http.CanonicalHeaderKey(k), v)
......
......@@ -141,14 +141,6 @@ func TestGet__WithParams(t *testing.T) {
}
}
func TestGet__InvalidQuery(t *testing.T) {
r, _ := http.NewRequest("GET", "/get?foo=%ZZ", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusBadRequest)
}
func TestGet__OnlyAllowsGets(t *testing.T) {
r, _ := http.NewRequest("POST", "/get", nil)
w := httptest.NewRecorder()
......@@ -540,13 +532,6 @@ func TestPost__BodyTooBig(t *testing.T) {
assertContentType(t, w, "application/json; encoding=utf-8")
}
func TestPost__InvalidQueryParams(t *testing.T) {
r, _ := http.NewRequest("POST", "/post?foo=%ZZ", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusBadRequest)
}
func TestPost__QueryParams(t *testing.T) {
params := url.Values{}
params.Set("foo", "foo")
......@@ -743,14 +728,7 @@ func TestResponseHeaders__OverrideContentType(t *testing.T) {
assertContentType(t, w, contentType)
}
func TestResponseHeaders__InvalidQuery(t *testing.T) {
r, _ := http.NewRequest("GET", "/response-headers?foo=%ZZ", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusBadRequest)
}
func TestRedirects__OK(t *testing.T) {
func TestAbsoluteAndRelativeRedirects__OK(t *testing.T) {
var tests = []struct {
relative bool
n int
......@@ -782,7 +760,7 @@ func TestRedirects__OK(t *testing.T) {
}
}
func TestRedirects__Errors(t *testing.T) {
func TestAbsoluteAndRelativeRedirects__Errors(t *testing.T) {
var tests = []struct {
relative bool
given interface{}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment