
* refactor: take clients out of internal * refactor: move stdio to pkg * Move internal/api to api * refactor: final changes for Kapacitor to access shared functionality * chore: regenerate mocks * fix: bad automated refactor * chore: extra formatting not caught by make fmt
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"context"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNoGzipRequest(t *testing.T) {
|
|
client := APIClient{cfg: NewConfiguration()}
|
|
body := []byte("This should not get gzipped")
|
|
req, err := client.prepareRequest(
|
|
context.Background(),
|
|
"/foo", "POST", body,
|
|
map[string]string{},
|
|
nil, nil, "", "", nil,
|
|
)
|
|
require.NoError(t, err)
|
|
defer req.Body.Close()
|
|
|
|
out := bytes.Buffer{}
|
|
_, err = io.Copy(&out, req.Body)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(body), out.String())
|
|
}
|
|
|
|
func TestGzipRequest(t *testing.T) {
|
|
client := APIClient{cfg: NewConfiguration()}
|
|
body := []byte("This should get gzipped")
|
|
req, err := client.prepareRequest(
|
|
context.Background(),
|
|
"/foo", "POST", body,
|
|
map[string]string{"Content-Encoding": "gzip"},
|
|
nil, nil, "", "", nil,
|
|
)
|
|
require.NoError(t, err)
|
|
defer req.Body.Close()
|
|
|
|
out := bytes.Buffer{}
|
|
gzr, err := gzip.NewReader(req.Body)
|
|
require.NoError(t, err)
|
|
defer gzr.Close()
|
|
_, err = io.Copy(&out, gzr)
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(body), out.String())
|
|
}
|