Skip to content

Commit e7fae0d

Browse files
authored
Merge pull request #340 from go-resty/dev-iteration
Dev iteration
2 parents 98f59aa + 6ad880c commit e7fae0d

11 files changed

+127
-52
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: go
33
sudo: false
44

55
go: # use travis ci resource effectively, keep always latest 2 versions and tip :)
6-
- 1.12.x
6+
- 1.14.x
77
- 1.13.x
88
- tip
99

client.go

-22
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ import (
1515
"io"
1616
"io/ioutil"
1717
"math"
18-
"net"
1918
"net/http"
2019
"net/url"
2120
"reflect"
2221
"regexp"
23-
"runtime"
2422
"strings"
2523
"sync"
2624
"time"
@@ -961,23 +959,3 @@ func createClient(hc *http.Client) *Client {
961959

962960
return c
963961
}
964-
965-
func createTransport(localAddr net.Addr) *http.Transport {
966-
dialer := &net.Dialer{
967-
Timeout: 30 * time.Second,
968-
KeepAlive: 30 * time.Second,
969-
DualStack: true,
970-
}
971-
if localAddr != nil {
972-
dialer.LocalAddr = localAddr
973-
}
974-
return &http.Transport{
975-
Proxy: http.ProxyFromEnvironment,
976-
DialContext: dialer.DialContext,
977-
MaxIdleConns: 100,
978-
IdleConnTimeout: 90 * time.Second,
979-
TLSHandshakeTimeout: 10 * time.Second,
980-
ExpectContinueTimeout: 1 * time.Second,
981-
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
982-
}
983-
}

client_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ func TestClientRedirectPolicy(t *testing.T) {
107107
c := dc().SetRedirectPolicy(FlexibleRedirectPolicy(20))
108108
_, err := c.R().Get(ts.URL + "/redirect-1")
109109

110-
assertEqual(t, "Get /redirect-21: stopped after 20 redirects", err.Error())
110+
assertEqual(t, true, ("Get /redirect-21: stopped after 20 redirects" == err.Error() ||
111+
"Get \"/redirect-21\": stopped after 20 redirects" == err.Error()))
111112

112113
c.SetRedirectPolicy(NoRedirectPolicy())
113114
_, err = c.R().Get(ts.URL + "/redirect-1")
114-
assertEqual(t, "Get /redirect-2: auto redirect is disabled", err.Error())
115+
assertEqual(t, true, ("Get /redirect-2: auto redirect is disabled" == err.Error() ||
116+
"Get \"/redirect-2\": auto redirect is disabled" == err.Error()))
115117
}
116118

117119
func TestClientTimeout(t *testing.T) {

context_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ func TestClientRetryWithSetContext(t *testing.T) {
192192
SetContext(context.Background()).
193193
Get(ts.URL + "/")
194194

195-
assertEqual(t, true, strings.HasPrefix(err.Error(), "Get "+ts.URL+"/"))
195+
assertEqual(t, true, (strings.HasPrefix(err.Error(), "Get "+ts.URL+"/") ||
196+
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/\"")))
196197
}
197198

198199
func TestRequestContext(t *testing.T) {

middleware.go

+1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ func responseLogger(c *Client, res *Response) error {
314314
debugLog := res.Request.values[debugRequestLogKey].(string)
315315
debugLog += "~~~ RESPONSE ~~~\n" +
316316
fmt.Sprintf("STATUS : %s\n", res.Status()) +
317+
fmt.Sprintf("PROTO : %s\n", res.RawResponse.Proto) +
317318
fmt.Sprintf("RECEIVED AT : %v\n", res.ReceivedAt().Format(time.RFC3339Nano)) +
318319
fmt.Sprintf("TIME DURATION: %v\n", res.Time()) +
319320
"HEADERS :\n" +

request_test.go

+29-19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestGet(t *testing.T) {
3838

3939
assertError(t, err)
4040
assertEqual(t, http.StatusOK, resp.StatusCode())
41+
assertEqual(t, "HTTP/1.1", resp.Proto())
4142
assertEqual(t, "200 OK", resp.Status())
4243
assertNotNil(t, resp.Body())
4344
assertEqual(t, "TestGet: text response", resp.String())
@@ -56,6 +57,7 @@ func TestGetCustomUserAgent(t *testing.T) {
5657

5758
assertError(t, err)
5859
assertEqual(t, http.StatusOK, resp.StatusCode())
60+
assertEqual(t, "HTTP/1.1", resp.Proto())
5961
assertEqual(t, "200 OK", resp.Status())
6062
assertEqual(t, "TestGet: text response", resp.String())
6163

@@ -80,6 +82,7 @@ func TestGetClientParamRequestParam(t *testing.T) {
8082

8183
assertError(t, err)
8284
assertEqual(t, http.StatusOK, resp.StatusCode())
85+
assertEqual(t, "HTTP/1.1", resp.Proto())
8386
assertEqual(t, "200 OK", resp.Status())
8487
assertEqual(t, "TestGet: text response", resp.String())
8588

@@ -825,20 +828,8 @@ func TestGetWithCookies(t *testing.T) {
825828
ts := createGetServer(t)
826829
defer ts.Close()
827830

828-
cookies := []*http.Cookie{
829-
{
830-
Name: "go-resty-1",
831-
Value: "This is cookie 1 value",
832-
},
833-
{
834-
Name: "go-resty-2",
835-
Value: "This is cookie 2 value",
836-
},
837-
}
838-
839831
c := dc()
840-
c.SetHostURL(ts.URL).
841-
SetCookies(cookies)
832+
c.SetHostURL(ts.URL).SetDebug(true)
842833

843834
tu, _ := url.Parse(ts.URL)
844835
c.GetClient().Jar.SetCookies(tu, []*http.Cookie{
@@ -852,10 +843,26 @@ func TestGetWithCookies(t *testing.T) {
852843
},
853844
})
854845

855-
resp, err := c.R().
856-
SetCookie(&http.Cookie{
846+
resp, err := c.R().SetHeader("Cookie", "").Get("mypage2")
847+
assertError(t, err)
848+
assertEqual(t, http.StatusOK, resp.StatusCode())
849+
850+
// Client cookies
851+
c.SetCookies([]*http.Cookie{
852+
{
857853
Name: "go-resty-1",
858-
Value: "This is cookie 1 value additional append",
854+
Value: "This is cookie 1 value",
855+
},
856+
{
857+
Name: "go-resty-2",
858+
Value: "This is cookie 2 value",
859+
},
860+
})
861+
862+
resp, err = c.R().
863+
SetCookie(&http.Cookie{
864+
Name: "req-go-resty-1",
865+
Value: "This is request cookie 1 value additional append",
859866
}).
860867
Get("mypage2")
861868

@@ -952,7 +959,8 @@ func TestHTTPAutoRedirectUpTo10(t *testing.T) {
952959

953960
_, err := dc().R().Get(ts.URL + "/redirect-1")
954961

955-
assertEqual(t, "Get /redirect-11: stopped after 10 redirects", err.Error())
962+
assertEqual(t, true, ("Get /redirect-11: stopped after 10 redirects" == err.Error() ||
963+
"Get \"/redirect-11\": stopped after 10 redirects" == err.Error()))
956964
}
957965

958966
func TestHostCheckRedirectPolicy(t *testing.T) {
@@ -1119,11 +1127,13 @@ func TestGetClient(t *testing.T) {
11191127
func TestIncorrectURL(t *testing.T) {
11201128
c := dc()
11211129
_, err := c.R().Get("//not.a.user@%66%6f%6f.com/just/a/path/also")
1122-
assertEqual(t, true, strings.Contains(err.Error(), "parse //not.a.user@%66%6f%6f.com/just/a/path/also"))
1130+
assertEqual(t, true, (strings.Contains(err.Error(), "parse //not.a.user@%66%6f%6f.com/just/a/path/also") ||
1131+
strings.Contains(err.Error(), "parse \"//not.a.user@%66%6f%6f.com/just/a/path/also\"")))
11231132

11241133
c.SetHostURL("//not.a.user@%66%6f%6f.com")
11251134
_, err1 := c.R().Get("/just/a/path/also")
1126-
assertEqual(t, true, strings.Contains(err1.Error(), "parse //not.a.user@%66%6f%6f.com/just/a/path/also"))
1135+
assertEqual(t, true, (strings.Contains(err1.Error(), "parse //not.a.user@%66%6f%6f.com/just/a/path/also") ||
1136+
strings.Contains(err1.Error(), "parse \"//not.a.user@%66%6f%6f.com/just/a/path/also\"")))
11271137
}
11281138

11291139
func TestDetectContentTypeForPointer(t *testing.T) {

response.go

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func (r *Response) StatusCode() int {
5555
return r.RawResponse.StatusCode
5656
}
5757

58+
// Proto method returns the HTTP response protocol used for the request.
59+
func (r *Response) Proto() string {
60+
if r.RawResponse == nil {
61+
return ""
62+
}
63+
return r.RawResponse.Proto
64+
}
65+
5866
// Result method returns the response value as an object if it has one
5967
func (r *Response) Result() interface{} {
6068
return r.Request.Result

retry_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,14 @@ func TestClientRetryGet(t *testing.T) {
146146

147147
resp, err := c.R().Get(ts.URL + "/set-retrycount-test")
148148
assertEqual(t, "", resp.Status())
149+
assertEqual(t, "", resp.Proto())
149150
assertEqual(t, 0, resp.StatusCode())
150151
assertEqual(t, 0, len(resp.Cookies()))
151152
assertNotNil(t, resp.Body())
152153
assertEqual(t, 0, len(resp.Header()))
153154

154-
assertEqual(t, true, strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test"))
155+
assertEqual(t, true, (strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test") ||
156+
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\"")))
155157
}
156158

157159
func TestClientRetryWait(t *testing.T) {
@@ -608,6 +610,7 @@ func TestClientRetryCount(t *testing.T) {
608610

609611
resp, err := c.R().Get(ts.URL + "/set-retrycount-test")
610612
assertEqual(t, "", resp.Status())
613+
assertEqual(t, "", resp.Proto())
611614
assertEqual(t, 0, resp.StatusCode())
612615
assertEqual(t, 0, len(resp.Cookies()))
613616
assertNotNil(t, resp.Body())
@@ -616,7 +619,8 @@ func TestClientRetryCount(t *testing.T) {
616619
// 2 attempts were made
617620
assertEqual(t, attempt, 2)
618621

619-
assertEqual(t, true, strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test"))
622+
assertEqual(t, true, (strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test") ||
623+
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\"")))
620624
}
621625

622626
func filler(*Response, error) bool {

transport.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// +build go1.13
2+
3+
// Copyright (c) 2015-2020 Jeevanandam M ([email protected]), All rights reserved.
4+
// resty source code and usage is governed by a MIT style
5+
// license that can be found in the LICENSE file.
6+
7+
package resty
8+
9+
import (
10+
"net"
11+
"net/http"
12+
"runtime"
13+
"time"
14+
)
15+
16+
func createTransport(localAddr net.Addr) *http.Transport {
17+
dialer := &net.Dialer{
18+
Timeout: 30 * time.Second,
19+
KeepAlive: 30 * time.Second,
20+
DualStack: true,
21+
}
22+
if localAddr != nil {
23+
dialer.LocalAddr = localAddr
24+
}
25+
return &http.Transport{
26+
Proxy: http.ProxyFromEnvironment,
27+
DialContext: dialer.DialContext,
28+
ForceAttemptHTTP2: true,
29+
MaxIdleConns: 100,
30+
IdleConnTimeout: 90 * time.Second,
31+
TLSHandshakeTimeout: 10 * time.Second,
32+
ExpectContinueTimeout: 1 * time.Second,
33+
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
34+
}
35+
}

transport112.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// +build !go1.13
2+
3+
// Copyright (c) 2015-2020 Jeevanandam M ([email protected]), All rights reserved.
4+
// resty source code and usage is governed by a MIT style
5+
// license that can be found in the LICENSE file.
6+
7+
package resty
8+
9+
import (
10+
"net"
11+
"net/http"
12+
"runtime"
13+
"time"
14+
)
15+
16+
func createTransport(localAddr net.Addr) *http.Transport {
17+
dialer := &net.Dialer{
18+
Timeout: 30 * time.Second,
19+
KeepAlive: 30 * time.Second,
20+
DualStack: true,
21+
}
22+
if localAddr != nil {
23+
dialer.LocalAddr = localAddr
24+
}
25+
return &http.Transport{
26+
Proxy: http.ProxyFromEnvironment,
27+
DialContext: dialer.DialContext,
28+
MaxIdleConns: 100,
29+
IdleConnTimeout: 90 * time.Second,
30+
TLSHandshakeTimeout: 10 * time.Second,
31+
ExpectContinueTimeout: 1 * time.Second,
32+
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
33+
}
34+
}

util.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,13 @@ func composeHeaders(c *Client, r *Request, hdrs http.Header) string {
297297
var v string
298298
if k == "Cookie" {
299299
cv := strings.TrimSpace(strings.Join(hdrs[k], ", "))
300-
for _, c := range c.GetClient().Jar.Cookies(r.RawRequest.URL) {
301-
if cv != "" {
302-
cv = cv + "; " + c.String()
303-
} else {
304-
cv = c.String()
300+
if c.GetClient().Jar != nil {
301+
for _, c := range c.GetClient().Jar.Cookies(r.RawRequest.URL) {
302+
if cv != "" {
303+
cv = cv + "; " + c.String()
304+
} else {
305+
cv = c.String()
306+
}
305307
}
306308
}
307309
v = strings.TrimSpace(fmt.Sprintf("%25s: %s", k, cv))

0 commit comments

Comments
 (0)