Skip to content

Commit

Permalink
add a few more tests to request modifiers and status rules
Browse files Browse the repository at this point in the history
  • Loading branch information
atomicptr committed Apr 4, 2020
1 parent 309e212 commit edef46d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 3 additions & 5 deletions pkg/cli/crawl/request_modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ func addUserAgentToRequest() crawler.RequestModifierFunc {
func addPrefixUrlToRequest(prefixUrl string) crawler.RequestModifierFunc {
return func(req *http.Request) {
parsedPrefixUrl, err := url.Parse(prefixUrl)
if err != nil {
if err != nil || parsedPrefixUrl.String() == "" {
// prefix url couldn't be parsed just abort
return
}

requestUrl, err := url.Parse(req.URL.String())
if err != nil {
return
}
requestUrl, _ := url.Parse(req.URL.String())

requestUrl.Scheme = parsedPrefixUrl.Scheme
requestUrl.Host = parsedPrefixUrl.Host
Expand Down
12 changes: 12 additions & 0 deletions pkg/cli/crawl/request_modifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func TestAddPrefixUrlToRequest(t *testing.T) {
}
}

func TestAddPrefixUrlToRequestWithInvalidPrefixUrl(t *testing.T) {
prefixUrl := "https://this is not a valid url"

modifier := crawler.RequestModifier{}
modifier.With(addPrefixUrlToRequest(prefixUrl))

req := httptest.NewRequest("GET", "https://example.com", strings.NewReader(""))
modifier.Do(req)

assert.Equal(t, "https://example.com", req.URL.String())
}

func TestAddCookiesToRequest(t *testing.T) {
modifier := crawler.RequestModifier{}
modifier.With(addCookiesToRequest(crawlerFlagOptions{
Expand Down
8 changes: 8 additions & 0 deletions pkg/filter/status_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func TestRuleIsGreaterThan(t *testing.T) {
assert.Equal(t, ResultDoesNotApply, RuleIsGreaterThan("200-299", 200))
}

func TestRuleIsGreaterThanInvalidQuery(t *testing.T) {
assert.Equal(t, ResultDoesNotApply, RuleIsGreaterThan(">test", 200))
}

func TestRuleIsSmallerThanInvalidQuery(t *testing.T) {
assert.Equal(t, ResultDoesNotApply, RuleIsSmallerThan("<test", 200))
}

func TestRuleIsSmallerThan(t *testing.T) {
assert.Equal(t, ResultTrue, RuleIsSmallerThan("<400", 399))
assert.Equal(t, ResultFalse, RuleIsSmallerThan("<400", 400))
Expand Down

0 comments on commit edef46d

Please sign in to comment.