mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
d935c83a54
* feat: support baggage propagation in httpc * chore: use go 1.16 * chore: use go 1.16 * chore: use go ^1.16 * chore: remove deprecated
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/zeromicro/go-zero/core/codec"
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
)
|
|
|
|
func TestGunzipHandler(t *testing.T) {
|
|
const message = "hello world"
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
handler := GunzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body, err := io.ReadAll(r.Body)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, string(body), message)
|
|
wg.Done()
|
|
}))
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "http://localhost",
|
|
bytes.NewReader(codec.Gzip([]byte(message))))
|
|
req.Header.Set(httpx.ContentEncoding, gzipEncoding)
|
|
resp := httptest.NewRecorder()
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestGunzipHandler_NoGzip(t *testing.T) {
|
|
const message = "hello world"
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
handler := GunzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body, err := io.ReadAll(r.Body)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, string(body), message)
|
|
wg.Done()
|
|
}))
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "http://localhost",
|
|
strings.NewReader(message))
|
|
resp := httptest.NewRecorder()
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestGunzipHandler_NoGzipButTelling(t *testing.T) {
|
|
const message = "hello world"
|
|
handler := GunzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
req := httptest.NewRequest(http.MethodPost, "http://localhost",
|
|
strings.NewReader(message))
|
|
req.Header.Set(httpx.ContentEncoding, gzipEncoding)
|
|
resp := httptest.NewRecorder()
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusBadRequest, resp.Code)
|
|
}
|