Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clients: Fix request with headers set #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func (c *fasthttpClient) do() (
// prepare the request
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
req.Header.SetHost(c.host)
if c.headers != nil {
c.headers.CopyTo(&req.Header)
}
req.Header.SetHost(c.host)
req.SetRequestURI(c.requestURI)
req.Header.SetMethod(c.method)
req.URI().SetScheme(c.scheme)
Expand Down
54 changes: 54 additions & 0 deletions clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,57 @@ func TestHTTP1Clients(t *testing.T) {
}
}
}

func TestHTTP1ClientsWithHeaders(t *testing.T) {
responseSize := 1024
response := bytes.Repeat([]byte{'a'}, responseSize)
s := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor != 1 {
t.Errorf("invalid HTTP proto version: %v", r.Proto)
}

w.WriteHeader(http.StatusOK)
_, err := w.Write(response)
if err != nil {
t.Error(err)
}
},
))
defer s.Close()

bytesRead, bytesWritten := int64(0), int64(0)
cc := &clientOpts{
HTTP2: false,

headers: &headersList{{"One", "Value one"}},
url: s.URL,
method: "GET",

body: new(string),

bytesRead: &bytesRead,
bytesWritten: &bytesWritten,
}
clients := []client{
newHTTPClient(cc),
newFastHTTPClient(cc),
}
for _, c := range clients {
bytesRead, bytesWritten = 0, 0
code, _, err := c.do()
if err != nil {
t.Error(err)
return
}
if code != http.StatusOK {
t.Errorf("invalid response code: %v", code)
}
if bytesRead == 0 {
t.Errorf("invalid response size: %v", bytesRead)
}
if bytesWritten == 0 {
t.Errorf("empty request of size: %v", bytesWritten)
}
}
}