Skip to content

Commit

Permalink
Merge pull request #460 from go-resty/for-gh318-gh407
Browse files Browse the repository at this point in the history
* Addressing SetScheme option side effects on schemeless Host URL #407
* Travis ci config update and simplify composite literal
  • Loading branch information
jeevatkm committed Sep 16, 2021
2 parents 2cae8cd + 18ff59d commit 1792d62
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
9 changes: 3 additions & 6 deletions .travis.yml
Expand Up @@ -2,10 +2,11 @@ language: go

os: linux

go: # use travis ci resource effectively, keep always latest 2 versions and tip :)
if: (branch = master) OR (type = pull_request)

go: # use travis ci resource effectively, keep always latest 2 versions
- 1.17.x
- 1.16.x
- tip

install:
- go get -v -t ./...
Expand All @@ -15,7 +16,3 @@ script:

after_success:
- bash <(curl -s https://codecov.io/bash)

jobs:
allow_failures:
- go: tip
2 changes: 1 addition & 1 deletion client.go
Expand Up @@ -761,7 +761,7 @@ func (c *Client) SetTransport(transport http.RoundTripper) *Client {
// client.SetScheme("http")
func (c *Client) SetScheme(scheme string) *Client {
if !IsStringEmpty(scheme) {
c.scheme = scheme
c.scheme = strings.TrimSpace(scheme)
}
return c
}
Expand Down
43 changes: 43 additions & 0 deletions client_test.go
Expand Up @@ -744,3 +744,46 @@ func TestResponseError(t *testing.T) {
assertNotNil(t, re.Unwrap())
assertEqual(t, err.Error(), re.Error())
}

func TestHostURLForGH318AndGH407(t *testing.T) {
ts := createPostServer(t)
defer ts.Close()

targetURL, _ := url.Parse(ts.URL)
t.Log("ts.URL:", ts.URL)
t.Log("targetURL.Host:", targetURL.Host)
// Sample output
// ts.URL: http://127.0.0.1:55967
// targetURL.Host: 127.0.0.1:55967

// Unable use the local http test server for this
// use case testing
//
// using `targetURL.Host` value or test case yield to ERROR
// "parse "127.0.0.1:55967": first path segment in URL cannot contain colon"

// test the functionality with httpbin.org locally
// will figure out later

c := dc()
// c.SetScheme("http")
// c.SetHostURL(targetURL.Host + "/")

// t.Log("with leading `/`")
// resp, err := c.R().Post("/login")
// assertNil(t, err)
// assertNotNil(t, resp)

// t.Log("\nwithout leading `/`")
// resp, err = c.R().Post("login")
// assertNil(t, err)
// assertNotNil(t, resp)

t.Log("with leading `/` on request & with trailing `/` on host url")
c.SetHostURL(ts.URL + "/")
resp, err := c.R().
SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}).
Post("/login")
assertNil(t, err)
assertNotNil(t, resp)
}
11 changes: 5 additions & 6 deletions middleware.go
Expand Up @@ -60,6 +60,11 @@ func parseRequestURL(c *Client, r *Request) error {
}
}

// GH #407 && #318
if reqURL.Scheme == "" && len(c.scheme) > 0 {
reqURL.Scheme = c.scheme
}

// Adding Query Param
query := make(url.Values)
for k, v := range c.QueryParam {
Expand Down Expand Up @@ -191,12 +196,6 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
r.RawRequest.AddCookie(cookie)
}

// it's for non-http scheme option
if r.RawRequest.URL != nil && r.RawRequest.URL.Scheme == "" {
r.RawRequest.URL.Scheme = c.scheme
r.RawRequest.URL.Host = r.URL
}

// Enable trace
if c.trace || r.trace {
r.clientTrace = &clientTrace{}
Expand Down
4 changes: 2 additions & 2 deletions request_test.go
Expand Up @@ -1377,8 +1377,8 @@ func TestSetHeaderMultipleValue(t *testing.T) {

r := dclr().
SetHeaderMultiValues(map[string][]string{
"Content": []string{"text/*", "text/html", "*"},
"Authorization": []string{"Bearer xyz"},
"Content": {"text/*", "text/html", "*"},
"Authorization": {"Bearer xyz"},
})
assertEqual(t, "text/*, text/html, *", r.Header.Get("content"))
assertEqual(t, "Bearer xyz", r.Header.Get("authorization"))
Expand Down

0 comments on commit 1792d62

Please sign in to comment.