126 lines
2.2 KiB
Go
126 lines
2.2 KiB
Go
package tests
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/brianvoe/gofakeit/v6"
|
|
"github.com/gavv/httpexpect/v2"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"url-shortener/internal/http-server/handlers/url/save"
|
|
"url-shortener/internal/lib/api"
|
|
"url-shortener/internal/lib/random"
|
|
)
|
|
|
|
const (
|
|
host = "localhost:8082"
|
|
user = "user"
|
|
password = "pa$$"
|
|
)
|
|
|
|
func TestURLShortener_HappyPath(t *testing.T) {
|
|
u := url.URL{
|
|
Scheme: "http",
|
|
Host: host,
|
|
}
|
|
e := httpexpect.Default(t, u.String())
|
|
|
|
e.POST("/url").
|
|
WithJSON(save.Request{
|
|
URL: gofakeit.URL(),
|
|
Alias: random.NewRandomString(10),
|
|
}).
|
|
WithBasicAuth(user, password).
|
|
Expect().
|
|
Status(200).
|
|
JSON().Object().
|
|
ContainsKey("alias")
|
|
}
|
|
|
|
//nolint:funlen
|
|
func TestURLShortener_SaveRedirect(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
url string
|
|
alias string
|
|
error string
|
|
}{
|
|
{
|
|
name: "Valid URL",
|
|
url: gofakeit.URL(),
|
|
alias: gofakeit.Word() + gofakeit.Word(),
|
|
},
|
|
{
|
|
name: "Invalid URL",
|
|
url: "invalid_url",
|
|
alias: gofakeit.Word(),
|
|
error: "field URL is not a valid url",
|
|
},
|
|
{
|
|
name: "Empty Alias",
|
|
url: gofakeit.URL(),
|
|
alias: "",
|
|
},
|
|
// TODO: add more test cases
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
u := url.URL{
|
|
Scheme: "http",
|
|
Host: host,
|
|
}
|
|
|
|
e := httpexpect.Default(t, u.String())
|
|
|
|
// Save
|
|
|
|
resp := e.POST("/url").
|
|
WithJSON(save.Request{
|
|
URL: tc.url,
|
|
Alias: tc.alias,
|
|
}).
|
|
WithBasicAuth(user, password).
|
|
Expect().Status(http.StatusOK).
|
|
JSON().Object()
|
|
|
|
if tc.error != "" {
|
|
resp.NotContainsKey("alias")
|
|
|
|
resp.Value("error").String().IsEqual(tc.error)
|
|
|
|
return
|
|
}
|
|
|
|
alias := tc.alias
|
|
|
|
if tc.alias != "" {
|
|
resp.Value("alias").String().IsEqual(tc.alias)
|
|
} else {
|
|
resp.Value("alias").String().NotEmpty()
|
|
|
|
alias = resp.Value("alias").String().Raw()
|
|
}
|
|
|
|
// Redirect
|
|
|
|
testRedirect(t, alias, tc.url)
|
|
})
|
|
}
|
|
}
|
|
|
|
func testRedirect(t *testing.T, alias string, urlToRedirect string) {
|
|
u := url.URL{
|
|
Scheme: "http",
|
|
Host: host,
|
|
Path: alias,
|
|
}
|
|
|
|
redirectedToURL, err := api.GetRedirect(u.String())
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, urlToRedirect, redirectedToURL)
|
|
}
|