Skip to content

Commit

Permalink
Remove dependency on x/net/context/ctxhttp
Browse files Browse the repository at this point in the history
Signed-off-by: Mihail Stoykov <[email protected]>
  • Loading branch information
mstoykov committed Sep 11, 2018
1 parent 41cb9a6 commit 96fdba4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
11 changes: 3 additions & 8 deletions client/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package client

import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
Expand All @@ -29,8 +30,6 @@ import (
"strings"
"sync"
"time"
"context"
"golang.org/x/net/context/ctxhttp"

"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/logger"
Expand Down Expand Up @@ -195,7 +194,6 @@ type Runtime struct {
clientOnce *sync.Once
client *http.Client
schemes []string
do func(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error)
}

// New creates a new default runtime for a swagger api runtime.Client
Expand Down Expand Up @@ -234,7 +232,6 @@ func New(host, basePath string, schemes []string) *Runtime {
if len(schemes) > 0 {
rt.schemes = schemes
}
rt.do = ctxhttp.Do
return &rt
}

Expand Down Expand Up @@ -390,10 +387,8 @@ func (r *Runtime) Submit(operation *runtime.ClientOperation) (interface{}, error
if client == nil {
client = r.client
}
if r.do == nil {
r.do = ctxhttp.Do
}
res, err := r.do(ctx, client, req) // make requests, by default follows 10 redirects before failing
req = req.WithContext(ctx)
res, err := client.Do(req) // make requests, by default follows 10 redirects before failing
if err != nil {
return nil, err
}
Expand Down
30 changes: 15 additions & 15 deletions client/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"testing"
"time"

"golang.org/x/net/context"

"crypto/x509"
"encoding/pem"

Expand Down Expand Up @@ -607,7 +605,6 @@ func TestRuntime_ContentTypeCanary(t *testing.T) {

hu, _ := url.Parse(server.URL)
rt := New(hu.Host, "/", []string{"http"})
rt.do = nil
res, err := rt.Submit(&runtime.ClientOperation{
ID: "getTasks",
Method: "GET",
Expand Down Expand Up @@ -730,6 +727,18 @@ func TestRuntime_OverrideClient(t *testing.T) {
assert.Equal(t, 0, i)
}

type overrideRoundTripper struct {
overriden bool
}

func (o *overrideRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
o.overriden = true
res := new(http.Response)
res.StatusCode = 200
res.Body = ioutil.NopCloser(bytes.NewBufferString("OK"))
return res, nil
}

func TestRuntime_OverrideClientOperation(t *testing.T) {
client := &http.Client{}
rt := NewWithClient("", "/", []string{"https"}, client)
Expand All @@ -738,17 +747,9 @@ func TestRuntime_OverrideClientOperation(t *testing.T) {
assert.Equal(t, client, rt.client)
assert.Equal(t, 0, i)

var seen *http.Client
rt.do = func(_ context.Context, cl *http.Client, _ *http.Request) (*http.Response, error) {
seen = cl
res := new(http.Response)
res.StatusCode = 200
res.Body = ioutil.NopCloser(bytes.NewBufferString("OK"))
return res, nil
}

client2 := new(http.Client)
client2.Timeout = 3 * time.Second
var transport = &overrideRoundTripper{}
client2.Transport = transport
if assert.NotEqual(t, client, client2) {
_, err := rt.Submit(&runtime.ClientOperation{
Client: client2,
Expand All @@ -760,8 +761,7 @@ func TestRuntime_OverrideClientOperation(t *testing.T) {
}),
})
if assert.NoError(t, err) {

assert.Equal(t, client2, seen)
assert.True(t, transport.overriden)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion client_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package runtime

import (
"net/http"
"context"
"net/http"
)

// ClientOperation represents the context for a swagger operation to be submitted to the transport
Expand Down

0 comments on commit 96fdba4

Please sign in to comment.