/
app_test.go
81 lines (63 loc) · 1.77 KB
/
app_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/mock"
)
const (
expTime = 60
longURL = "https://www.qq.com"
shortLink = "IJiwa"
shortLinkInfor = `{"url": "https://www.qq.com", "created_at":"2018-03-21 15:33:13"`
)
type storageMock struct {
mock.Mock
}
var app App
var mockR *storageMock
func (s *storageMock) Shorten(url string, exp int64) (string, error) {
args := s.Called(url, exp)
return args.String(0), args.Error(1)
}
func (s *storageMock) Unshorten(eid string) (string, error) {
args := s.Called(eid)
return args.String(0), args.Error(1)
}
func (s *storageMock) ShortlinkInfo(eid string) (interface{}, error) {
args := s.Called(eid)
return args.String(0), args.Error(1)
}
func init() {
app = App{}
mockR = new(storageMock)
app.Initialize(&Env{S: mockR})
}
func TestCreateShortlink(t *testing.T) {
var jsonStr = []byte(`{
"url": "https://www.qq.com"
"expiration_in_minutes": 60
}`)
req, err := http.NewRequest("POST", "/api/shorten", bytes.NewBuffer(jsonStr))
if err != nil {
t.Fatal("during create a request")
}
req.Header.Set("Content-Type", "application/json")
mockR.On("Shorten", longURL, int64(expTime)).Return(shortLink, nil).Once()
rw := httptest.NewRecorder()
app.Router.ServeHTTP(rw, req)
if rw.Code != http.StatusCreated {
t.Fatalf("Excepted receive %d. Got %d", http.StatusCreated, rw.Code)
}
resp := struct {
Shortlink string `json:"shortlink"`
}{}
if err := json.NewDecoder(rw.Body).Decode(&resp); err != nil {
t.Fatalf("during decode the response")
}
if resp.Shortlink != shortLink {
t.Fatalf("during receive %s. Got %s", shortLink, resp.Shortlink)
}
}