From 9d952edc67d0b3b94720f1f7944b782408280fc3 Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Mon, 27 Jul 2020 17:08:10 +0200 Subject: [PATCH] Add a functional test for `/nr` Fixes #68 --- handlers_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/handlers_test.go b/handlers_test.go index 1a35fd2..de5ff53 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -184,3 +184,38 @@ func TestIssue56(t *testing.T) { srv.ServeHTTP(rr, req) checkStatusCode(t, rr, http.StatusBadRequest) } + +func TestIssue68(t *testing.T) { + srv, _ := createTemporaryRouter(t) + originalURL := "https://example.com" + + var body bytes.Buffer + form := multipart.NewWriter(&body) + form.WriteField("shorten", "https://example.com") + form.Close() + req, err := http.NewRequest("POST", "/", bytes.NewReader(body.Bytes())) + if err != nil { + t.Fatal(err) + } + req.Header.Add("Content-Type", form.FormDataContentType()) + rr := httptest.NewRecorder() + srv.ServeHTTP(rr, req) + checkStatusCode(t, rr, http.StatusFound) + rawURL := strings.SplitN(rr.Body.String(), "\n", 2)[0] + pasteURL, err := url.Parse(rawURL) + if err != nil { + t.Fatal(err) + } + + // Check if the no-redirect handler works properly. + req, err = http.NewRequest("GET", pasteURL.Path+"/nr", nil) + if err != nil { + t.Fatal(err) + } + rr = httptest.NewRecorder() + srv.ServeHTTP(rr, req) + checkStatusCode(t, rr, http.StatusOK) + if rr.Body.String() != originalURL { + t.Errorf("incorrect URL = %v, want %v", rr.Body.String(), originalURL) + } +}