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