Weighs the soul of incoming HTTP requests to stop AI crawlers
1package internal
2
3import (
4 "bytes"
5 "log"
6 "strings"
7 "testing"
8)
9
10func TestErrorLogFilter(t *testing.T) {
11 var buf bytes.Buffer
12 destLogger := log.New(&buf, "", 0)
13 errorFilterWriter := &ErrorLogFilter{Unwrap: destLogger}
14 testErrorLogger := log.New(errorFilterWriter, "", 0)
15
16 // Test Case 1: Suppressed message
17 suppressedMessage := "http: proxy error: context canceled"
18 testErrorLogger.Println(suppressedMessage)
19
20 if buf.Len() != 0 {
21 t.Errorf("Suppressed message was written to output. Output: %q", buf.String())
22 }
23 buf.Reset()
24
25 // Test Case 2: Allowed message
26 allowedMessage := "http: another error occurred"
27 testErrorLogger.Println(allowedMessage)
28
29 output := buf.String()
30 if !strings.Contains(output, allowedMessage) {
31 t.Errorf("Allowed message was not written to output. Output: %q", output)
32 }
33 if !strings.HasSuffix(output, "\n") {
34 t.Errorf("Allowed message output is missing newline. Output: %q", output)
35 }
36 buf.Reset()
37
38 // Test Case 3: Partially matching message (should be suppressed)
39 partiallyMatchingMessage := "Some other log before http: proxy error: context canceled and after"
40 testErrorLogger.Println(partiallyMatchingMessage)
41
42 if buf.Len() != 0 {
43 t.Errorf("Partially matching message was written to output. Output: %q", buf.String())
44 }
45 buf.Reset()
46}