From 961f6dac62c45f28f2e8cfbdf2312c9171a5a67b Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Sun, 29 Oct 2017 22:46:47 +0100 Subject: [PATCH] - added test for creating an entry via `application/x-www-form-urlencoded` - added invalid url test for multipart form - added missing check if the key in the form func exists --- handlers/handlers.go | 4 ++ handlers/handlers_test.go | 102 +++++++++++++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 2 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 718f408..05927f8 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -112,6 +112,10 @@ func (h *Handler) handleCreateForm(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } + if r.PostFormValue("URL") == "" { + http.Error(w, "URL key does not exist", http.StatusBadRequest) + return + } id, err := h.store.CreateEntry(r.PostFormValue("URL"), r.RemoteAddr) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index bdbc804..1bab6f1 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -18,6 +18,7 @@ import ( const ( baseURL = "http://myshorter" testingDBName = "main.db" + testURL = "https://www.google.de/" ) var server *httptest.Server @@ -115,7 +116,6 @@ func TestCreateEntryMultipart(t *testing.T) { defer cleanup() t.Run("valid request", func(t *testing.T) { - const testURL = "https://www.google.de/" // Prepare a form that you will submit to that URL. var b bytes.Buffer multipartWriter := multipart.NewWriter(&b) @@ -143,8 +143,35 @@ func TestCreateEntryMultipart(t *testing.T) { }) }) + t.Run("invalid url", func(t *testing.T) { + // Prepare a form that you will submit to that URL. + var b bytes.Buffer + multipartWriter := multipart.NewWriter(&b) + formWriter, err := multipartWriter.CreateFormField("URL") + if err != nil { + t.Fatalf("could not create form field: %v", err) + } + formWriter.Write([]byte("this is definitely not a valid url")) + multipartWriter.Close() + + resp, err := http.Post(server.URL+"/api/v1/create", multipartWriter.FormDataContentType(), &b) + if err != nil { + t.Fatalf("could not post to the backend: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected status %d; got %d", http.StatusOK, resp.StatusCode) + } + body, err := ioutil.ReadAll(resp.Body) + body = bytes.TrimSpace(body) + if err != nil { + t.Fatalf("could not read the body: %v", err) + } + if string(body) != store.ErrNoValidURL.Error() { + t.Fatalf("received unexpected response: %s", body) + } + }) + t.Run("invalid request", func(t *testing.T) { - const testURL = "https://www.google.de/" // Prepare a form that you will submit to that URL. var b bytes.Buffer multipartWriter := multipart.NewWriter(&b) @@ -154,6 +181,9 @@ func TestCreateEntryMultipart(t *testing.T) { if err != nil { t.Fatalf("could not post to the backend: %v", err) } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected status %d; got %d", http.StatusOK, resp.StatusCode) + } body, err := ioutil.ReadAll(resp.Body) body = bytes.TrimSpace(body) if err != nil { @@ -165,6 +195,74 @@ func TestCreateEntryMultipart(t *testing.T) { }) } +func TestCreateEntryForm(t *testing.T) { + cleanup, err := getBackend() + if err != nil { + t.Fatalf("could not create backend: %v", err) + } + defer cleanup() + + t.Run("valid request", func(t *testing.T) { + data := url.Values{} + data.Set("URL", testURL) + + resp, err := http.Post(server.URL+"/api/v1/create", "application/x-www-form-urlencoded", bytes.NewBufferString(data.Encode())) + if err != nil { + t.Fatalf("could not post to the backend: %v", err) + } + if resp.StatusCode != http.StatusOK { + t.Errorf("expected status %d; got %d", http.StatusOK, resp.StatusCode) + } + var parsed URLUtil + err = json.NewDecoder(resp.Body).Decode(&parsed) + if err != nil { + t.Fatalf("could not unmarshal data: %v", err) + } + t.Run("test if shorted URL is correct", func(t *testing.T) { + testRedirect(t, parsed.URL, testURL) + }) + }) + + t.Run("invalid request", func(t *testing.T) { + resp, err := http.Post(server.URL+"/api/v1/create", "application/x-www-form-urlencoded", bytes.NewBufferString(url.Values{}.Encode())) + if err != nil { + t.Fatalf("could not post to the backend: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected status %d; got %d", http.StatusOK, resp.StatusCode) + } + body, err := ioutil.ReadAll(resp.Body) + body = bytes.TrimSpace(body) + if err != nil { + t.Fatalf("could not read the body: %v", err) + } + if string(body) != "URL key does not exist" { + t.Fatalf("received unexpected response: %s", body) + } + }) + + t.Run("invalid url", func(t *testing.T) { + data := url.Values{} + data.Set("URL", "this is definitely not a valid url") + + resp, err := http.Post(server.URL+"/api/v1/create", "application/x-www-form-urlencoded", bytes.NewBufferString(data.Encode())) + if err != nil { + t.Fatalf("could not post to the backend: %v", err) + } + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("expected status %d; got %d", http.StatusOK, resp.StatusCode) + } + body, err := ioutil.ReadAll(resp.Body) + body = bytes.TrimSpace(body) + if err != nil { + t.Fatalf("could not read the body: %v", err) + } + if string(body) != store.ErrNoValidURL.Error() { + t.Fatalf("received unexpected response: %s", body) + } + }) +} + func testRedirect(t *testing.T, shortURL, longURL string) { client := &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error {