Weighs the soul of incoming HTTP requests to stop AI crawlers
at main 2.9 kB view raw
1package lib 2 3import ( 4 "net/http/httptest" 5 "testing" 6 7 "github.com/TecharoHQ/anubis" 8) 9 10func TestSetCookie(t *testing.T) { 11 for _, tt := range []struct { 12 name string 13 options Options 14 host string 15 cookieName string 16 }{ 17 { 18 name: "basic", 19 options: Options{}, 20 host: "", 21 cookieName: anubis.CookieName, 22 }, 23 { 24 name: "domain techaro.lol", 25 options: Options{CookieDomain: "techaro.lol"}, 26 host: "", 27 cookieName: anubis.CookieName, 28 }, 29 { 30 name: "dynamic cookie domain", 31 options: Options{CookieDynamicDomain: true}, 32 host: "techaro.lol", 33 cookieName: anubis.CookieName, 34 }, 35 } { 36 t.Run(tt.name, func(t *testing.T) { 37 srv := spawnAnubis(t, tt.options) 38 rw := httptest.NewRecorder() 39 40 srv.SetCookie(rw, CookieOpts{Value: "test", Host: tt.host}) 41 42 resp := rw.Result() 43 cookies := resp.Cookies() 44 45 ckie := cookies[0] 46 47 if ckie.Name != tt.cookieName { 48 t.Errorf("wanted cookie named %q, got cookie named %q", tt.cookieName, ckie.Name) 49 } 50 }) 51 } 52} 53 54func TestClearCookie(t *testing.T) { 55 srv := spawnAnubis(t, Options{}) 56 rw := httptest.NewRecorder() 57 58 srv.ClearCookie(rw, CookieOpts{Host: "localhost"}) 59 60 resp := rw.Result() 61 62 cookies := resp.Cookies() 63 64 if len(cookies) != 1 { 65 t.Errorf("wanted 1 cookie, got %d cookies", len(cookies)) 66 } 67 68 ckie := cookies[0] 69 70 if ckie.Name != anubis.CookieName { 71 t.Errorf("wanted cookie named %q, got cookie named %q", anubis.CookieName, ckie.Name) 72 } 73 74 if ckie.MaxAge != -1 { 75 t.Errorf("wanted cookie max age of -1, got: %d", ckie.MaxAge) 76 } 77} 78 79func TestClearCookieWithDomain(t *testing.T) { 80 srv := spawnAnubis(t, Options{CookieDomain: "techaro.lol"}) 81 rw := httptest.NewRecorder() 82 83 srv.ClearCookie(rw, CookieOpts{Host: "localhost"}) 84 85 resp := rw.Result() 86 87 cookies := resp.Cookies() 88 89 if len(cookies) != 1 { 90 t.Errorf("wanted 1 cookie, got %d cookies", len(cookies)) 91 } 92 93 ckie := cookies[0] 94 95 if ckie.Name != anubis.CookieName { 96 t.Errorf("wanted cookie named %q, got cookie named %q", anubis.CookieName, ckie.Name) 97 } 98 99 if ckie.MaxAge != -1 { 100 t.Errorf("wanted cookie max age of -1, got: %d", ckie.MaxAge) 101 } 102} 103 104func TestClearCookieWithDynamicDomain(t *testing.T) { 105 srv := spawnAnubis(t, Options{CookieDynamicDomain: true}) 106 rw := httptest.NewRecorder() 107 108 srv.ClearCookie(rw, CookieOpts{Host: "subdomain.xeiaso.net"}) 109 110 resp := rw.Result() 111 112 cookies := resp.Cookies() 113 114 if len(cookies) != 1 { 115 t.Errorf("wanted 1 cookie, got %d cookies", len(cookies)) 116 } 117 118 ckie := cookies[0] 119 120 if ckie.Name != anubis.CookieName { 121 t.Errorf("wanted cookie named %q, got cookie named %q", anubis.CookieName, ckie.Name) 122 } 123 124 if ckie.Domain != "xeiaso.net" { 125 t.Errorf("wanted cookie domain %q, got cookie domain %q", "xeiaso.net", ckie.Domain) 126 } 127 128 if ckie.MaxAge != -1 { 129 t.Errorf("wanted cookie max age of -1, got: %d", ckie.MaxAge) 130 } 131}