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
|
package upstream
import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestGzipEncoding(t *testing.T) {
resp := httptest.NewRecorder()
var b bytes.Buffer
w := gzip.NewWriter(&b)
fmt.Fprint(w, "test")
w.Close()
body := ioutil.NopCloser(&b)
req, err := http.NewRequest("POST", "http://address/test", body)
require.NoError(t, err)
req.Header.Set("Content-Encoding", "gzip")
contentEncodingHandler(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
require.IsType(t, &gzip.Reader{}, r.Body, "body type")
require.Empty(t, r.Header.Get("Content-Encoding"), "Content-Encoding should be deleted")
})).ServeHTTP(resp, req)
require.Equal(t, 200, resp.Code)
}
func TestNoEncoding(t *testing.T) {
resp := httptest.NewRecorder()
var b bytes.Buffer
body := ioutil.NopCloser(&b)
req, err := http.NewRequest("POST", "http://address/test", body)
require.NoError(t, err)
req.Header.Set("Content-Encoding", "")
contentEncodingHandler(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
require.Equal(t, body, r.Body, "Expected the same body")
require.Empty(t, r.Header.Get("Content-Encoding"), "Content-Encoding should be deleted")
})).ServeHTTP(resp, req)
require.Equal(t, 200, resp.Code)
}
func TestInvalidEncoding(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequest("POST", "http://address/test", nil)
require.NoError(t, err)
req.Header.Set("Content-Encoding", "application/unknown")
contentEncodingHandler(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
t.Fatal("it shouldn't be executed")
})).ServeHTTP(resp, req)
require.Equal(t, 500, resp.Code)
}
|