github.com/kjdelisle/consul@v1.4.5/agent/blacklist_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestBlacklist(t *testing.T) {
     8  	t.Parallel()
     9  
    10  	complex := []string{
    11  		"/a",
    12  		"/b/c",
    13  	}
    14  
    15  	tests := []struct {
    16  		desc     string
    17  		prefixes []string
    18  		path     string
    19  		block    bool
    20  	}{
    21  		{"nothing blocked root", nil, "/", false},
    22  		{"nothing blocked path", nil, "/a", false},
    23  		{"exact match 1", complex, "/a", true},
    24  		{"exact match 2", complex, "/b/c", true},
    25  		{"subpath", complex, "/a/b", true},
    26  		{"longer prefix", complex, "/apple", true},
    27  		{"longer subpath", complex, "/b/c/d", true},
    28  		{"partial prefix", complex, "/b/d", false},
    29  		{"no match", complex, "/c", false},
    30  	}
    31  	for _, tt := range tests {
    32  		t.Run(tt.desc, func(t *testing.T) {
    33  			blacklist := NewBlacklist(tt.prefixes)
    34  			if got, want := blacklist.Block(tt.path), tt.block; got != want {
    35  				t.Fatalf("got %v want %v", got, want)
    36  			}
    37  		})
    38  	}
    39  }