Skip to content
Snippets Groups Projects
Commit 7e6536fb authored by Will McCutchen's avatar Will McCutchen
Browse files

Add /gzip handler

parent 1f30f965
No related branches found
No related tags found
No related merge requests found
package httpbin
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"net/http"
......@@ -67,6 +69,27 @@ func (h *HTTPBin) RequestWithBody(w http.ResponseWriter, r *http.Request) {
writeJSON(w, body, http.StatusOK)
}
// Gzip returns a gzipped response
func (h *HTTPBin) Gzip(w http.ResponseWriter, r *http.Request) {
resp := &gzipResponse{
Headers: r.Header,
Origin: getOrigin(r),
Gzipped: true,
}
body, _ := json.Marshal(resp)
buf := &bytes.Buffer{}
gzw := gzip.NewWriter(buf)
gzw.Write(body)
gzw.Close()
gzBody := buf.Bytes()
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(gzBody)))
writeJSON(w, gzBody, http.StatusOK)
}
// IP echoes the IP address of the incoming request
func (h *HTTPBin) IP(w http.ResponseWriter, r *http.Request) {
body, _ := json.Marshal(&ipResponse{
......
......@@ -2,13 +2,16 @@ package httpbin
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"strings"
"testing"
"time"
......@@ -1075,3 +1078,47 @@ func TestDigestAuth(t *testing.T) {
})
}
}
func TestGzip(t *testing.T) {
r, _ := http.NewRequest("GET", "/gzip", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
assertContentType(t, w, "application/json; encoding=utf-8")
assertHeader(t, w, "Content-Encoding", "gzip")
assertStatusCode(t, w, http.StatusOK)
zippedContentLengthStr := w.HeaderMap.Get("Content-Length")
if zippedContentLengthStr == "" {
t.Fatalf("missing Content-Length header in response")
}
zippedContentLength, err := strconv.Atoi(zippedContentLengthStr)
if err != nil {
t.Fatalf("error converting Content-Lengh %v to integer: %s", zippedContentLengthStr, err)
}
gzipReader, err := gzip.NewReader(w.Body)
if err != nil {
t.Fatalf("error creating gzip reader: %s", err)
}
unzippedBody, err := ioutil.ReadAll(gzipReader)
if err != nil {
t.Fatalf("error reading gzipped body: %s", err)
}
var resp *gzipResponse
err = json.Unmarshal(unzippedBody, &resp)
if err != nil {
t.Fatalf("error unmarshalling response: %s", err)
}
if resp.Gzipped != true {
t.Fatalf("expected resp.Gzipped == true")
}
if len(unzippedBody) >= zippedContentLength {
t.Fatalf("expected compressed body")
}
}
......@@ -46,6 +46,12 @@ type authResponse struct {
User string `json:"user"`
}
type gzipResponse struct {
Headers http.Header `json:"headers"`
Origin string `json:"origin"`
Gzipped bool `json:"gzipped"`
}
// Options are used to configure HTTPBin
type Options struct {
MaxMemory int64
......@@ -88,6 +94,8 @@ func (h *HTTPBin) Handler() http.Handler {
mux.HandleFunc("/hidden-basic-auth/", h.HiddenBasicAuth)
mux.HandleFunc("/digest-auth/", h.DigestAuth)
mux.HandleFunc("/gzip", h.Gzip)
// Make sure our ServeMux doesn't "helpfully" redirect these invalid
// endpoints by adding a trailing slash. See the ServeMux docs for more
// info: https://golang.org/pkg/net/http/#ServeMux
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment