Skip to content

Commit

Permalink
#339 Addressing HTTP2 issue on go1.13 above
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed May 11, 2020
1 parent 14d9c98 commit 10a13d3
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: go
sudo: false

go: # use travis ci resource effectively, keep always latest 2 versions and tip :)
- 1.12.x
- 1.14.x
- 1.13.x
- tip

Expand Down
22 changes: 0 additions & 22 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import (
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -961,23 +959,3 @@ func createClient(hc *http.Client) *Client {

return c
}

func createTransport(localAddr net.Addr) *http.Transport {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
if localAddr != nil {
dialer.LocalAddr = localAddr
}
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
}
}
6 changes: 4 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ func TestClientRedirectPolicy(t *testing.T) {
c := dc().SetRedirectPolicy(FlexibleRedirectPolicy(20))
_, err := c.R().Get(ts.URL + "/redirect-1")

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

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

func TestClientTimeout(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ func TestClientRetryWithSetContext(t *testing.T) {
SetContext(context.Background()).
Get(ts.URL + "/")

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

func TestRequestContext(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func responseLogger(c *Client, res *Response) error {
debugLog := res.Request.values[debugRequestLogKey].(string)
debugLog += "~~~ RESPONSE ~~~\n" +
fmt.Sprintf("STATUS : %s\n", res.Status()) +
fmt.Sprintf("PROTO : %s\n", res.RawResponse.Proto) +
fmt.Sprintf("RECEIVED AT : %v\n", res.ReceivedAt().Format(time.RFC3339Nano)) +
fmt.Sprintf("TIME DURATION: %v\n", res.Time()) +
"HEADERS :\n" +
Expand Down
12 changes: 9 additions & 3 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestGet(t *testing.T) {

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

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "HTTP/1.1", resp.Proto())
assertEqual(t, "200 OK", resp.Status())
assertEqual(t, "TestGet: text response", resp.String())

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

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "HTTP/1.1", resp.Proto())
assertEqual(t, "200 OK", resp.Status())
assertEqual(t, "TestGet: text response", resp.String())

Expand Down Expand Up @@ -952,7 +955,8 @@ func TestHTTPAutoRedirectUpTo10(t *testing.T) {

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

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

func TestHostCheckRedirectPolicy(t *testing.T) {
Expand Down Expand Up @@ -1119,11 +1123,13 @@ func TestGetClient(t *testing.T) {
func TestIncorrectURL(t *testing.T) {
c := dc()
_, err := c.R().Get("//not.a.user@%66%6f%6f.com/just/a/path/also")
assertEqual(t, true, strings.Contains(err.Error(), "parse //not.a.user@%66%6f%6f.com/just/a/path/also"))
assertEqual(t, true, (strings.Contains(err.Error(), "parse //not.a.user@%66%6f%6f.com/just/a/path/also") ||
strings.Contains(err.Error(), "parse \"//not.a.user@%66%6f%6f.com/just/a/path/also\"")))

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

func TestDetectContentTypeForPointer(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func (r *Response) StatusCode() int {
return r.RawResponse.StatusCode
}

// Proto method returns the HTTP response protocol used for the request.
func (r *Response) Proto() string {
if r.RawResponse == nil {
return ""
}
return r.RawResponse.Proto
}

// Result method returns the response value as an object if it has one
func (r *Response) Result() interface{} {
return r.Request.Result
Expand Down
5 changes: 3 additions & 2 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ func TestClientRetryGet(t *testing.T) {
assertNotNil(t, resp.Body())
assertEqual(t, 0, len(resp.Header()))

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

func TestClientRetryWait(t *testing.T) {
Expand Down Expand Up @@ -616,7 +617,7 @@ func TestClientRetryCount(t *testing.T) {
// 2 attempts were made
assertEqual(t, attempt, 2)

assertEqual(t, true, strings.HasPrefix(err.Error(), "Get "+ts.URL+"/set-retrycount-test"))
assertEqual(t, true, strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\""))
}

func filler(*Response, error) bool {
Expand Down
35 changes: 35 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build go1.13

// Copyright (c) 2015-2020 Jeevanandam M ([email protected]), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package resty

import (
"net"
"net/http"
"runtime"
"time"
)

func createTransport(localAddr net.Addr) *http.Transport {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
if localAddr != nil {
dialer.LocalAddr = localAddr
}
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
}
}
34 changes: 34 additions & 0 deletions transport112.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// +build !go1.13

// Copyright (c) 2015-2020 Jeevanandam M ([email protected]), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package resty

import (
"net"
"net/http"
"runtime"
"time"
)

func createTransport(localAddr net.Addr) *http.Transport {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}
if localAddr != nil {
dialer.LocalAddr = localAddr
}
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
}
}

0 comments on commit 10a13d3

Please sign in to comment.