Skip to content

Commit 6491a5d

Browse files
committed
clients.go: change implementation to send Host header correctly
Turns out there is a way to send Host header correctly with fasthttp. This commit also adds a bunch of tests to improve coverage. Updates #41.
1 parent e71c65b commit 6491a5d

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-7
lines changed

args_parser_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,41 @@ func TestArgsParsing(t *testing.T) {
5050
in [][]string
5151
out config
5252
}{
53+
{
54+
[][]string{
55+
{programName, ":8080"},
56+
{programName, "localhost:8080"},
57+
},
58+
config{
59+
numConns: defaultNumberOfConns,
60+
timeout: defaultTimeout,
61+
headers: new(headersList),
62+
method: "GET",
63+
url: "http://localhost:8080",
64+
printIntro: true,
65+
printProgress: true,
66+
printResult: true,
67+
format: knownFormat("plain-text"),
68+
},
69+
},
70+
{
71+
[][]string{
72+
{programName, "https://"},
73+
{programName, "https://:443"},
74+
{programName, "https://localhost"},
75+
},
76+
config{
77+
numConns: defaultNumberOfConns,
78+
timeout: defaultTimeout,
79+
headers: new(headersList),
80+
method: "GET",
81+
url: "https://localhost:443",
82+
printIntro: true,
83+
printProgress: true,
84+
printResult: true,
85+
format: knownFormat("plain-text"),
86+
},
87+
},
5388
{
5489
[][]string{{programName, "https://somehost.somedomain"}},
5590
config{
@@ -790,3 +825,18 @@ func TestArgsParsingWithInvalidPrintSpec(t *testing.T) {
790825
}
791826
}
792827
}
828+
829+
func TestTryParseUrl(t *testing.T) {
830+
invalid := []string{
831+
"ftp://bla:89",
832+
"http://bla:bla:bla",
833+
"htp:/bla:bla:bla",
834+
}
835+
836+
for _, url := range invalid {
837+
_, err := tryParseURL(url)
838+
if err == nil {
839+
t.Errorf("%q is not a valid URL", url)
840+
}
841+
}
842+
}

bombardier_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,41 @@ func testBombardierStreamsBodyFromFile(clientType clientTyp, t *testing.T) {
703703
b.disableOutput()
704704
b.bombard()
705705
}
706+
707+
func TestBombardierShouldSendCustomHostHeader(t *testing.T) {
708+
testAllClients(t, testBombardierShouldSendCustomHostHeader)
709+
}
710+
711+
func testBombardierShouldSendCustomHostHeader(
712+
clientType clientTyp, t *testing.T,
713+
) {
714+
host := "custom-host"
715+
s := httptest.NewServer(
716+
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
717+
if r.Host != host {
718+
t.Errorf("Host must be %q, but it's %q", host, r.Host)
719+
}
720+
}),
721+
)
722+
defer s.Close()
723+
numReqs := uint64(100)
724+
headers := headersList([]header{
725+
{"Host", host},
726+
})
727+
b, e := newBombardier(config{
728+
numConns: defaultNumberOfConns,
729+
numReqs: &numReqs,
730+
url: s.URL,
731+
headers: &headers,
732+
timeout: defaultTimeout,
733+
method: "GET",
734+
body: "",
735+
clientType: clientType,
736+
format: knownFormat("plain-text"),
737+
})
738+
if e != nil {
739+
t.Error(e)
740+
}
741+
b.disableOutput()
742+
b.bombard()
743+
}

clients.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,28 @@ type clientOpts struct {
3636
}
3737

3838
type fasthttpClient struct {
39-
client *fasthttp.Client
39+
client *fasthttp.HostClient
4040

41-
headers *fasthttp.RequestHeader
42-
url, method string
41+
headers *fasthttp.RequestHeader
42+
host, requestURI, method string
4343

4444
body *string
4545
bodProd bodyStreamProducer
4646
}
4747

4848
func newFastHTTPClient(opts *clientOpts) client {
4949
c := new(fasthttpClient)
50-
c.client = &fasthttp.Client{
51-
MaxConnsPerHost: int(opts.maxConns),
50+
u, err := url.Parse(opts.url)
51+
if err != nil {
52+
// opts.url guaranteed to be valid at this point
53+
panic(err)
54+
}
55+
c.host = u.Host
56+
c.requestURI = "/" + u.Path + "?" + u.RawQuery
57+
c.client = &fasthttp.HostClient{
58+
Addr: u.Host,
59+
IsTLS: u.Scheme == "https",
60+
MaxConns: int(opts.maxConns),
5261
ReadTimeout: opts.timeout,
5362
WriteTimeout: opts.timeout,
5463
DisableHeaderNamesNormalizing: true,
@@ -58,7 +67,7 @@ func newFastHTTPClient(opts *clientOpts) client {
5867
),
5968
}
6069
c.headers = headersToFastHTTPHeaders(opts.headers)
61-
c.url, c.method, c.body = opts.url, opts.method, opts.body
70+
c.method, c.body = opts.method, opts.body
6271
c.bodProd = opts.bodProd
6372
return client(c)
6473
}
@@ -72,8 +81,11 @@ func (c *fasthttpClient) do() (
7281
if c.headers != nil {
7382
c.headers.CopyTo(&req.Header)
7483
}
84+
if len(req.Header.Host()) == 0 {
85+
req.Header.SetHost(c.host)
86+
}
7587
req.Header.SetMethod(c.method)
76-
req.SetRequestURI(c.url)
88+
req.SetRequestURI(c.requestURI)
7789
if c.body != nil {
7890
req.SetBodyString(*c.body)
7991
} else {

0 commit comments

Comments
 (0)