MCPcopy
hub / github.com/crowdsecurity/crowdsec / TestAcquistionSocket

Function TestAcquistionSocket

pkg/acquisition/modules/http/http_test.go:343–389  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

341}
342
343func TestAcquistionSocket(t *testing.T) {
344 tempDir := t.TempDir()
345 socketFile := filepath.Join(tempDir, "test.sock")
346
347 ctx := t.Context()
348 h := &Source{}
349 out, reg, tomb := SetupAndRunHTTPSource(t, h, []byte(`
350source: http
351listen_socket: `+socketFile+`
352path: /test
353auth_type: headers
354headers:
355 key: test`), 2)
356
357 time.Sleep(1 * time.Second)
358 rawEvt := `{"test": "test"}`
359 errChan := make(chan error)
360 go assertEvents(out, []string{rawEvt}, errChan)
361
362 dialer := &net.Dialer{}
363 client := &http.Client{
364 Transport: &http.Transport{
365 DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
366 return dialer.DialContext(ctx, "unix", socketFile)
367 },
368 },
369 }
370 req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/test", testHTTPServerAddr), strings.NewReader(rawEvt))
371 require.NoError(t, err)
372
373 req.Header.Add("Key", "test")
374 resp, err := client.Do(req)
375 require.NoError(t, err)
376
377 assert.Equal(t, http.StatusOK, resp.StatusCode)
378 closeBody(t, resp)
379
380 err = <-errChan
381 require.NoError(t, err)
382
383 assertMetrics(t, reg, h.GetMetrics(), 1)
384
385 h.Server.Close()
386 tomb.Kill(nil)
387 err = tomb.Wait()
388 require.NoError(t, err)
389}
390
391type slowReader struct {
392 delay time.Duration

Callers

nothing calls this directly

Calls 10

GetMetricsMethod · 0.95
SetupAndRunHTTPSourceFunction · 0.85
assertEventsFunction · 0.85
closeBodyFunction · 0.85
assertMetricsFunction · 0.85
NewReaderMethod · 0.80
DoMethod · 0.80
KillMethod · 0.80
AddMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…