Skip to content

Commit

Permalink
Merge pull request #30 from aanm/add-set-scheme
Browse files Browse the repository at this point in the history
Add set scheme client option, enables go-resty with unix sockets too.
  • Loading branch information
jeevatkm authored Jun 29, 2016
2 parents 39c3db9 + 05d5b05 commit 45ff948
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Client struct {
setContentLength bool
isHTTPMode bool
outputDirectory string
scheme string
proxyURL *url.URL
mutex *sync.Mutex
beforeRequest []func(*Client, *Request) error
Expand Down Expand Up @@ -583,6 +584,17 @@ func (c *Client) SetTransport(transport *http.Transport) *Client {
return c
}

// SetScheme method sets custom scheme in the resty client. Its way to override default.
// resty.SetScheme("http")
//
func (c *Client) SetScheme(scheme string) *Client {
if c.scheme == "" {
c.scheme = scheme
}

return c
}

// executes the given `Request` object and returns response
func (c *Client) execute(req *Request) (*Response, error) {
// Apply Request middleware
Expand All @@ -606,6 +618,7 @@ func (c *Client) execute(req *Request) (*Response, error) {

req.Time = time.Now()
c.httpClient.Transport = c.transport

resp, err := c.httpClient.Do(req.RawRequest)

c.mutex.Unlock()
Expand Down
6 changes: 6 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ func SetTransport(transport *http.Transport) *Client {
return DefaultClient.SetTransport(transport)
}

// SetScheme method sets custom scheme in the resty client.
// See `Client.SetScheme` for more information.
func SetScheme(scheme string) *Client {
return DefaultClient.SetScheme(scheme)
}

func init() {
DefaultClient = New()
}
5 changes: 5 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
r.RawRequest.AddCookie(cookie)
}

if r.RawRequest.URL != nil && r.RawRequest.URL.Scheme == "" {
r.RawRequest.URL.Scheme = c.scheme
r.RawRequest.URL.Host = r.URL
}

return
}

Expand Down
11 changes: 11 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,14 @@ func TestSetTransport(t *testing.T) {
assertEqual(t, true, DefaultClient.transport != nil)
}

func TestSetScheme(t *testing.T) {
DefaultClient = dc()

SetScheme("http")

assertEqual(t, true, DefaultClient.scheme == "http")
}

func TestClientOptions(t *testing.T) {
SetHTTPMode().SetContentLength(true)
assertEqual(t, Mode(), "http")
Expand Down Expand Up @@ -1339,6 +1347,9 @@ func TestClientOptions(t *testing.T) {
SetDebug(true)
assertEqual(t, DefaultClient.Debug, true)

SetScheme("http")
assertEqual(t, DefaultClient.scheme, "http")

SetLogger(ioutil.Discard)
}

Expand Down

0 comments on commit 45ff948

Please sign in to comment.