-
Notifications
You must be signed in to change notification settings - Fork 0
/
short_test.go
61 lines (49 loc) · 1.87 KB
/
short_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package short
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestShort(t *testing.T) {
t.Run("CreateShortenedUrl and GetUrlFromShortenedUrl", func(t *testing.T) {
host := "host.com:12345"
url := "https://test.com/?sdfsdfsd"
shortner, err := NewShortener(DefaultConfig().WithHost(host).WithMongoUri(getRandomMongoURIForTesting()))
require.Nil(t, err)
t.Run("valid url", func(t *testing.T) {
surl, err := shortner.CreateShortenedUrl(context.Background(), url)
require.Nil(t, err)
require.Contains(t, surl.GetUrl(), "https://host.com:12345/")
rurl, err := shortner.GetUrlFromShortenedUrl(context.Background(), surl.GetUrl())
require.Nil(t, err)
require.Equal(t, url, rurl)
})
t.Run("invalid url", func(t *testing.T) {
_, err := shortner.CreateShortenedUrl(context.Background(), "http1s://test.com/?sdfsdfsd")
require.Error(t, err)
})
t.Run("invalid short url", func(t *testing.T) {
surl, err := shortner.CreateShortenedUrl(context.Background(), url)
require.Nil(t, err)
_, err = shortner.GetUrlFromShortenedUrl(context.Background(), surl.GetUrl()+"!")
require.Error(t, err)
require.Contains(t, err.Error(), "invalid short url path")
})
t.Run("id not found", func(t *testing.T) {
_, err = shortner.GetUrlFromShortenedUrl(context.Background(), "https://host.com:12345/aaaaaaa")
require.Error(t, err)
var perr *IdNotFoundError
require.ErrorAs(t, err, &perr)
require.Contains(t, err.Error(), "aaaaaaa")
})
t.Run("expired short url", func(t *testing.T) {
surl, err := shortner.CreateShortenedUrl(context.Background(), url, DefaultUrlConfig().WithExpirationDate(time.Now().Add(-time.Hour)))
require.Nil(t, err)
_, err = shortner.GetUrlFromShortenedUrl(context.Background(), surl.GetUrl())
require.Error(t, err)
var perr *IdNotFoundError
require.ErrorAs(t, err, &perr)
})
})
}