github.com/sirkon/goproxy@v1.4.8/tree_test.go (about)

     1  package goproxy
     2  
     3  import (
     4  	"net/http"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  var _ Plugin = plugin("")
    12  
    13  type plugin string
    14  
    15  func (plugin) Module(req *http.Request, prefix string) (Module, error) { panic("implement me") }
    16  func (plugin) Leave(source Module) error                               { panic("implement me") }
    17  func (plugin) Close() error                                            { panic("implement me") }
    18  func (plugin) String() string                                          { return "plugin" }
    19  
    20  func nodeLists(n *node) [][]string {
    21  	res := [][]string{}
    22  	if n.f != nil {
    23  		res = append(res, nil)
    24  	}
    25  	for _, f := range n.further {
    26  		for _, ff := range nodeLists(f.node) {
    27  			item := make([]string, len(ff)+1)
    28  			item[0] = f.path
    29  			copy(item[1:], ff)
    30  			res = append(res, item)
    31  		}
    32  	}
    33  	sort.Slice(res, func(i, j int) bool {
    34  		a := res[i]
    35  		b := res[j]
    36  		for k := range a {
    37  			if k >= len(b) {
    38  				return false
    39  			}
    40  			if a[k] != b[k] {
    41  				return a[k] < b[k]
    42  			}
    43  		}
    44  		return false
    45  	})
    46  	return res
    47  }
    48  
    49  func TestNode(t *testing.T) {
    50  	n := &node{f: plugin("a")}
    51  	require.Error(t, n.addNode("", plugin("α")))
    52  	if err := n.addNode("gitlab.stageoffice.ru", plugin("b")); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if err := n.addNode("gitlab.com", plugin("c")); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if err := n.addNode("gitlab.stageoffice.ru/UCS-COMMON/schema", plugin("d")); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	require.Error(t, n.addNode("gitlab.stageoffice.ru/UCS-COMMON/schema", plugin("δ")))
    62  	if err := n.addNode("gitlab.stageoffice.ru/UCS-CADDY-PLUGINS", plugin("e")); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	lists := nodeLists(n)
    67  	require.Equal(t, [][]string{
    68  		nil,
    69  		{"gitlab.", "com"},
    70  		{"gitlab.", "stageoffice.ru"},
    71  		{"gitlab.", "stageoffice.ru", "/UCS-C", "ADDY-PLUGINS"},
    72  		{"gitlab.", "stageoffice.ru", "/UCS-C", "OMMON/schema"},
    73  	}, lists)
    74  
    75  	// get tests
    76  	tests := []struct {
    77  		name     string
    78  		url      string
    79  		expected string
    80  	}{
    81  		{
    82  			name:     "trivial",
    83  			url:      "",
    84  			expected: "a",
    85  		},
    86  		{
    87  			name:     "full-mismatch",
    88  			url:      "github.com/sirkon/goproxy",
    89  			expected: "a",
    90  		},
    91  		{
    92  			name:     "to-the-gitlab-com",
    93  			url:      "gitlab.com/repo/project",
    94  			expected: "c",
    95  		},
    96  		{
    97  			name:     "stageoffice-generic",
    98  			url:      "gitlab.stageoffice.ru/UCS-PLATFORM/marker",
    99  			expected: "b",
   100  		},
   101  		{
   102  			name:     "stageoffice-schema",
   103  			url:      "gitlab.stageoffice.ru/UCS-COMMON/schema/marker",
   104  			expected: "d",
   105  		},
   106  		{
   107  			name:     "stageoffice-caddy-plugins",
   108  			url:      "gitlab.stageoffice.ru/UCS-CADDY-PLUGINS/algol",
   109  			expected: "e",
   110  		},
   111  		{
   112  			name:     "match-rollback",
   113  			url:      "gitlab.org",
   114  			expected: "a",
   115  		},
   116  	}
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			res := n.getNode(tt.url)
   120  			if string(res.(plugin)) != tt.expected {
   121  				t.Errorf("plugin %v expected for url %s, got %v", plugin(tt.expected), tt.url, res)
   122  			}
   123  		})
   124  	}
   125  
   126  	if err := n.addNode("gitlab.stageoffice.ru/UCS-PLATFORM/marker", plugin("f")); err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	if err := n.addNode("gitlab.stageoffice.ru/UCS-PLATFORM", plugin("g")); err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	lists = nodeLists(n)
   133  	require.Equal(t, [][]string{
   134  		nil,
   135  		{"gitlab.", "com"},
   136  		{"gitlab.", "stageoffice.ru"},
   137  		{"gitlab.", "stageoffice.ru", "/UCS-", "C", "ADDY-PLUGINS"},
   138  		{"gitlab.", "stageoffice.ru", "/UCS-", "C", "OMMON/schema"},
   139  		{"gitlab.", "stageoffice.ru", "/UCS-", "PLATFORM"},
   140  		{"gitlab.", "stageoffice.ru", "/UCS-", "PLATFORM", "/marker"},
   141  	}, lists)
   142  
   143  	if err := n.addNode("somehost.com", plugin("y")); err != nil {
   144  		t.Fatal(err)
   145  	}
   146  	lists = nodeLists(n)
   147  	require.Equal(t, [][]string{
   148  		nil,
   149  		{"gitlab.", "com"},
   150  		{"gitlab.", "stageoffice.ru"},
   151  		{"gitlab.", "stageoffice.ru", "/UCS-", "C", "ADDY-PLUGINS"},
   152  		{"gitlab.", "stageoffice.ru", "/UCS-", "C", "OMMON/schema"},
   153  		{"gitlab.", "stageoffice.ru", "/UCS-", "PLATFORM"},
   154  		{"gitlab.", "stageoffice.ru", "/UCS-", "PLATFORM", "/marker"},
   155  		{"somehost.com"},
   156  	}, lists)
   157  
   158  	if err := n.addNode("gitlab.stageoffice.ru/UCS-C", plugin("z")); err != nil {
   159  		t.Fatal(err)
   160  	}
   161  	lists = nodeLists(n)
   162  	require.Equal(t, [][]string{
   163  		nil,
   164  		{"gitlab.", "com"},
   165  		{"gitlab.", "stageoffice.ru"},
   166  		{"gitlab.", "stageoffice.ru", "/UCS-", "C"},
   167  		{"gitlab.", "stageoffice.ru", "/UCS-", "C", "ADDY-PLUGINS"},
   168  		{"gitlab.", "stageoffice.ru", "/UCS-", "C", "OMMON/schema"},
   169  		{"gitlab.", "stageoffice.ru", "/UCS-", "PLATFORM"},
   170  		{"gitlab.", "stageoffice.ru", "/UCS-", "PLATFORM", "/marker"},
   171  		{"somehost.com"},
   172  	}, lists)
   173  }