Skip to content

Commit 84585f9

Browse files
committed
Fix default User-Agent when using custom headers
When using non-nil hdr parameter with c.Request method, and not setting User-Agent header explicitly, it would be left unset (when it should've been set to c.UserAgent). This means that Go HTTP client would use the default "Go-http-client/2.0" string. Suppressing the header altogether is still possible by setting it to empty string.
1 parent e4729d0 commit 84585f9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

colly.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,10 @@ func (c *Collector) scrape(u, method string, depth int, requestData io.Reader, c
558558
}
559559

560560
if hdr == nil {
561-
hdr = http.Header{"User-Agent": []string{c.UserAgent}}
561+
hdr = http.Header{}
562+
}
563+
if _, ok := hdr["User-Agent"]; !ok {
564+
hdr.Set("User-Agent", c.UserAgent)
562565
}
563566
rc, ok := requestData.(io.ReadCloser)
564567
if !ok && requestData != nil {

colly_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,17 @@ func TestUserAgent(t *testing.T) {
10091009
t.Errorf("mismatched User-Agent (nil hdr): got=%q want=%q", got, want)
10101010
}
10111011
}()
1012+
func() {
1013+
c := NewCollector(UserAgent(exampleUserAgent1))
1014+
c.OnResponse(func(resp *Response) {
1015+
receivedUserAgent = string(resp.Body)
1016+
})
1017+
1018+
c.Request("GET", ts.URL+"/user_agent", nil, nil, http.Header{})
1019+
if got, want := receivedUserAgent, exampleUserAgent1; got != want {
1020+
t.Errorf("mismatched User-Agent (non-nil hdr): got=%q want=%q", got, want)
1021+
}
1022+
}()
10121023
func() {
10131024
c := NewCollector(UserAgent(exampleUserAgent1))
10141025
c.OnResponse(func(resp *Response) {

0 commit comments

Comments
 (0)