This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
redirect_test.go
120 lines (87 loc) · 3.45 KB
/
redirect_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"errors"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
var getPostSuccess = func(t *testing.T, expId string) func(id string, r *http.Request) (Post, error) {
return func(id string, r *http.Request) (Post, error) {
assert.Equal(t, id, expId, "wrong post id")
return Post{
Url: "https://www.dailynow.co",
Id: id,
}, nil
}
}
func TestRedirectOnBot(t *testing.T) {
getPost = getPostSuccess(t, "post_id")
req, err := http.NewRequest("GET", "/r/post_id", nil)
assert.Nil(t, err)
req.Header.Set("user-agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
rr := httptest.NewRecorder()
router := createApp()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusMovedPermanently, rr.Code, "wrong status code")
assert.Equal(t, "https://www.dailynow.co", rr.Header().Get("Location"), "wrong redirect")
}
func TestRedirectOnBrowser(t *testing.T) {
getPost = getPostSuccess(t, "post_id2")
req, err := http.NewRequest("GET", "/r/post_id2", nil)
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
assert.Nil(t, err)
rr := httptest.NewRecorder()
router := createApp()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code, "wrong status code")
res := "<html><head><meta http-equiv=\"refresh\" content=\"0;URL=https://www.dailynow.co\"></head></html>\n"
assert.Equal(t, res, rr.Body.String(), "wrong response")
}
func TestViewPublish(t *testing.T) {
agent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
called := false
getPost = getPostSuccess(t, "post_id3")
publishView = func(view View) error {
called = true
assert.Equal(t, "post_id3", view.PostId)
assert.Equal(t, "user_id", view.UserId)
assert.Equal(t, agent, view.Agent)
return nil
}
req, err := http.NewRequest("GET", "/r/post_id3", nil)
req.Header.Set("user-agent", agent)
req.Header.Set("user-id", "user_id")
assert.Nil(t, err)
rr := httptest.NewRecorder()
router := createApp()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code, "wrong status code")
res := "<html><head><meta http-equiv=\"refresh\" content=\"0;URL=https://www.dailynow.co\"></head></html>\n"
assert.Equal(t, rr.Body.String(), res, "wrong response")
assert.Equal(t, true, called, "publishView should be called")
}
func TestAPIFail(t *testing.T) {
getPost = func(id string, r *http.Request) (Post, error) {
return Post{}, errors.New("fail")
}
req, err := http.NewRequest("GET", "/r/post_id4", nil)
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
assert.Nil(t, err)
rr := httptest.NewRecorder()
router := createApp()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusServiceUnavailable, rr.Code, "wrong status code")
}
func TestNotFound(t *testing.T) {
getPost = func(id string, r *http.Request) (Post, error) {
return Post{}, errors.New("not found")
}
req, err := http.NewRequest("GET", "/r/post_id5", nil)
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
assert.Nil(t, err)
rr := httptest.NewRecorder()
router := createApp()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotFound, rr.Code, "wrong status code")
}