mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-10 20:05:55 +02:00
CI: enable linter "noctx" (#3528)
* CI: enable linter "noctx" * rename NewRequestWithContext() -> PrepareRequest()
This commit is contained in:
parent
a718475422
commit
c245b1e6f8
14 changed files with 77 additions and 45 deletions
|
@ -284,7 +284,6 @@ linters:
|
|||
- ireturn # Accept Interfaces, Return Concrete Types
|
||||
- mnd # An analyzer to detect magic numbers.
|
||||
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value.
|
||||
- noctx # Finds sending http request without context.Context
|
||||
- unparam # Reports unused function parameters
|
||||
|
||||
#
|
||||
|
|
|
@ -254,7 +254,12 @@ basic_auth:
|
|||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/test", testHTTPServerAddr))
|
||||
ctx := t.Context()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, testHTTPServerAddr + "/test", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusMethodNotAllowed, res.StatusCode)
|
||||
|
||||
|
@ -265,6 +270,8 @@ basic_auth:
|
|||
}
|
||||
|
||||
func TestStreamingAcquisitionUnknownPath(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
|
||||
h := &HTTPSource{}
|
||||
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
|
||||
source: http
|
||||
|
@ -277,7 +284,10 @@ basic_auth:
|
|||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("%s/unknown", testHTTPServerAddr))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, testHTTPServerAddr + "/unknown", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusNotFound, res.StatusCode)
|
||||
|
||||
|
@ -303,11 +313,15 @@ basic_auth:
|
|||
|
||||
client := &http.Client{}
|
||||
|
||||
resp, err := http.Post(fmt.Sprintf("%s/test", testHTTPServerAddr), "application/json", strings.NewReader("test"))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, testHTTPServerAddr + "/test", strings.NewReader("test"))
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/test", testHTTPServerAddr), strings.NewReader("test"))
|
||||
req, err = http.NewRequestWithContext(ctx, http.MethodPost, testHTTPServerAddr + "/test", strings.NewReader("test"))
|
||||
require.NoError(t, err)
|
||||
req.SetBasicAuth("test", "WrongPassword")
|
||||
|
||||
|
@ -553,6 +567,8 @@ timeout: 1s`), 0)
|
|||
}
|
||||
|
||||
func TestStreamingAcquisitionTLSHTTPRequest(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
|
||||
h := &HTTPSource{}
|
||||
_, _, tomb := SetupAndRunHTTPSource(t, h, []byte(`
|
||||
source: http
|
||||
|
@ -566,7 +582,11 @@ tls:
|
|||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
resp, err := http.Post(fmt.Sprintf("%s/test", testHTTPServerAddr), "application/json", strings.NewReader("test"))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, testHTTPServerAddr + "/test", strings.NewReader("test"))
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
||||
|
|
|
@ -49,7 +49,7 @@ type AlertsDeleteOpts struct {
|
|||
func (s *AlertsService) Add(ctx context.Context, alerts models.AddAlertsRequest) (*models.AddAlertsResponse, *Response, error) {
|
||||
u := fmt.Sprintf("%s/alerts", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodPost, u, &alerts)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &alerts)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ func (s *AlertsService) List(ctx context.Context, opts AlertsListOpts) (*models.
|
|||
URI = fmt.Sprintf("%s?%s", URI, params.Encode())
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, URI, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, URI, nil)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("building request: %w", err)
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ func (s *AlertsService) Delete(ctx context.Context, opts AlertsDeleteOpts) (*mod
|
|||
|
||||
u := fmt.Sprintf("%s/alerts?%s", s.client.URLPrefix, params.Encode())
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodDelete, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ func (s *AlertsService) Delete(ctx context.Context, opts AlertsDeleteOpts) (*mod
|
|||
func (s *AlertsService) DeleteOne(ctx context.Context, alertID string) (*models.DeleteAlertsResponse, *Response, error) {
|
||||
u := fmt.Sprintf("%s/alerts/%s", s.client.URLPrefix, alertID)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodDelete, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ func (s *AlertsService) DeleteOne(ctx context.Context, alertID string) (*models.
|
|||
func (s *AlertsService) GetByID(ctx context.Context, alertID int) (*models.Alert, *Response, error) {
|
||||
u := fmt.Sprintf("%s/alerts/%d", s.client.URLPrefix, alertID)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ func (s *AllowlistsService) List(ctx context.Context, opts AllowlistListOpts) (*
|
|||
|
||||
u += "?" + params.Encode()
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ func (s *AllowlistsService) Get(ctx context.Context, name string, opts Allowlist
|
|||
|
||||
log.Debugf("GET %s", u)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ func (s *AllowlistsService) Get(ctx context.Context, name string, opts Allowlist
|
|||
func (s *AllowlistsService) CheckIfAllowlisted(ctx context.Context, value string) (bool, *Response, error) {
|
||||
u := s.client.URLPrefix + "/allowlists/check/" + value
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodHead, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodHead, u, nil)
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ func (s *AllowlistsService) CheckIfAllowlisted(ctx context.Context, value string
|
|||
func (s *AllowlistsService) CheckIfAllowlistedWithReason(ctx context.Context, value string) (*models.CheckAllowlistResponse, *Response, error) {
|
||||
u := s.client.URLPrefix + "/allowlists/check/" + value
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ type enrollRequest struct {
|
|||
func (s *AuthService) UnregisterWatcher(ctx context.Context) (*Response, error) {
|
||||
u := fmt.Sprintf("%s/watchers", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodDelete, u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ func (s *AuthService) UnregisterWatcher(ctx context.Context) (*Response, error)
|
|||
func (s *AuthService) RegisterWatcher(ctx context.Context, registration models.WatcherRegistrationRequest) (*Response, error) {
|
||||
u := fmt.Sprintf("%s/watchers", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodPost, u, ®istration)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, ®istration)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.Watch
|
|||
|
||||
u := fmt.Sprintf("%s/watchers/login", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodPost, u, &auth)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &auth)
|
||||
if err != nil {
|
||||
return authResp, nil, err
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.Watch
|
|||
func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string, overwrite bool) (*Response, error) {
|
||||
u := fmt.Sprintf("%s/watchers/enroll", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodPost, u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, Overwrite: overwrite})
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, Overwrite: overwrite})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import (
|
|||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (c *ApiClient) NewRequestWithContext(ctx context.Context, method, url string, body interface{}) (*http.Request, error) {
|
||||
func (c *ApiClient) PrepareRequest(ctx context.Context, method, url string, body interface{}) (*http.Request, error) {
|
||||
if !strings.HasSuffix(c.BaseURL.Path, "/") {
|
||||
return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", c.BaseURL)
|
||||
}
|
||||
|
|
|
@ -81,7 +81,7 @@ func (s *DecisionsService) List(ctx context.Context, opts DecisionsListOpts) (*m
|
|||
|
||||
u := fmt.Sprintf("%s/decisions?%s", s.client.URLPrefix, params.Encode())
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ func (s *DecisionsService) List(ctx context.Context, opts DecisionsListOpts) (*m
|
|||
}
|
||||
|
||||
func (s *DecisionsService) FetchV2Decisions(ctx context.Context, url string) (*models.DecisionsStreamResponse, *Response, error) {
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ func (s *DecisionsService) FetchV3Decisions(ctx context.Context, url string) (*m
|
|||
scenarioDeleted := "deleted"
|
||||
durationDeleted := "1h"
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -271,7 +271,7 @@ func (s *DecisionsService) GetStreamV3(ctx context.Context, opts DecisionsStream
|
|||
return nil, nil, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -289,7 +289,7 @@ func (s *DecisionsService) GetStreamV3(ctx context.Context, opts DecisionsStream
|
|||
func (s *DecisionsService) StopStream(ctx context.Context) (*Response, error) {
|
||||
u := fmt.Sprintf("%s/decisions", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodDelete, u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -310,7 +310,7 @@ func (s *DecisionsService) Delete(ctx context.Context, opts DecisionsDeleteOpts)
|
|||
|
||||
u := fmt.Sprintf("%s/decisions?%s", s.client.URLPrefix, params.Encode())
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodDelete, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -328,7 +328,7 @@ func (s *DecisionsService) Delete(ctx context.Context, opts DecisionsDeleteOpts)
|
|||
func (s *DecisionsService) DeleteOne(ctx context.Context, decisionID string) (*models.DeleteDecisionResponse, *Response, error) {
|
||||
u := fmt.Sprintf("%s/decisions/%s", s.client.URLPrefix, decisionID)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodDelete, u, nil)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodDelete, u, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ type DecisionDeleteService service
|
|||
func (d *DecisionDeleteService) Add(ctx context.Context, deletedDecisions *models.DecisionsDeleteRequest) (interface{}, *Response, error) {
|
||||
u := fmt.Sprintf("%s/decisions/delete", d.client.URLPrefix)
|
||||
|
||||
req, err := d.client.NewRequestWithContext(ctx, http.MethodPost, u, &deletedDecisions)
|
||||
req, err := d.client.PrepareRequest(ctx, http.MethodPost, u, &deletedDecisions)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("while building request: %w", err)
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ type HeartBeatService service
|
|||
func (h *HeartBeatService) Ping(ctx context.Context) (bool, *Response, error) {
|
||||
u := fmt.Sprintf("%s/heartbeat", h.client.URLPrefix)
|
||||
|
||||
req, err := h.client.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
req, err := h.client.PrepareRequest(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ type MetricsService service
|
|||
func (s *MetricsService) Add(ctx context.Context, metrics *models.Metrics) (interface{}, *Response, error) {
|
||||
u := fmt.Sprintf("%s/metrics/", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodPost, u, &metrics)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &metrics)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ type SignalService service
|
|||
func (s *SignalService) Add(ctx context.Context, signals *models.AddSignalsRequest) (interface{}, *Response, error) {
|
||||
u := fmt.Sprintf("%s/signals", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodPost, u, &signals)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &signals)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("while building request: %w", err)
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ type UsageMetricsService service
|
|||
func (s *UsageMetricsService) Add(ctx context.Context, metrics *models.AllMetrics) (interface{}, *Response, error) {
|
||||
u := fmt.Sprintf("%s/usage-metrics", s.client.URLPrefix)
|
||||
|
||||
req, err := s.client.NewRequestWithContext(ctx, http.MethodPost, u, &metrics)
|
||||
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &metrics)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
|
@ -754,7 +754,13 @@ func (a *apic) UpdateAllowlists(ctx context.Context, allowlistsLinks []*modelsca
|
|||
description = *link.Description
|
||||
}
|
||||
|
||||
resp, err := defaultClient.GetClient().Get(*link.URL)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, *link.URL, http.NoBody)
|
||||
if err != nil {
|
||||
log.Errorf("while pulling allowlist: %s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
resp, err := defaultClient.GetClient().Do(req)
|
||||
if err != nil {
|
||||
log.Errorf("while pulling allowlist: %s", err)
|
||||
continue
|
||||
|
|
|
@ -21,6 +21,8 @@ For those hashes, the value used was the one returned by our code (because we de
|
|||
*/
|
||||
|
||||
func TestJA4H_A(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
request func() *http.Request
|
||||
|
@ -29,7 +31,7 @@ func TestJA4H_A(t *testing.T) {
|
|||
{
|
||||
name: "basic GET request - HTTP1.1 - no accept-language header",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
return req
|
||||
},
|
||||
expectedResult: "ge11nn000000",
|
||||
|
@ -37,7 +39,7 @@ func TestJA4H_A(t *testing.T) {
|
|||
{
|
||||
name: "basic GET request - HTTP1.1 - with accept-language header",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("Accept-Language", "en-US")
|
||||
return req
|
||||
},
|
||||
|
@ -46,7 +48,7 @@ func TestJA4H_A(t *testing.T) {
|
|||
{
|
||||
name: "basic POST request - HTTP1.1 - no accept-language header - cookies - referer",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodPost, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "http://example.com", http.NoBody)
|
||||
req.AddCookie(&http.Cookie{Name: "foo", Value: "bar"})
|
||||
req.Header.Set("Referer", "http://example.com")
|
||||
return req
|
||||
|
@ -56,7 +58,7 @@ func TestJA4H_A(t *testing.T) {
|
|||
{
|
||||
name: "bad accept-language header",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("Accept-Language", "aksjdhaslkdhalkjsd")
|
||||
return req
|
||||
},
|
||||
|
@ -65,7 +67,7 @@ func TestJA4H_A(t *testing.T) {
|
|||
{
|
||||
name: "bad accept-language header 2",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("Accept-Language", ",")
|
||||
return req
|
||||
},
|
||||
|
@ -86,6 +88,9 @@ func TestJA4H_A(t *testing.T) {
|
|||
func TestJA4H_B(t *testing.T) {
|
||||
// This test is only for non-regression
|
||||
// Because go does not keep headers order, we just want to make sure our code always process the headers in the same order
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
request func() *http.Request
|
||||
|
@ -94,7 +99,7 @@ func TestJA4H_B(t *testing.T) {
|
|||
{
|
||||
name: "no headers",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
return req
|
||||
},
|
||||
expectedResult: "e3b0c44298fc",
|
||||
|
@ -102,7 +107,7 @@ func TestJA4H_B(t *testing.T) {
|
|||
{
|
||||
name: "header with arbitrary content",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("X-Custom-Header", "some value")
|
||||
return req
|
||||
},
|
||||
|
@ -111,7 +116,7 @@ func TestJA4H_B(t *testing.T) {
|
|||
{
|
||||
name: "header with multiple headers",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.Header.Set("X-Custom-Header", "some value")
|
||||
req.Header.Set("Authorization", "Bearer token")
|
||||
return req
|
||||
|
@ -121,7 +126,7 @@ func TestJA4H_B(t *testing.T) {
|
|||
{
|
||||
name: "curl-like request",
|
||||
request: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", http.NoBody)
|
||||
req.Header.Set("Host", "localhost")
|
||||
req.Header.Set("User-Agent", "curl/8.12.1")
|
||||
req.Header.Set("Accept", "*/*")
|
||||
|
@ -260,6 +265,8 @@ func TestJA4H_D(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestJA4H(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
req func() *http.Request
|
||||
|
@ -268,7 +275,7 @@ func TestJA4H(t *testing.T) {
|
|||
{
|
||||
name: "Basic GET - No cookies",
|
||||
req: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
return req
|
||||
},
|
||||
expectedHash: "ge11nn000000_e3b0c44298fc_000000000000_000000000000",
|
||||
|
@ -276,7 +283,7 @@ func TestJA4H(t *testing.T) {
|
|||
{
|
||||
name: "Basic GET - With cookies",
|
||||
req: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.AddCookie(&http.Cookie{Name: "session", Value: "12345"})
|
||||
return req
|
||||
},
|
||||
|
@ -285,7 +292,7 @@ func TestJA4H(t *testing.T) {
|
|||
{
|
||||
name: "Basic GET - Multiple cookies",
|
||||
req: func() *http.Request {
|
||||
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "http://example.com", http.NoBody)
|
||||
req.AddCookie(&http.Cookie{Name: "foo", Value: "bar"})
|
||||
req.AddCookie(&http.Cookie{Name: "baz", Value: "qux"})
|
||||
return req
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue