Skip to content

Commit

Permalink
Create a basic transport
Browse files Browse the repository at this point in the history
  • Loading branch information
Peltoche committed Sep 29, 2018
1 parent ee17b03 commit 7352060
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gometalinter.json
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 7352060

Please sign in to comment.