diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-03-23 18:46:01 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-23 18:46:01 -0400 |
| commit | 725e11d3a643bd6e909aa00aa2297a34765b7bca (patch) | |
| tree | 1b85f32537e501e6e0ff7f1a2fb6e1add71eb0ea /lib/anubis_test.go | |
| parent | f462209b02cbb62864f5c4ccacd5a31cca5b138e (diff) | |
| download | anubis-725e11d3a643bd6e909aa00aa2297a34765b7bca.tar.xz anubis-725e11d3a643bd6e909aa00aa2297a34765b7bca.zip | |
lib: fix default difficulty (#96)
Before this did not respect the difficulty flag and instead used
difficulty 4. This has been fixed.
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'lib/anubis_test.go')
| -rw-r--r-- | lib/anubis_test.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/anubis_test.go b/lib/anubis_test.go new file mode 100644 index 0000000..0498c13 --- /dev/null +++ b/lib/anubis_test.go @@ -0,0 +1,81 @@ +package lib + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/TecharoHQ/anubis" +) + +func spawnAnubis(t *testing.T, h http.Handler) string { + t.Helper() + + policy, err := LoadPoliciesOrDefault("", anubis.DefaultDifficulty) + if err != nil { + t.Fatal(err) + } + + s, err := New(Options{ + Next: h, + Policy: policy, + ServeRobotsTXT: true, + }) + if err != nil { + t.Fatalf("can't construct libanubis.Server: %v", err) + } + + ts := httptest.NewServer(s) + t.Log(ts.URL) + + t.Cleanup(func() { + ts.Close() + }) + + return ts.URL +} + +func TestCheckDefaultDifficultyMatchesPolicy(t *testing.T) { + h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "OK") + }) + + for i := 1; i < 10; i++ { + t.Run(fmt.Sprint(i), func(t *testing.T) { + policy, err := LoadPoliciesOrDefault("", i) + if err != nil { + t.Fatal(err) + } + + s, err := New(Options{ + Next: h, + Policy: policy, + ServeRobotsTXT: true, + }) + if err != nil { + t.Fatalf("can't construct libanubis.Server: %v", err) + } + + req, err := http.NewRequest(http.MethodGet, "/", nil) + if err != nil { + t.Fatal(err) + } + + req.Header.Add("X-Real-Ip", "127.0.0.1") + + _, bot, err := s.check(req) + if err != nil { + t.Fatal(err) + } + + if bot.Challenge.Difficulty != i { + t.Errorf("Challenge.Difficulty is wrong, wanted %d, got: %d", i, bot.Challenge.Difficulty) + } + + if bot.Challenge.ReportAs != i { + t.Errorf("Challenge.ReportAs is wrong, wanted %d, got: %d", i, bot.Challenge.ReportAs) + } + }) + } +} |
