-
Notifications
You must be signed in to change notification settings - Fork 15
/
replacer_test.go
103 lines (85 loc) · 3.64 KB
/
replacer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package caddynats
import (
"bytes"
"net/http"
"reflect"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/nats-io/nats.go"
)
// Returns a basic request for testing
func reqPath(path string) *http.Request {
req, _ := http.NewRequest("GET", "http://localhost"+path, &bytes.Buffer{})
return req
}
func TestAddNatsPublishVarsToReplacer(t *testing.T) {
type test struct {
req *http.Request
input string
want string
}
tests := []test{
// Basic subject mapping
{req: reqPath("/foo/bar"), input: "{nats.subject}", want: "foo.bar"},
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject}", want: "foo.bar.bat.baz"},
{req: reqPath("/foo/bar/bat/baz?query=true"), input: "{nats.subject}", want: "foo.bar.bat.baz"},
{req: reqPath("/foo/bar"), input: "prefix.{nats.subject}.suffix", want: "prefix.foo.bar.suffix"},
// Segment placeholders
{req: reqPath("/foo/bar"), input: "{nats.subject.0}", want: "foo"},
{req: reqPath("/foo/bar"), input: "{nats.subject.1}", want: "bar"},
// Segment Ranges
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.0:}", want: "foo.bar.bat.baz"},
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.1:}", want: "bar.bat.baz"},
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.2:}", want: "bat.baz"},
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.1:3}", want: "bar.bat"},
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.0:3}", want: "foo.bar.bat"},
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.0:4}", want: "foo.bar.bat.baz"},
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.:3}", want: "foo.bar.bat"},
// Out of bounds ranges
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.0:18}", want: ""},
{req: reqPath("/foo/bar/bat/baz"), input: "{nats.subject.-1:}", want: ""},
}
for _, tc := range tests {
repl := caddy.NewReplacer()
addNATSPublishVarsToReplacer(repl, tc.req)
got := repl.ReplaceAll(tc.input, "")
if !reflect.DeepEqual(tc.want, got) {
t.Errorf("expected: %v, got: %v", tc.want, got)
}
}
}
func TestAddNatsSubscribeVarsToReplacer(t *testing.T) {
type test struct {
msg *nats.Msg
input string
want string
}
tests := []test{
// Basic subject mapping
{msg: nats.NewMsg("foo.bar"), input: "{nats.path}", want: "foo/bar"},
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path}", want: "foo/bar/bat/baz"},
{msg: nats.NewMsg("foo.bar"), input: "prefix/{nats.path}/suffix", want: "prefix/foo/bar/suffix"},
// // Segment placeholders
{msg: nats.NewMsg("foo.bar"), input: "{nats.path.0}", want: "foo"},
{msg: nats.NewMsg("foo.bar"), input: "{nats.path.1}", want: "bar"},
// // Segment Ranges
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.0:}", want: "foo/bar/bat/baz"},
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.1:}", want: "bar/bat/baz"},
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.2:}", want: "bat/baz"},
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.1:3}", want: "bar/bat"},
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.0:3}", want: "foo/bar/bat"},
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.:3}", want: "foo/bar/bat"},
// Out of bounds ranges
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.0:18}", want: ""},
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.:18}", want: ""},
{msg: nats.NewMsg("foo.bar.bat.baz"), input: "{nats.path.-1:}", want: ""},
}
for _, tc := range tests {
repl := caddy.NewReplacer()
addNatsSubscribeVarsToReplacer(repl, tc.msg)
got := repl.ReplaceAll(tc.input, "")
if !reflect.DeepEqual(tc.want, got) {
t.Errorf("expected: %v, got: %v", tc.want, got)
}
}
}