From 0ae6e77864b8384440f9a9103d8a8e90f50d3ad8 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Sun, 29 Oct 2017 23:06:52 +0100 Subject: [PATCH] Added unit test for handling the info call --- handlers/handlers.go | 10 ++--- handlers/handlers_test.go | 85 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 05927f8..e4d1f0a 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -143,13 +143,13 @@ func (h *Handler) handleInfo(w http.ResponseWriter, r *http.Request, p httproute var req struct { ID string } - if r.Body == nil { - http.Error(w, "invalid request, body is nil", http.StatusBadRequest) - return - } err := json.NewDecoder(r.Body).Decode(&req) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, fmt.Sprintf("could not decode JSON: %v", err), http.StatusBadRequest) + return + } + if req.ID == "" { + http.Error(w, "no ID provided", http.StatusBadRequest) return } raw, err := h.store.GetEntryByIDRaw(req.ID) diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index 1bab6f1..2b4c062 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -9,6 +9,7 @@ import ( "net/http/httptest" "net/url" "os" + "strings" "testing" "github.com/maxibanki/golang-url-shorter/store" @@ -263,6 +264,90 @@ func TestCreateEntryForm(t *testing.T) { }) } +func TestHandleInfo(t *testing.T) { + cleanup, err := getBackend() + if err != nil { + t.Fatalf("could not create backend: %v", err) + } + defer cleanup() + + t.Run("check existing entry", func(t *testing.T) { + body, err := json.Marshal(store.Entry{ + URL: testURL, + }) + if err != nil { + t.Fatalf("could not marshal json: %v", err) + } + resp, err := http.Post(server.URL+"/api/v1/create", "application/json", bytes.NewBuffer(body)) + var parsed URLUtil + err = json.NewDecoder(resp.Body).Decode(&parsed) + if err != nil { + t.Fatalf("could not unmarshal data: %v", err) + } + id := strings.Replace(parsed.URL, server.URL+"/", "", 1) + body, err = json.Marshal(struct { + ID string + }{ + ID: id, + }) + if err != nil { + t.Fatalf("could not marshal the body: %v", err) + } + resp, err = http.Post(server.URL+"/api/v1/info", "appplication/json", bytes.NewBuffer(body)) + 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 entry store.Entry + err = json.NewDecoder(resp.Body).Decode(&entry) + if err != nil { + t.Fatalf("could not unmarshal data: %v", err) + } + if entry.URL != testURL { + t.Fatalf("url is not the expected one: %s; got: %s", testURL, entry.URL) + } + }) + t.Run("invalid body", func(t *testing.T) { + resp, err := http.Post(server.URL+"/api/v1/info", "appplication/json", bytes.NewBuffer(nil)) + 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.StatusBadRequest, 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) != "could not decode JSON: EOF" { + t.Fatalf("body is not the expected one: %s", body) + } + }) + t.Run("no ID provided", func(t *testing.T) { + if err != nil { + t.Fatalf("could not marshal the body: %v", err) + } + resp, err := http.Post(server.URL+"/api/v1/info", "appplication/json", bytes.NewBufferString("{}")) + 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.StatusBadRequest, 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) != "no ID provided" { + t.Fatalf("body is not the expected one: %s", body) + } + }) +} + func testRedirect(t *testing.T, shortURL, longURL string) { client := &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error {