| 1 |
package helpers |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
) |
| 8 |
|
| 9 |
func TestGetRegexForPath(t *testing.T) { |
| 10 |
tests := []struct { |
| 11 |
name string |
| 12 |
tpl string |
| 13 |
wantErr bool |
| 14 |
wantExpr string |
| 15 |
}{ |
| 16 |
{ |
| 17 |
name: "well-formed template with default pattern", |
| 18 |
tpl: "/orders/{id}", |
| 19 |
wantErr: false, |
| 20 |
wantExpr: "^/orders/([^/]*)$", |
| 21 |
}, |
| 22 |
{ |
| 23 |
name: "well-formed template with custom pattern", |
| 24 |
tpl: "/orders/{id:[0-9]+}", |
| 25 |
wantErr: false, |
| 26 |
wantExpr: "^/orders/([0-9]+)$", |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "missing name in template", |
| 30 |
tpl: "/orders/{:pattern}", |
| 31 |
wantErr: true, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "missing pattern in template", |
| 35 |
tpl: "/orders/{name:}", |
| 36 |
wantErr: true, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "unbalanced braces in template", |
| 40 |
tpl: "/orders/{id", |
| 41 |
wantErr: true, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "unbalanced braces in template", |
| 45 |
tpl: "/orders/id}", |
| 46 |
wantErr: true, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "template with multiple variables", |
| 50 |
tpl: "/orders/{id:[0-9]+}/items/{itemId}", |
| 51 |
wantErr: false, |
| 52 |
wantExpr: "^/orders/([0-9]+)/items/([^/]*)$", |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "OData formatted URL with single quotes", |
| 56 |
tpl: "/entities('{id}')", |
| 57 |
wantErr: false, |
| 58 |
wantExpr: "^/entities\\('([^/]*)'\\)$", |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "OData formatted URL with custom pattern", |
| 62 |
tpl: "/entities('{id:[0-9]+}')", |
| 63 |
wantErr: false, |
| 64 |
wantExpr: "^/entities\\('([0-9]+)'\\)$", |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "get default pattern", |
| 68 |
tpl: "/{param}", |
| 69 |
wantErr: false, |
| 70 |
wantExpr: "^/([^/]*)$", |
| 71 |
}, |
| 72 |
} |
| 73 |
|
| 74 |
for _, tt := range tests { |
| 75 |
t.Run(tt.name, func(t *testing.T) { |
| 76 |
got, err := GetRegexForPath(tt.tpl) |
| 77 |
if (err != nil) != tt.wantErr { |
| 78 |
t.Errorf("GetRegexForPath() error = %v, wantErr %v", err, tt.wantErr) |
| 79 |
return |
| 80 |
} |
| 81 |
if !tt.wantErr && got.String() != tt.wantExpr { |
| 82 |
t.Errorf("GetRegexForPath() = %v, want %v", got.String(), tt.wantExpr) |
| 83 |
} |
| 84 |
}) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func TestBraceIndices(t *testing.T) { |
| 89 |
tests := []struct { |
| 90 |
name string |
| 91 |
s string |
| 92 |
want []int |
| 93 |
wantErr bool |
| 94 |
}{ |
| 95 |
{ |
| 96 |
name: "well-formed braces", |
| 97 |
s: "/orders/{id}/items/{itemId}", |
| 98 |
want: []int{8, 12, 19, 27}, |
| 99 |
wantErr: false, |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "unbalanced braces", |
| 103 |
s: "/orders/{id/items/{itemId}", |
| 104 |
wantErr: true, |
| 105 |
}, |
| 106 |
{ |
| 107 |
name: "unbalanced braces", |
| 108 |
s: "/orders/{id}/items/{itemId", |
| 109 |
wantErr: true, |
| 110 |
}, |
| 111 |
{ |
| 112 |
name: "no braces", |
| 113 |
s: "/orders/id/items/itemId", |
| 114 |
want: []int{}, |
| 115 |
wantErr: false, |
| 116 |
}, |
| 117 |
{ |
| 118 |
name: "OData formatted URL with single quotes", |
| 119 |
s: "/entities('{id}')", |
| 120 |
want: []int{11, 15}, |
| 121 |
wantErr: false, |
| 122 |
}, |
| 123 |
} |
| 124 |
|
| 125 |
for _, tt := range tests { |
| 126 |
t.Run(tt.name, func(t *testing.T) { |
| 127 |
got, err := BraceIndices(tt.s) |
| 128 |
if (err != nil) != tt.wantErr { |
| 129 |
t.Errorf("BraceIndices() error = %v, wantErr %v", err, tt.wantErr) |
| 130 |
return |
| 131 |
} |
| 132 |
if !tt.wantErr && !equal(got, tt.want) { |
| 133 |
t.Errorf("BraceIndices() = %v, want %v", got, tt.want) |
| 134 |
} |
| 135 |
}) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func TestDefaultPatternCompileCache(t *testing.T) { |
| 140 |
res, err := GetRegexForPath("{param}") |
| 141 |
|
| 142 |
assert.Nil(t, err) |
| 143 |
assert.Equal(t, res, DefaultPatternRegex) |
| 144 |
assert.Equal(t, res.String(), DefaultPatternRegexString) |
| 145 |
} |
| 146 |
|
| 147 |
func equal(a, b []int) bool { |
| 148 |
if len(a) != len(b) { |
| 149 |
return false |
| 150 |
} |
| 151 |
for i := range a { |
| 152 |
if a[i] != b[i] { |
| 153 |
return false |
| 154 |
} |
| 155 |
} |
| 156 |
return true |
| 157 |
} |