Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /brotli endpoint #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This way, you can write tests without relying on an external dependency like [ht
- `/cache/:n` Sets a Cache-Control header for _n_ seconds.
- `/gzip` Returns gzip-encoded data.
- `/deflate` Returns deflate-encoded data.
- `/brotli` Returns brotli-encoded data.
- `/robots.txt` Returns some robots.txt rules.
- `/deny` Denied by robots.txt file.
- `/basic-auth/:user/:passwd` Challenges HTTP Basic Auth.
Expand Down
21 changes: 21 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/gorilla/mux"
"github.com/pkg/errors"
brotlienc "gopkg.in/kothar/brotli-go.v0/enc"
)

var (
Expand Down Expand Up @@ -64,6 +65,7 @@ func GetMux() *mux.Router {
r.HandleFunc(`/cache`, CacheHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(`/cache/{n:[\d]+}`, SetCacheHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(`/gzip`, GZIPHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(`/brotli`, BrotliHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(`/deflate`, DeflateHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(`/html`, HTMLHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc(`/xml`, XMLHandler).Methods(http.MethodGet, http.MethodHead)
Expand Down Expand Up @@ -441,6 +443,25 @@ func DeflateHandler(w http.ResponseWriter, r *http.Request) {
}
}

// BrotliHandler returns a Brotli-encoded response
func BrotliHandler(w http.ResponseWriter, r *http.Request) {
h, _, _ := net.SplitHostPort(r.RemoteAddr)

v := brotliResponse{
headersResponse: headersResponse{getHeaders(r)},
ipResponse: ipResponse{h},
Compressed: true,
}

w.Header().Set("Content-Type", "application/json")
w.Header().Add("Content-Encoding", "br")
ww := brotlienc.NewBrotliWriter(nil, w)
defer ww.Close() // flush
if err := writeJSON(ww, v); err != nil {
writeErrorJSON(w, errors.Wrap(err, "failed to write json"))
}
}

// RobotsTXTHandler returns a robots.txt response.
func RobotsTXTHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
Expand Down
25 changes: 25 additions & 0 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/ahmetb/go-httpbin"
"github.com/stretchr/testify/require"
brotlidec "gopkg.in/kothar/brotli-go.v0/dec"
)

var (
Expand Down Expand Up @@ -568,6 +569,30 @@ func TestDeflate(t *testing.T) {
require.True(t, v.Deflated)
}

func TestBrotli(t *testing.T) {
srv := testServer()
defer srv.Close()

client := new(http.Client)
req, err := http.NewRequest("GET", srv.URL+"/brotli", nil)
require.Nil(t, err)

req.Header.Add("Accept-Encoding", "br")
resp, err := client.Do(req)
require.Nil(t, err)
defer resp.Body.Close()

require.EqualValues(t, "br", resp.Header.Get("Content-Encoding"))
require.EqualValues(t, "application/json", resp.Header.Get("Content-Type"))
zr := brotlidec.NewBrotliReader(resp.Body)

var v struct {
Compressed bool `json:"compressed"`
}
require.Nil(t, json.NewDecoder(zr).Decode(&v))
require.True(t, v.Compressed)
}

func TestRobotsTXT(t *testing.T) {
srv := testServer()
defer srv.Close()
Expand Down
6 changes: 6 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ type deflateResponse struct {
Deflated bool `json:"deflated"`
}

type brotliResponse struct {
headersResponse
ipResponse
Compressed bool `json:"compressed"`
}

type basicAuthResponse struct {
Authenticated bool `json:"authenticated"`
User string `json:"string"`
Expand Down