feat(api): improve codegen outputs (#41)
* Use `[]byte` for generated request bodies when the source schema has `format: byte` * Gzip-compress request bodies when `Content-Encoding: gzip` is set * Require that all models returned as error conditions in our API implement the `error` interface * Move the implementation for `api.HealthCheck` out of `ping.go` and into `api/error.go` Co-authored-by: William Baker <55118525+wbaker85@users.noreply.github.com>
This commit is contained in:
52
internal/api/client_internal_test.go
Normal file
52
internal/api/client_internal_test.go
Normal file
@ -0,0 +1,52 @@
|
||||
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())
|
||||
}
|
Reference in New Issue
Block a user