Skip to content

Commit

Permalink
fixed check for accept header, added accept_header to test server
Browse files Browse the repository at this point in the history
  • Loading branch information
k4lizen committed Oct 18, 2023
1 parent a485676 commit e181623
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 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 @@ -428,20 +433,33 @@ func TestNoAcceptHeader(t *testing.T) {
ts := newTestServer()
defer ts.Close()

c := NewCollector()

c.OnRequest(func(r *Request) {
r.Headers.Del("Accept")
})

c.OnResponse(func(r *Response) {
ret := r.Request.Headers.Get("Accept")
if ret != "" {
t.Error("Failed to pass request with no Accept header.")
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)
}
})
}()

c.Visit(ts.URL)
// 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) {
Expand Down

0 comments on commit e181623

Please sign in to comment.