add more e2e test (#2505)

This commit is contained in:
fatedier
2021-08-02 13:07:28 +08:00
committed by GitHub
parent 2a68c1152f
commit 09f39de74e
20 changed files with 1448 additions and 154 deletions

View File

@@ -3,6 +3,7 @@ package request
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
@@ -25,11 +26,12 @@ type Request struct {
body []byte
timeout time.Duration
// for http
method string
host string
path string
headers map[string]string
// for http or https
method string
host string
path string
headers map[string]string
tlsConfig *tls.Config
proxyURL string
}
@@ -64,6 +66,11 @@ func (r *Request) HTTP() *Request {
return r
}
func (r *Request) HTTPS() *Request {
r.protocol = "https"
return r
}
func (r *Request) Proxy(url string) *Request {
r.proxyURL = url
return r
@@ -102,6 +109,11 @@ func (r *Request) HTTPHeaders(headers map[string]string) *Request {
return r
}
func (r *Request) TLSConfig(tlsConfig *tls.Config) *Request {
r.tlsConfig = tlsConfig
return r
}
func (r *Request) Timeout(timeout time.Duration) *Request {
r.timeout = timeout
return r
@@ -119,10 +131,10 @@ func (r *Request) Do() (*Response, error) {
)
addr := net.JoinHostPort(r.addr, strconv.Itoa(r.port))
// for protocol http
if r.protocol == "http" {
return r.sendHTTPRequest(r.method, fmt.Sprintf("http://%s%s", addr, r.path),
r.host, r.headers, r.proxyURL, r.body)
// for protocol http and https
if r.protocol == "http" || r.protocol == "https" {
return r.sendHTTPRequest(r.method, fmt.Sprintf("%s://%s%s", r.protocol, addr, r.path),
r.host, r.headers, r.proxyURL, r.body, r.tlsConfig)
}
// for protocol tcp and udp
@@ -165,7 +177,10 @@ type Response struct {
Content []byte
}
func (r *Request) sendHTTPRequest(method, urlstr string, host string, headers map[string]string, proxy string, body []byte) (*Response, error) {
func (r *Request) sendHTTPRequest(method, urlstr string, host string, headers map[string]string,
proxy string, body []byte, tlsConfig *tls.Config,
) (*Response, error) {
var inBody io.Reader
if len(body) != 0 {
inBody = bytes.NewReader(body)
@@ -190,6 +205,7 @@ func (r *Request) sendHTTPRequest(method, urlstr string, host string, headers ma
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
}
if len(proxy) != 0 {
tr.Proxy = func(req *http.Request) (*url.URL, error) {