MCPcopy Index your code
hub / github.com/coder/coder / TestServer_AllowRequest

Function TestServer_AllowRequest

agent/boundarylogproxy/proxy_test.go:544–600  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

542}
543
544func TestServer_AllowRequest(t *testing.T) {
545 t.Parallel()
546
547 socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
548 srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath, prometheus.NewRegistry())
549
550 err := srv.Start()
551 require.NoError(t, err)
552 t.Cleanup(func() { require.NoError(t, srv.Close()) })
553
554 reporter := &fakeReporter{}
555
556 ctx, cancel := context.WithCancel(context.Background())
557 defer cancel()
558 forwarderDone := make(chan error, 1)
559 go func() {
560 forwarderDone <- srv.RunForwarder(ctx, reporter)
561 }()
562
563 conn, err := net.Dial("unix", socketPath)
564 require.NoError(t, err)
565 defer conn.Close()
566
567 // Send an allowed request with a matched rule.
568 logTime := timestamppb.Now()
569 req := &agentproto.ReportBoundaryLogsRequest{
570 Logs: []*agentproto.BoundaryLog{
571 {
572 Allowed: true,
573 Time: logTime,
574 Resource: &agentproto.BoundaryLog_HttpRequest_{
575 HttpRequest: &agentproto.BoundaryLog_HttpRequest{
576 Method: "GET",
577 Url: "https://malicious.com/attack",
578 MatchedRule: "*.malicious.com",
579 },
580 },
581 },
582 },
583 }
584 sendLogs(t, conn, req)
585
586 require.Eventually(t, func() bool {
587 logs := reporter.getLogs()
588 return len(logs) == 1
589 }, testutil.WaitShort, testutil.IntervalFast)
590
591 logs := reporter.getLogs()
592 require.Len(t, logs, 1)
593 require.True(t, logs[0].Logs[0].Allowed)
594 require.Equal(t, logTime.Seconds, logs[0].Logs[0].Time.Seconds)
595 require.Equal(t, logTime.Nanos, logs[0].Logs[0].Time.Nanos)
596 require.Equal(t, "*.malicious.com", logs[0].Logs[0].GetHttpRequest().MatchedRule)
597
598 cancel()
599 <-forwarderDone
600}
601

Callers

nothing calls this directly

Calls 14

StartMethod · 0.95
CloseMethod · 0.95
RunForwarderMethod · 0.95
getLogsMethod · 0.95
TempDirUnixSocketFunction · 0.92
NewServerFunction · 0.92
LoggerFunction · 0.92
sendLogsFunction · 0.85
GetHttpRequestMethod · 0.80
CleanupMethod · 0.65
DialMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected