Skip to content
Merged
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
8 changes: 4 additions & 4 deletions colly.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ func (c *Collector) fetch(u, method string, depth int, requestData io.Reader, ct
ID: atomic.AddUint32(&c.requestCount, 1),
}

if req.Header.Get("Accept") == "" {
req.Header.Set("Accept", "*/*")
}

c.handleOnRequest(request)

if request.abort {
Expand All @@ -681,10 +685,6 @@ func (c *Collector) fetch(u, method string, depth int, requestData io.Reader, ct
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}

if req.Header.Get("Accept") == "" {
req.Header.Set("Accept", "*/*")
}

var hTrace *HTTPTrace
if c.TraceHTTP {
hTrace = &HTTPTrace{}
Expand Down
38 changes: 38 additions & 0 deletions colly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func newUnstartedTestServer() *httptest.Server {
w.Write([]byte(r.Host))
})

mux.HandleFunc("/accept_header", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(r.Header.Get("Accept")))
})

mux.HandleFunc("/custom_header", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(r.Header.Get("Test")))
Expand Down Expand Up @@ -424,6 +429,39 @@ var newCollectorTests = map[string]func(*testing.T){
},
}

func TestNoAcceptHeader(t *testing.T) {
ts := newTestServer()
defer ts.Close()

var receivedHeader string
// checks if Accept is enabled by default
func() {
c := NewCollector()
c.OnResponse(func(resp *Response) {
receivedHeader = string(resp.Body)
})
c.Visit(ts.URL + "/accept_header")
if receivedHeader != "*/*" {
t.Errorf("default Accept header isn't */*. got: %v", receivedHeader)
}
}()

// checks if Accept can be disabled
func() {
c := NewCollector()
c.OnRequest(func(r *Request) {
r.Headers.Del("Accept")
})
c.OnResponse(func(resp *Response) {
receivedHeader = string(resp.Body)
})
c.Visit(ts.URL + "/accept_header")
if receivedHeader != "" {
t.Errorf("failed to pass request with no Accept header. got: %v", receivedHeader)
}
}()
}

func TestNewCollector(t *testing.T) {
t.Run("Functional Options", func(t *testing.T) {
for name, test := range newCollectorTests {
Expand Down