Skip to content

Commit 48a5f15

Browse files
committed
Addressing SetScheme option side effects on schemaless Host URL #407
1 parent 79cc4d4 commit 48a5f15

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

client.go

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

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{}

0 commit comments

Comments
 (0)