-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavicon_test.go
130 lines (96 loc) · 2.72 KB
/
favicon_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
121
122
123
124
125
126
127
128
129
130
package favicon
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/go-http-utils/headers"
"github.com/stretchr/testify/suite"
)
func init() {
bytes, err := ioutil.ReadFile("./test_fixtures/favicon.ico")
if err != nil {
panic(err)
}
faviconBytes = bytes
}
var faviconBytes []byte
type FaviconSuite struct {
suite.Suite
relServer *httptest.Server
absServer *httptest.Server
}
func (s *FaviconSuite) SetupTest() {
relMux := http.NewServeMux()
relMux.Handle("/", http.HandlerFunc(helloHandlerFunc))
s.relServer = httptest.NewServer(Handler(relMux,
"./test_fixtures/favicon.ico"))
absMux := http.NewServeMux()
absMux.Handle("/", http.HandlerFunc(helloHandlerFunc))
wd, err := os.Getwd()
s.Nil(err)
s.absServer = httptest.NewServer(Handler(absMux,
filepath.Join(wd, "./test_fixtures/favicon.ico")))
panicMux := http.NewServeMux()
s.Panics(func() {
httptest.NewServer(Handler(panicMux, "invalid-path"))
})
}
func (s *FaviconSuite) TestNotFaviconRes() {
res, err := http.Get(s.relServer.URL + "/")
s.Nil(err)
s.Equal(http.StatusOK, res.StatusCode)
s.Equal([]byte("Hello World"), getResRawBody(res))
}
func (s *FaviconSuite) TestRelPathFaviconRes() {
res, err := http.Get(s.relServer.URL + "/favicon.ico")
s.Nil(err)
s.Equal(http.StatusOK, res.StatusCode)
s.Equal("image/x-icon", res.Header.Get(headers.ContentType))
s.Equal(faviconBytes, getResRawBody(res))
}
func (s *FaviconSuite) TestAbsPathFaviconRes() {
res, err := http.Get(s.absServer.URL + "/favicon.ico")
s.Nil(err)
s.Equal(http.StatusOK, res.StatusCode)
s.Equal("image/x-icon", res.Header.Get(headers.ContentType))
s.Equal(faviconBytes, getResRawBody(res))
}
func (s *FaviconSuite) TestNotAcceptPostReq() {
req, err := http.NewRequest(http.MethodPost,
s.relServer.URL+"/favicon.ico", nil)
s.Nil(err)
res, err := sendRequest(req)
s.Nil(err)
s.Equal(http.StatusMethodNotAllowed, res.StatusCode)
s.Equal("GET, HEAD, OPTIONS", res.Header.Get(headers.Allow))
}
func (s *FaviconSuite) TestAcceptOptionsReq() {
req, err := http.NewRequest(http.MethodOptions,
s.relServer.URL+"/favicon.ico", nil)
s.Nil(err)
res, err := sendRequest(req)
s.Nil(err)
s.Equal(http.StatusOK, res.StatusCode)
s.Equal("GET, HEAD, OPTIONS", res.Header.Get(headers.Allow))
}
func TestFavicon(t *testing.T) {
suite.Run(t, new(FaviconSuite))
}
func helloHandlerFunc(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
res.Write([]byte("Hello World"))
}
func getResRawBody(res *http.Response) []byte {
if b, err := ioutil.ReadAll(res.Body); err != nil {
panic(err)
} else {
return b
}
}
func sendRequest(req *http.Request) (*http.Response, error) {
cli := &http.Client{}
return cli.Do(req)
}