package shortener import ( "crypto/sha1" "errors" "fmt" "strings" "testing" "github.com/jimeh/ozu.io/shortener/mocks" "github.com/stretchr/testify/suite" ) // Mocks //go:generate mockery -name Store -dir .. -recursive // Suite Setup type Base58ShortenerSuite struct { suite.Suite store *mocks.Store shortener *Base58Shortener errNotFound error } func (s *Base58ShortenerSuite) SetupTest() { s.store = new(mocks.Store) s.shortener = NewBase58(s.store) s.errNotFound = errors.New("not found") } // Tests func (s *Base58ShortenerSuite) TestShortenExisting() { rawURL := []byte("http://google.com/") uid := []byte("ig") urlSHA := fmt.Sprintf("%x", sha1.Sum(rawURL)) s.store.On("Get", append([]byte("url:"), urlSHA...)).Return(uid, nil) resultUID, resultURL, err := s.shortener.Shorten(rawURL) s.NoError(err) s.Equal(uid, resultUID) s.Equal(rawURL, resultURL) s.store.AssertExpectations(s.T()) } func (s *Base58ShortenerSuite) TestShortenNew() { rawURL := []byte("https://google.com") url := []byte("https://google.com/") uid := []byte("ig") urlKey := append([]byte("url:"), fmt.Sprintf("%x", sha1.Sum(url))...) s.store.On("Get", urlKey).Return(nil, s.errNotFound) s.store.On("NextSequence").Return(1001, nil) s.store.On("Set", urlKey, uid).Return(nil) s.store.On("Set", append([]byte("uid:"), uid...), url).Return(nil) rUID, rURL, err := s.shortener.Shorten(rawURL) s.NoError(err) s.Equal(uid, rUID) s.Equal(url, rURL) s.store.AssertExpectations(s.T()) } func (s *Base58ShortenerSuite) TestShortenInvalidURL() { examples := []struct { url string error string }{ { url: "*$)]+_