Skip to content

Commit

Permalink
Add set scheme client option
Browse files Browse the repository at this point in the history
Signed-off-by: André Martins <[email protected]>
  • Loading branch information
aanm committed Jun 29, 2016
1 parent 84e74db commit 05d5b05
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
beforeRequest []func(*Client, *Request) error
afterResponse []func(*Client, *Response) error
Expand Down Expand Up @@ -582,6 +583,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 @@ -603,6 +615,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)

response := &Response{
Expand Down
6 changes: 6 additions & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,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 @@ -138,6 +138,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 @@ -1213,6 +1213,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 @@ -1301,6 +1309,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 05d5b05

Please sign in to comment.