Skip to content

Commit e664321

Browse files
authored
Merge pull request #592 from gocolly/fix-user-agent
Fix default User-Agent when using custom headers
2 parents a888c12 + 84585f9 commit e664321

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,86 @@ func TestEnvSettings(t *testing.T) {
968968
}
969969
}
970970

971+
func TestUserAgent(t *testing.T) {
972+
const exampleUserAgent1 = "Example/1.0"
973+
const exampleUserAgent2 = "Example/2.0"
974+
const defaultUserAgent = "colly - https://github.com/gocolly/colly/v2"
975+
976+
ts := newTestServer()
977+
defer ts.Close()
978+
979+
var receivedUserAgent string
980+
981+
func() {
982+
c := NewCollector()
983+
c.OnResponse(func(resp *Response) {
984+
receivedUserAgent = string(resp.Body)
985+
})
986+
c.Visit(ts.URL + "/user_agent")
987+
if got, want := receivedUserAgent, defaultUserAgent; got != want {
988+
t.Errorf("mismatched User-Agent: got=%q want=%q", got, want)
989+
}
990+
}()
991+
func() {
992+
c := NewCollector(UserAgent(exampleUserAgent1))
993+
c.OnResponse(func(resp *Response) {
994+
receivedUserAgent = string(resp.Body)
995+
})
996+
c.Visit(ts.URL + "/user_agent")
997+
if got, want := receivedUserAgent, exampleUserAgent1; got != want {
998+
t.Errorf("mismatched User-Agent: got=%q want=%q", got, want)
999+
}
1000+
}()
1001+
func() {
1002+
c := NewCollector(UserAgent(exampleUserAgent1))
1003+
c.OnResponse(func(resp *Response) {
1004+
receivedUserAgent = string(resp.Body)
1005+
})
1006+
1007+
c.Request("GET", ts.URL+"/user_agent", nil, nil, nil)
1008+
if got, want := receivedUserAgent, exampleUserAgent1; got != want {
1009+
t.Errorf("mismatched User-Agent (nil hdr): got=%q want=%q", got, want)
1010+
}
1011+
}()
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+
}()
1023+
func() {
1024+
c := NewCollector(UserAgent(exampleUserAgent1))
1025+
c.OnResponse(func(resp *Response) {
1026+
receivedUserAgent = string(resp.Body)
1027+
})
1028+
hdr := http.Header{}
1029+
hdr.Set("User-Agent", "")
1030+
1031+
c.Request("GET", ts.URL+"/user_agent", nil, nil, hdr)
1032+
if got, want := receivedUserAgent, ""; got != want {
1033+
t.Errorf("mismatched User-Agent (hdr with empty UA): got=%q want=%q", got, want)
1034+
}
1035+
}()
1036+
func() {
1037+
c := NewCollector(UserAgent(exampleUserAgent1))
1038+
c.OnResponse(func(resp *Response) {
1039+
receivedUserAgent = string(resp.Body)
1040+
})
1041+
hdr := http.Header{}
1042+
hdr.Set("User-Agent", exampleUserAgent2)
1043+
1044+
c.Request("GET", ts.URL+"/user_agent", nil, nil, hdr)
1045+
if got, want := receivedUserAgent, exampleUserAgent2; got != want {
1046+
t.Errorf("mismatched User-Agent (hdr with UA): got=%q want=%q", got, want)
1047+
}
1048+
}()
1049+
}
1050+
9711051
func TestParseHTTPErrorResponse(t *testing.T) {
9721052
contentCount := 0
9731053
ts := newTestServer()

0 commit comments

Comments
 (0)