-
Notifications
You must be signed in to change notification settings - Fork 14
/
compression_handler_test.go
170 lines (137 loc) · 11.8 KB
/
compression_handler_test.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package nethttplibrary
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
nethttp "net/http"
httptest "net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompressionHandlerAddsAcceptEncodingHeader(t *testing.T) {
postBody, _ := json.Marshal(map[string]string{"name": "Test", "email": "[email protected]"})
var acceptEncodingHeader string
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
acceptEncodingHeader = req.Header.Get("Accept-Encoding")
fmt.Fprint(res, `{}`)
}))
defer testServer.Close()
client := GetDefaultClient(NewCompressionHandler())
client.Post(testServer.URL, "application/json", bytes.NewBuffer(postBody))
assert.Equal(t, acceptEncodingHeader, "gzip")
}
func TestCompressionHandlerAddsContentEncodingHeader(t *testing.T) {
postBody, _ := json.Marshal(map[string]string{"name": "Test", "email": "[email protected]"})
var contentTypeHeader string
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
contentTypeHeader = req.Header.Get("Content-Encoding")
fmt.Fprint(res, `{}`)
}))
defer testServer.Close()
client := GetDefaultClient(NewCompressionHandler())
client.Post(testServer.URL, "application/json", bytes.NewBuffer(postBody))
assert.Equal(t, contentTypeHeader, "gzip")
}
func TestCompressionHandlerCompressesRequestBody(t *testing.T) {
postBody, _ := json.Marshal(map[string]string{"name": `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.a line in section 1.10.32.
`, "email": "[email protected]"})
var compressedBody []byte
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
compressedBody, _ = io.ReadAll(req.Body)
fmt.Fprint(res, `{}`)
}))
defer testServer.Close()
client := GetDefaultClient(NewCompressionHandler())
client.Post(testServer.URL, "application/json", bytes.NewBuffer(postBody))
assert.Greater(t, len(postBody), len(compressedBody))
}
func TestCompressionHandlerCompressesRequestBodyWithRetry(t *testing.T) {
postBody, _ := json.Marshal(map[string]string{"name": `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.a line in section 1.10.32.
`, "email": "[email protected]"})
var compressedBody []byte
var requestCount int
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
if requestCount == 0 {
res.WriteHeader(http.StatusTooManyRequests)
requestCount++
return
}
compressedBody, _ = io.ReadAll(req.Body)
fmt.Fprint(res, `{}`)
}))
defer testServer.Close()
client := GetDefaultClient(NewCompressionHandler(), NewRetryHandler())
_, err := client.Post(testServer.URL, "application/json", bytes.NewBuffer(postBody))
assert.NotZero(t, len(compressedBody))
assert.Greater(t, len(postBody), len(compressedBody))
assert.Equal(t, requestCount, 1)
assert.NoError(t, err)
}
func TestCompressionHandlerContentRangeRequestBody(t *testing.T) {
postBody, _ := json.Marshal(map[string]string{"name": `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.a line in section 1.10.32.
`, "email": "[email protected]"})
var compressedBody []byte
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
compressedBody, _ = io.ReadAll(req.Body)
fmt.Fprint(res, `{}`)
}))
defer testServer.Close()
client := GetDefaultClient(NewCompressionHandler())
req, _ := nethttp.NewRequest("PUT", testServer.URL, bytes.NewBuffer(postBody))
req.Header.Add("Content-Range", "bytes 0-3/4")
client.Do(req)
assert.Equal(t, len(postBody), len(compressedBody))
}
func TestCompressionHandlerContentEncodingRequestBody(t *testing.T) {
postBody, _ := json.Marshal(map[string]string{"name": `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.a line in section 1.10.32.
`, "email": "[email protected]"})
var compressedBody []byte
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
compressedBody, _ = io.ReadAll(req.Body)
fmt.Fprint(res, `{}`)
}))
defer testServer.Close()
client := GetDefaultClient(NewCompressionHandler())
req, _ := nethttp.NewRequest("PUT", testServer.URL, bytes.NewBuffer(postBody))
req.Header.Add("Content-Encoding", "gzip")
client.Do(req)
assert.Equal(t, len(postBody), len(compressedBody))
}
func TestCompressionHandlerRetriesRequest(t *testing.T) {
postBody, _ := json.Marshal(map[string]string{"name": "Test", "email": "[email protected]"})
status := 415
reqCount := 0
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
defer req.Body.Close()
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(status)
status = 200
reqCount += 1
fmt.Fprint(res, `{}`)
}))
defer testServer.Close()
client := getDefaultClientWithoutMiddleware()
client.Transport = NewCustomTransport(NewCompressionHandler())
client.Post(testServer.URL, "application/json", bytes.NewBuffer(postBody))
assert.Equal(t, reqCount, 2)
}
func TestCompressionHandlerWorksWithEmptyBody(t *testing.T) {
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
result, _ := json.Marshal(map[string]string{"name": "Test", "email": "[email protected]"})
res.Header().Set("Content-Type", "application/json")
res.Header().Set("Content-Encoding", "gzip")
fmt.Fprint(res, result)
}))
defer testServer.Close()
client := getDefaultClientWithoutMiddleware()
client.Transport = NewCustomTransport(NewCompressionHandler())
fmt.Print(testServer.URL)
resp, _ := client.Get(testServer.URL)
assert.NotNil(t, resp)
}
func TestResetTransport(t *testing.T) {
client := getDefaultClientWithoutMiddleware()
client.Transport = &nethttp.Transport{}
}