diff --git a/.gometalinter.json b/.gometalinter.json new file mode 100644 index 0000000..03c625c --- /dev/null +++ b/.gometalinter.json @@ -0,0 +1,26 @@ +{ + "Disable": [ + "dupl", + "golint", + "gocyclo" + ], + "Enable": [ + "structcheck", + "maligned", + "deadcode", + "ineffassign", + "gotype", + "goimports", + "errcheck", + "varcheck", + "interfacer", + "goconst", + "unparam", + "megacheck", + "misspell", + "gas", + "safesql" + ], + "LineLength": 120, + "Vendor": true +} diff --git a/transport.go b/transport.go new file mode 100644 index 0000000..0748ec3 --- /dev/null +++ b/transport.go @@ -0,0 +1,19 @@ +package oaichecker + +import "net/http" + +type Transport struct { + Transport http.RoundTripper +} + +func NewTransport() *Transport { + return &Transport{ + Transport: http.DefaultTransport, + } +} + +func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) { + res, err := t.Transport.RoundTrip(req) + + return res, err +} diff --git a/transport_test.go b/transport_test.go new file mode 100644 index 0000000..574e298 --- /dev/null +++ b/transport_test.go @@ -0,0 +1,42 @@ +package oaichecker + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func resBody(t *testing.T, res *http.Response) string { + body, err := ioutil.ReadAll(res.Body) + require.NoError(t, err) + + return string(body) +} + +func newServer(handlerFunc func(http.ResponseWriter, *http.Request)) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(handlerFunc)) +} + +func Test_Transport_implements_RoundTripper(t *testing.T) { + assert.Implements(t, (*http.RoundTripper)(nil), NewTransport()) +} + +func Test_Transport_emit_the_request(t *testing.T) { + ts := newServer(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("some-response")) + }) + defer ts.Close() + + client := http.Client{ + Transport: NewTransport(), + } + + res, err := client.Get(ts.URL) + + assert.NoError(t, err) + assert.Equal(t, "some-response", resBody(t, res)) +}