github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/tests/http_test.go (about)

     1  // Package test provides tests for common low-level types and utilities for all aistore projects
     2  /*
     3   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package tests_test
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/NVIDIA/aistore/cmn"
    12  )
    13  
    14  func TestMatchRESTItems(t *testing.T) {
    15  	tests := []struct {
    16  		name string
    17  
    18  		path       string
    19  		itemsAfter int
    20  		splitAfter bool
    21  		items      []string
    22  
    23  		expectedItems []string
    24  		expectedErr   bool
    25  	}{
    26  		{
    27  			name: "smoke",
    28  			path: "/some/path/to/url", itemsAfter: 2, splitAfter: true, items: []string{"some", "path"},
    29  			expectedItems: []string{"to", "url"},
    30  		},
    31  		{
    32  			name: "minimal",
    33  			path: "/some/path", itemsAfter: 0, splitAfter: false, items: []string{"some", "path"},
    34  			expectedItems: []string{},
    35  		},
    36  		{
    37  			name: "with_empty",
    38  			path: "/some/path/", itemsAfter: 0, splitAfter: false, items: []string{"some", "path"},
    39  			expectedItems: []string{},
    40  		},
    41  		{
    42  			name: "dont_split_after",
    43  			path: "/some/path/to/url", itemsAfter: 2, splitAfter: true, items: []string{"some", "path"},
    44  			expectedItems: []string{"to", "url"},
    45  		},
    46  		{
    47  			name: "more_items_after",
    48  			path: "/some/path/to/url/more", itemsAfter: 2, splitAfter: true, items: []string{"some", "path"},
    49  			expectedItems: []string{"to", "url", "more"},
    50  		},
    51  		{
    52  			name: "more_items_after_without_split",
    53  			path: "/some/path/to/url/more", itemsAfter: 2, splitAfter: false, items: []string{"some", "path"},
    54  			expectedItems: []string{"to", "url/more"},
    55  		},
    56  		{
    57  			name: "invalid_path",
    58  			path: "/some/to/url/path", itemsAfter: 2, splitAfter: true, items: []string{"some", "path"},
    59  			expectedErr: true,
    60  		},
    61  		{
    62  			name: "too_short",
    63  			path: "/some/to/url/path", itemsAfter: 3, splitAfter: false, items: []string{"some", "to"},
    64  			expectedErr: true,
    65  		},
    66  		{
    67  			name: "too_long",
    68  			path: "/some/path/url", itemsAfter: 0, splitAfter: false, items: []string{"some", "path"},
    69  			expectedErr: true,
    70  		},
    71  	}
    72  
    73  	for _, test := range tests {
    74  		apiItems, err := cmn.ParseURL(test.path, test.items, test.itemsAfter, test.splitAfter)
    75  		if err != nil && !test.expectedErr {
    76  			t.Fatalf("test: %s, err: %v", test.name, err)
    77  		} else if err == nil && test.expectedErr {
    78  			t.Fatalf("test: %s, expected error", test.name)
    79  		} else if err != nil && test.expectedErr {
    80  			continue
    81  		}
    82  		if !reflect.DeepEqual(apiItems, test.expectedItems) {
    83  			t.Fatalf("test: %s, items are not equal (got: %v, expected: %v)", test.name, apiItems, test.expectedItems)
    84  		}
    85  	}
    86  }