MCPcopy
hub / github.com/TecharoHQ/anubis / TestASNChecker

Function TestASNChecker

lib/thoth/asnchecker_test.go:15–64  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13var _ checker.Impl = &thoth.ASNChecker{}
14
15func TestASNChecker(t *testing.T) {
16 cli := loadSecrets(t)
17
18 asnc := cli.ASNCheckerFor([]uint32{13335})
19
20 for _, cs := range []struct {
21 ipAddress string
22 wantMatch bool
23 wantError bool
24 }{
25 {
26 ipAddress: "1.1.1.1",
27 wantMatch: true,
28 wantError: false,
29 },
30 {
31 ipAddress: "2.2.2.2",
32 wantMatch: false,
33 wantError: false,
34 },
35 {
36 ipAddress: "taco",
37 wantMatch: false,
38 wantError: false,
39 },
40 {
41 ipAddress: "127.0.0.1",
42 wantMatch: false,
43 wantError: false,
44 },
45 } {
46 t.Run(fmt.Sprintf("%v", cs), func(t *testing.T) {
47 req := httptest.NewRequest("GET", "/", nil)
48 req.Header.Set("X-Real-Ip", cs.ipAddress)
49
50 match, err := asnc.Check(req)
51
52 if match != cs.wantMatch {
53 t.Errorf("Wanted match: %v, got: %v", cs.wantMatch, match)
54 }
55
56 switch {
57 case err != nil && !cs.wantError:
58 t.Errorf("Did not want error but got: %v", err)
59 case err == nil && cs.wantError:
60 t.Error("Wanted error but got none")
61 }
62 })
63 }
64}
65
66func BenchmarkWithCache(b *testing.B) {
67 cli := loadSecrets(b)

Callers

nothing calls this directly

Calls 5

loadSecretsFunction · 0.85
ASNCheckerForMethod · 0.80
ErrorMethod · 0.80
SetMethod · 0.65
CheckMethod · 0.65

Tested by

no test coverage detected