-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
572e35c
commit 8947f61
Showing
5 changed files
with
183 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
# favicon | ||
[![Build Status](https://travis-ci.org/go-http-utils/favicon.svg?branch=master)](https://travis-ci.org/go-http-utils/favicon) | ||
[![Coverage Status](https://coveralls.io/repos/github/go-http-utils/favicon/badge.svg?branch=master)](https://coveralls.io/github/go-http-utils/favicon?branch=master) | ||
|
||
Go http middleware for serving the favicon. | ||
|
||
## Installation | ||
|
||
go get -u github.com/go-http-utils/favicon | ||
|
||
## Documentation | ||
|
||
API documentation can be found here: https://godoc.org/github.com/go-http-utils/favicon | ||
|
||
## Usage | ||
|
||
```go | ||
import ( | ||
"github.com/go-http-utils/favicon" | ||
) | ||
``` | ||
|
||
```go | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { | ||
res.Write([]byte("Hello World")) | ||
}) | ||
|
||
http.ListenAndServe(":8080", favicon.Handler(mux, "./public/favicon.ico")) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package favicon_test | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-http-utils/favicon" | ||
) | ||
|
||
func Example() { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { | ||
res.Write([]byte("Hello World")) | ||
}) | ||
|
||
http.ListenAndServe(":8080", favicon.Handler(mux, "./public/favicon.ico")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,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) | ||
} |
Binary file not shown.