-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
135 lines (107 loc) · 3.85 KB
/
Copy pathmain_test.go
File metadata and controls
135 lines (107 loc) · 3.85 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main_test
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
proxy "linkpreview.net/proxy/v2"
)
func TestLinkpreviewProxy(t *testing.T) {
apihit := false
// mock linkpreview api
apiresp := []byte(`{"title":"Wikipedia","description":"Wikipedia is a free online encyclopedia, created and edited by volunteers around the world and hosted by the Wikimedia Foundation.","image":"https://www.wikipedia.org/static/apple-touch/wikipedia.png","url":"https://www.wikipedia.org/"}`)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apihit = true
w.WriteHeader(http.StatusOK)
w.Write(apiresp)
}))
defer s.Close()
proxy.LinkpreviewAPI = s.URL
// hit proxy
req := httptest.NewRequest("GET", "/linkpreview/?q=https://wikipedia.org", nil)
res := httptest.NewRecorder()
proxy.LinkpreviewProxyHandler(res, req)
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err)
// check the actual response
assert.Equal(t, true, apihit)
assert.Equal(t, http.StatusOK, res.Code)
assert.Equal(t, "application/json", res.Header().Get("Content-Type"))
assert.Equal(t, apiresp, body)
// check if we have this cached now
cacheKey := fmt.Sprintf("%s%d", "https://wikipedia.org", time.Now().Day())
cached, err := proxy.Cache.Value(cacheKey)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, cached.Data().(*proxy.CachedResponse).Status)
assert.Equal(t, apiresp, cached.Data().(*proxy.CachedResponse).Body)
}
func TestLinkpreviewProxyFromCached(t *testing.T) {
// put something in the cache
cr := proxy.CachedResponse{}
cr.Body = []byte(`{"title":"Example site","description":"Example site","image":"","url":"https://example.com/"}`)
cr.Status = http.StatusOK
cacheKey := fmt.Sprintf("%s%d", "https://example.com/test", time.Now().Day())
proxy.Cache.Add(cacheKey, 24*time.Hour, &cr)
// hit proxy
req := httptest.NewRequest("GET", "/linkpreview/?q=https://example.com/test", nil)
res := httptest.NewRecorder()
proxy.LinkpreviewProxyHandler(res, req)
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.Code)
assert.Equal(t, "application/json", res.Header().Get("Content-Type"))
assert.Contains(t, string(body), "Example site")
}
func TestGoodReferrer(t *testing.T) {
passed := false
s := proxy.MWrefererCheck("https://example.com/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
passed = true
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Referer", "https://example.com/")
res := httptest.NewRecorder()
// hit
s(res, req)
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err)
// check the actual response
assert.Equal(t, http.StatusOK, res.Code)
assert.Equal(t, true, passed)
assert.Equal(t, "", string(body))
}
func TestNoReferrer(t *testing.T) {
passed := false
s := proxy.MWrefererCheck("https://example.com/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
passed = true
}))
req := httptest.NewRequest("GET", "/", nil)
res := httptest.NewRecorder()
// hit
s(res, req)
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err)
// check the actual response
assert.Equal(t, http.StatusForbidden, res.Code)
assert.Equal(t, false, passed)
assert.Equal(t, "Forbidden\n", string(body))
}
func TestBadReferrer(t *testing.T) {
passed := false
s := proxy.MWrefererCheck("https://example.com/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
passed = true
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Referer", "https://strange.com/")
res := httptest.NewRecorder()
// hit
s(res, req)
body, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err)
// check the actual response
assert.Equal(t, http.StatusForbidden, res.Code)
assert.Equal(t, false, passed)
assert.Equal(t, "Forbidden\n", string(body))
}