github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/http/request_annotations_test.go (about)

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