49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
|
|
package ctl
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Test_headerTransport_RoundTrip(t1 *testing.T) {
|
||
|
|
type fields struct {
|
||
|
|
apiKey string
|
||
|
|
}
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
fields fields
|
||
|
|
handler func(t *testing.T) http.Handler
|
||
|
|
want *http.Response
|
||
|
|
wantErr bool
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "apiKey is set as header",
|
||
|
|
fields: fields{
|
||
|
|
apiKey: "abc123",
|
||
|
|
},
|
||
|
|
handler: func(t *testing.T) http.Handler {
|
||
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||
|
|
assert.Equal(t, "abc123", req.Header.Get("x-api-key"))
|
||
|
|
_, err := w.Write([]byte("OK"))
|
||
|
|
assert.NoError(t, err)
|
||
|
|
})
|
||
|
|
},
|
||
|
|
want: nil,
|
||
|
|
wantErr: false,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
for _, tt := range tests {
|
||
|
|
t1.Run(tt.name, func(t1 *testing.T) {
|
||
|
|
srv := httptest.NewServer(tt.handler(t1))
|
||
|
|
defer srv.Close()
|
||
|
|
t := NewTransport(http.DefaultTransport, tt.fields.apiKey)
|
||
|
|
c := http.Client{Transport: t}
|
||
|
|
_, err := c.Get(srv.URL)
|
||
|
|
assert.NoError(t1, err)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|