Browse Source

Add a functional test for `/nr`

Fixes #68
master
Daan Sprenkels 4 months ago
parent
commit
9d952edc67
1 changed files with 35 additions and 0 deletions
  1. +35
    -0
      handlers_test.go

+ 35
- 0
handlers_test.go View File

@@ -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)
}
}

Loading…
Cancel
Save