Created test to simulate TCP io timeout

This commit is contained in:
Bhaskar Brahma 2021-01-28 12:24:41 -08:00
Родитель e99c79aa4d
Коммит 1f020744a4
2 изменённых файлов: 38 добавлений и 0 удалений

Просмотреть файл

@ -1,7 +1,13 @@
package download
import (
"fmt"
"net"
"net/http"
"net/url"
"regexp"
"testing"
"time"
"github.com/stretchr/testify/require"
)
@ -26,3 +32,29 @@ func Test_urlDownload_GetRequest_goodURL(t *testing.T) {
require.Nil(t, err, u)
require.NotNil(t, r, u)
}
func Test_simulateDialTcpTimeout(t *testing.T) {
oldHttpClient := httpClient
httpClient = &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 1 * time.Nanosecond,
KeepAlive: 30 * time.Second,
}).Dial,
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 20 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}}
defer func() {
httpClient = oldHttpClient
}()
u := "https://bhbrahmastorage.blob.core.windows.net/vmappcontainer/goland"
url, _ := url.Parse(u)
d := NewURLDownload(u)
_, _, err := Download(d)
// fmt.Println(err.Error())
require.Error(t, err)
require.Regexp(t, regexp.MustCompile(fmt.Sprintf("dial tcp %s: i/o timeout", url.Host)), err.Error())
}

Просмотреть файл

@ -22,5 +22,11 @@ func IsValidUrl(urlstring string) bool {
if parseError == nil && u.Scheme != "" && u.Host != "" {
return true
}
// try again with trimmed string
urlstringTrimmed := strings.Trim(urlstring, "\"")
u, parseError = url.Parse(urlstringTrimmed)
if parseError == nil && u.Scheme != "" && u.Host != "" {
return true
}
return false
}