File: gzip_test.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 872 kB
  • sloc: makefile: 4
file content (63 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download | duplicates (2)
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
package httptoo

import (
	"compress/gzip"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

const helloWorld = "hello, world\n"

func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
	// w.Header().Set("Content-Length", strconv.FormatInt(int64(len(helloWorld)), 10))
	w.Write([]byte(helloWorld))
}

func requestResponse(h http.Handler, r *http.Request) (*http.Response, error) {
	s := httptest.NewServer(h)
	defer s.Close()
	return http.DefaultClient.Do(r)
}

func TestGzipHandler(t *testing.T) {
	rr := httptest.NewRecorder()
	helloWorldHandler(rr, nil)
	assert.EqualValues(t, helloWorld, rr.Body.String())

	rr = httptest.NewRecorder()
	GzipHandler(http.HandlerFunc(helloWorldHandler)).ServeHTTP(rr, new(http.Request))
	assert.EqualValues(t, helloWorld, rr.Body.String())

	rr = httptest.NewRecorder()
	r, err := http.NewRequest("GET", "/", nil)
	require.NoError(t, err)
	r.Header.Set("Accept-Encoding", "gzip")
	GzipHandler(http.HandlerFunc(helloWorldHandler)).ServeHTTP(rr, r)
	gr, err := gzip.NewReader(rr.Body)
	require.NoError(t, err)
	defer gr.Close()
	b, err := ioutil.ReadAll(gr)
	require.NoError(t, err)
	assert.EqualValues(t, helloWorld, b)

	s := httptest.NewServer(nil)
	s.Config.Handler = GzipHandler(http.HandlerFunc(helloWorldHandler))
	req, err := http.NewRequest("GET", s.URL, nil)
	req.Header.Set("Accept-Encoding", "gzip")
	resp, err := http.DefaultClient.Do(req)
	require.NoError(t, err)
	gr.Close()
	gr, err = gzip.NewReader(resp.Body)
	require.NoError(t, err)
	defer gr.Close()
	b, err = ioutil.ReadAll(gr)
	require.NoError(t, err)
	assert.EqualValues(t, helloWorld, b)
	assert.EqualValues(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
	assert.EqualValues(t, "gzip", resp.Header.Get("Content-Encoding"))
}