|
|
@ -18,6 +18,7 @@ import ( |
|
|
const ( |
|
|
const ( |
|
|
baseURL = "http://myshorter" |
|
|
baseURL = "http://myshorter" |
|
|
testingDBName = "main.db" |
|
|
testingDBName = "main.db" |
|
|
|
|
|
testURL = "https://www.google.de/" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
var server *httptest.Server |
|
|
var server *httptest.Server |
|
|
@ -115,7 +116,6 @@ func TestCreateEntryMultipart(t *testing.T) { |
|
|
defer cleanup() |
|
|
defer cleanup() |
|
|
|
|
|
|
|
|
t.Run("valid request", func(t *testing.T) { |
|
|
t.Run("valid request", func(t *testing.T) { |
|
|
const testURL = "https://www.google.de/" |
|
|
|
|
|
// Prepare a form that you will submit to that URL.
|
|
|
// Prepare a form that you will submit to that URL.
|
|
|
var b bytes.Buffer |
|
|
var b bytes.Buffer |
|
|
multipartWriter := multipart.NewWriter(&b) |
|
|
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) { |
|
|
t.Run("invalid request", func(t *testing.T) { |
|
|
const testURL = "https://www.google.de/" |
|
|
|
|
|
// Prepare a form that you will submit to that URL.
|
|
|
// Prepare a form that you will submit to that URL.
|
|
|
var b bytes.Buffer |
|
|
var b bytes.Buffer |
|
|
multipartWriter := multipart.NewWriter(&b) |
|
|
multipartWriter := multipart.NewWriter(&b) |
|
|
@ -154,6 +181,9 @@ func TestCreateEntryMultipart(t *testing.T) { |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Fatalf("could not post to the backend: %v", err) |
|
|
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, err := ioutil.ReadAll(resp.Body) |
|
|
body = bytes.TrimSpace(body) |
|
|
body = bytes.TrimSpace(body) |
|
|
if err != nil { |
|
|
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) { |
|
|
func testRedirect(t *testing.T, shortURL, longURL string) { |
|
|
client := &http.Client{ |
|
|
client := &http.Client{ |
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error { |
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error { |
|
|
|