Skip to content

Commit 1792d62

Browse files
authored
Merge pull request #460 from go-resty/for-gh318-gh407
* Addressing SetScheme option side effects on schemeless Host URL #407 * Travis ci config update and simplify composite literal
2 parents 2cae8cd + 18ff59d commit 1792d62

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

.travis.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ language: go
22

33
os: linux
44

5-
go: # use travis ci resource effectively, keep always latest 2 versions and tip :)
5+
if: (branch = master) OR (type = pull_request)
6+
7+
go: # use travis ci resource effectively, keep always latest 2 versions
68
- 1.17.x
79
- 1.16.x
8-
- tip
910

1011
install:
1112
- go get -v -t ./...
@@ -15,7 +16,3 @@ script:
1516

1617
after_success:
1718
- bash <(curl -s https://codecov.io/bash)
18-
19-
jobs:
20-
allow_failures:
21-
- go: tip

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ func (c *Client) SetTransport(transport http.RoundTripper) *Client {
761761
// client.SetScheme("http")
762762
func (c *Client) SetScheme(scheme string) *Client {
763763
if !IsStringEmpty(scheme) {
764-
c.scheme = scheme
764+
c.scheme = strings.TrimSpace(scheme)
765765
}
766766
return c
767767
}

client_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,3 +744,46 @@ func TestResponseError(t *testing.T) {
744744
assertNotNil(t, re.Unwrap())
745745
assertEqual(t, err.Error(), re.Error())
746746
}
747+
748+
func TestHostURLForGH318AndGH407(t *testing.T) {
749+
ts := createPostServer(t)
750+
defer ts.Close()
751+
752+
targetURL, _ := url.Parse(ts.URL)
753+
t.Log("ts.URL:", ts.URL)
754+
t.Log("targetURL.Host:", targetURL.Host)
755+
// Sample output
756+
// ts.URL: http://127.0.0.1:55967
757+
// targetURL.Host: 127.0.0.1:55967
758+
759+
// Unable use the local http test server for this
760+
// use case testing
761+
//
762+
// using `targetURL.Host` value or test case yield to ERROR
763+
// "parse "127.0.0.1:55967": first path segment in URL cannot contain colon"
764+
765+
// test the functionality with httpbin.org locally
766+
// will figure out later
767+
768+
c := dc()
769+
// c.SetScheme("http")
770+
// c.SetHostURL(targetURL.Host + "/")
771+
772+
// t.Log("with leading `/`")
773+
// resp, err := c.R().Post("/login")
774+
// assertNil(t, err)
775+
// assertNotNil(t, resp)
776+
777+
// t.Log("\nwithout leading `/`")
778+
// resp, err = c.R().Post("login")
779+
// assertNil(t, err)
780+
// assertNotNil(t, resp)
781+
782+
t.Log("with leading `/` on request & with trailing `/` on host url")
783+
c.SetHostURL(ts.URL + "/")
784+
resp, err := c.R().
785+
SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}).
786+
Post("/login")
787+
assertNil(t, err)
788+
assertNotNil(t, resp)
789+
}

middleware.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func parseRequestURL(c *Client, r *Request) error {
6060
}
6161
}
6262

63+
// GH #407 && #318
64+
if reqURL.Scheme == "" && len(c.scheme) > 0 {
65+
reqURL.Scheme = c.scheme
66+
}
67+
6368
// Adding Query Param
6469
query := make(url.Values)
6570
for k, v := range c.QueryParam {
@@ -191,12 +196,6 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
191196
r.RawRequest.AddCookie(cookie)
192197
}
193198

194-
// it's for non-http scheme option
195-
if r.RawRequest.URL != nil && r.RawRequest.URL.Scheme == "" {
196-
r.RawRequest.URL.Scheme = c.scheme
197-
r.RawRequest.URL.Host = r.URL
198-
}
199-
200199
// Enable trace
201200
if c.trace || r.trace {
202201
r.clientTrace = &clientTrace{}

request_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,8 +1377,8 @@ func TestSetHeaderMultipleValue(t *testing.T) {
13771377

13781378
r := dclr().
13791379
SetHeaderMultiValues(map[string][]string{
1380-
"Content": []string{"text/*", "text/html", "*"},
1381-
"Authorization": []string{"Bearer xyz"},
1380+
"Content": {"text/*", "text/html", "*"},
1381+
"Authorization": {"Bearer xyz"},
13821382
})
13831383
assertEqual(t, "text/*, text/html, *", r.Header.Get("content"))
13841384
assertEqual(t, "Bearer xyz", r.Header.Get("authorization"))

0 commit comments

Comments
 (0)