forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_annotations_test.go
48 lines (40 loc) · 1.68 KB
/
request_annotations_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package http
import (
"context"
"net/http"
"testing"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/httpclientpool"
"github.com/projectdiscovery/retryablehttp-go"
"github.com/stretchr/testify/require"
)
func TestRequestParseAnnotationsTimeout(t *testing.T) {
t.Run("positive", func(t *testing.T) {
request := &Request{
connConfiguration: &httpclientpool.Configuration{NoTimeout: true},
}
rawRequest := `@timeout: 2s
GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com", nil)
require.Nil(t, err, "could not create http request")
overrides, modified := request.parseAnnotations(rawRequest, httpReq)
require.NotNil(t, overrides.cancelFunc, "could not initialize valid cancel function")
require.True(t, modified, "could not get correct modified value")
_, deadlined := overrides.request.Context().Deadline()
require.True(t, deadlined, "could not get set request deadline")
})
t.Run("negative", func(t *testing.T) {
request := &Request{
connConfiguration: &httpclientpool.Configuration{},
}
rawRequest := `GET / HTTP/1.1
Host: {{Hostname}}`
httpReq, err := retryablehttp.NewRequestWithContext(context.Background(), http.MethodGet, "https://example.com", nil)
require.Nil(t, err, "could not create http request")
newRequestWithOverrides, modified := request.parseAnnotations(rawRequest, httpReq)
require.Nil(t, newRequestWithOverrides.cancelFunc, "cancel function should be nil")
require.False(t, modified, "could not get correct modified value")
_, deadlined := newRequestWithOverrides.request.Context().Deadline()
require.False(t, deadlined, "could not get set request deadline")
})
}