Testing and Link disolve Close #8
parent
221b8ff49a
commit
2fcd341fd9
@ -1,2 +1,3 @@
|
||||
sgot.db
|
||||
test.db
|
||||
shrt
|
||||
|
@ -0,0 +1,106 @@
|
||||
package shrty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"crypto/sha256"
|
||||
"time"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
URL string `json:"url"`
|
||||
URLFetched string `json:"url_fetched"`
|
||||
CanonicalURL string `json:"canonical_url"`
|
||||
OriginalURL string `json:"original_url"`
|
||||
Domain string `json:"domain"`
|
||||
Hash string `json:"hash"`
|
||||
Token string `json:"token"`
|
||||
Meta `json:"meta"`
|
||||
FavIconLink string `json:"favicon_url"`
|
||||
HTTPStatusCode string `json:"http_code"`
|
||||
Category string `json:"category"`
|
||||
Created string `json:"created_at"`
|
||||
Clicks int64 `json:"clicks"`
|
||||
}
|
||||
|
||||
type Meta struct{
|
||||
Title string `json:"title"meta:"og:title"`
|
||||
Image string `json:"image"meta:"og:image"`
|
||||
Description string `json:"description"meta:"og:description"`
|
||||
Type string `json:"type"meta:"og:type"`
|
||||
}
|
||||
|
||||
func (s *Store) Short(URL string, value *string) error {
|
||||
d := &Data{
|
||||
URL: URL,
|
||||
}
|
||||
|
||||
err := CheckURL(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create a sha256 Hash from the URL
|
||||
d.Hash = fmt.Sprintf("%x", sha256.Sum256([]byte(d.OriginalURL)))
|
||||
|
||||
// Check if the URL already in the Storage
|
||||
if ok := s.Exist(d); ok {
|
||||
*value = d.Token
|
||||
return nil
|
||||
}
|
||||
|
||||
// Iterate to the length of the hash to get the shortest output
|
||||
for hashShortestLen := 1; hashShortestLen <= 32; hashShortestLen++ {
|
||||
// Test if the Token not exist and return the new generated token
|
||||
if _, ok := s.Token[d.Hash[:hashShortestLen]]; !ok {
|
||||
d.Token = d.Hash[:hashShortestLen]
|
||||
d.Created = time.Now().String()
|
||||
|
||||
s.Token[d.Token] = d
|
||||
s.Url[d.Hash] = d.Token
|
||||
*value = d.Token
|
||||
|
||||
s.Save()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return ErrCreateToken
|
||||
}
|
||||
|
||||
// URL already Exist in the
|
||||
func (s *Store) Exist(d *Data) bool {
|
||||
if val, ok := s.Url[d.Hash]; ok {
|
||||
d = s.Token[val]
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Returns the URL for the given token
|
||||
func (s *Store) Redirect(token string) (string, bool) {
|
||||
if shrt, ok := s.Token[token]; ok {
|
||||
s.Token[token].Clicks += 1
|
||||
s.Save()
|
||||
return shrt.OriginalURL, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Get Data for the given Token
|
||||
func (s *Store) Get(token string, value *Data) bool {
|
||||
if data, ok := s.Token[token]; ok {
|
||||
*value = *data
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Get all entries
|
||||
func (s *Store) GetAll() string {
|
||||
b, err := json.Marshal(&s)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
@ -0,0 +1,168 @@
|
||||
package shrty
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const path = "short_test.db"
|
||||
|
||||
// This test seems to be not working correctly
|
||||
func TestStorage_Short(t *testing.T) {
|
||||
s, err := Open(path)
|
||||
defer os.Remove(path)
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
var token string
|
||||
|
||||
err = s.Short("http://bit.ly/2scBYES", &token)
|
||||
|
||||
expected := &Data{
|
||||
URL:"http://bit.ly/2scBYES",
|
||||
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
||||
CanonicalURL:"",
|
||||
OriginalURL:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
||||
Domain:"www.youtube.com",
|
||||
Hash:"c921ca733b92ca7b57782e9f12a3fe60dbc6b91a317fc419e13f8f5b5805232f",
|
||||
Token:"c",
|
||||
Meta: Meta{
|
||||
Title:"",
|
||||
Image:"",
|
||||
Description:"",
|
||||
Type:""},
|
||||
FavIconLink:"",
|
||||
HTTPStatusCode:"200",
|
||||
Category:"",
|
||||
Created:s.Token[token].Created,
|
||||
Clicks:0,
|
||||
}
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, s.Token[token])
|
||||
}
|
||||
|
||||
// Whats wrong with this Test?
|
||||
func TestStorage_Get(t *testing.T) {
|
||||
s, err := Open(path)
|
||||
defer os.Remove(path)
|
||||
|
||||
var d Data
|
||||
empty := Data{}
|
||||
|
||||
ok := s.Get("notexist", &d)
|
||||
|
||||
assert.Equal(t, empty, d)
|
||||
assert.Equal(t, false, ok)
|
||||
|
||||
var token string
|
||||
err = s.Short("http://bit.ly/2scBYES", &token)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "c", token)
|
||||
|
||||
expected := &Data{
|
||||
URL:"http://bit.ly/2scBYES",
|
||||
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
||||
CanonicalURL:"",
|
||||
OriginalURL:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
||||
Domain:"www.youtube.com",
|
||||
Hash:"c921ca733b92ca7b57782e9f12a3fe60dbc6b91a317fc419e13f8f5b5805232f",
|
||||
Token:"c",
|
||||
Meta: Meta{
|
||||
Title:"",
|
||||
Image:"",
|
||||
Description:"",
|
||||
Type:""},
|
||||
FavIconLink:"",
|
||||
HTTPStatusCode:"200",
|
||||
Category:"",
|
||||
Created:s.Token[token].Created,
|
||||
Clicks:0,
|
||||
}
|
||||
|
||||
ok = s.Get(token, &d)
|
||||
|
||||
assert.Equal(t, expected, &d)
|
||||
}
|
||||
|
||||
func TestStore_Exist(t *testing.T) {
|
||||
s, err := Open("test.db")
|
||||
defer os.Remove(path)
|
||||
|
||||
d := &Data{
|
||||
URL:"http://bit.ly/2scBYES",
|
||||
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
||||
CanonicalURL:"",
|
||||
OriginalURL:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
||||
Domain:"www.youtube.com",
|
||||
Hash:"c921ca733b92ca7b57782e9f12a3fe60dbc6b91a317fc419e13f8f5b5805232f",
|
||||
Token:"c",
|
||||
Meta: Meta{
|
||||
Title:"",
|
||||
Image:"",
|
||||
Description:"",
|
||||
Type:""},
|
||||
FavIconLink:"",
|
||||
HTTPStatusCode:"200",
|
||||
Category:"",
|
||||
Created:time.Now().String(),
|
||||
Clicks:0,
|
||||
}
|
||||
|
||||
notexist := s.Exist(d)
|
||||
assert.Equal(t, false, notexist)
|
||||
|
||||
var token string
|
||||
|
||||
err = s.Short(d.URL, &token)
|
||||
assert.NoError(t, err)
|
||||
|
||||
exist := s.Exist(d)
|
||||
|
||||
assert.Equal(t, true, exist)
|
||||
}
|
||||
|
||||
func TestStore_Redirect(t *testing.T) {
|
||||
s, err := Open(path)
|
||||
defer os.Remove(path)
|
||||
|
||||
url, ok := s.Redirect("notexist")
|
||||
|
||||
assert.Equal(t, "", url)
|
||||
assert.Equal(t, false, ok)
|
||||
|
||||
var token string
|
||||
err = s.Short("http://bit.ly/2scBYES", &token)
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := &Data{
|
||||
URL:"http://bit.ly/2scBYES",
|
||||
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
||||
CanonicalURL:"",
|
||||
OriginalURL:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
||||
Domain:"www.youtube.com",
|
||||
Hash:"c921ca733b92ca7b57782e9f12a3fe60dbc6b91a317fc419e13f8f5b5805232f",
|
||||
Token:"c",
|
||||
Meta: Meta{
|
||||
Title:"",
|
||||
Image:"",
|
||||
Description:"",
|
||||
Type:""},
|
||||
FavIconLink:"",
|
||||
HTTPStatusCode:"200",
|
||||
Category:"",
|
||||
Created:time.Now().String(),
|
||||
Clicks:0,
|
||||
}
|
||||
|
||||
url, ok = s.Redirect(token)
|
||||
|
||||
assert.Equal(t, expected.OriginalURL, url)
|
||||
}
|
||||
|
||||
//func TestStore_GetAll(t *testing.T) {
|
||||
|
||||
//}
|
@ -0,0 +1,17 @@
|
||||
package shrty
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestOpen(t *testing.T) {
|
||||
s, err := Open(path)
|
||||
defer os.Remove(path)
|
||||
|
||||
expected := Store{Token: make(map[string]*Data), Url: make(map[string]string)}
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, &expected, s)
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package shrty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
func CheckURL(d *Data) error {
|
||||
resp, err := http.Get(d.URL)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
||||
d.HTTPStatusCode = fmt.Sprint(resp.StatusCode)
|
||||
d.URLFetched = fmt.Sprint(resp.Request.URL)
|
||||
d.OriginalURL = fmt.Sprint(resp.Request.URL)
|
||||
d.Domain = resp.Request.URL.Host
|
||||
|
||||
return err
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package shrty
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCheckURL(t *testing.T) {
|
||||
data := Data{
|
||||
URL: "https://t.co/kA45uWnKkg",
|
||||
}
|
||||
|
||||
expected := Data{
|
||||
URL: "https://t.co/kA45uWnKkg",
|
||||
URLFetched: "https://www.youtube.com/watch?v=Qg0pO9VG1J8&utm_content=buffera4269&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer",
|
||||
OriginalURL: "https://www.youtube.com/watch?v=Qg0pO9VG1J8&utm_content=buffera4269&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer",
|
||||
HTTPStatusCode: "200",
|
||||
Domain: "www.youtube.com",
|
||||
}
|
||||
|
||||
err := CheckURL(&data)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, data)
|
||||
}
|
Loading…
Reference in New Issue