Skip to content

Commit

Permalink
Merge ac1a098 into 3678d91
Browse files Browse the repository at this point in the history
  • Loading branch information
variadico committed Mar 4, 2021
2 parents 3678d91 + ac1a098 commit c863a62
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
23 changes: 20 additions & 3 deletions js.go
Expand Up @@ -165,10 +165,27 @@ func (opt jsOptFn) configureJSContext(opts *js) error {
// APIPrefix changes the default prefix used for the JetStream API.
func APIPrefix(pre string) JSOpt {
return jsOptFn(func(js *js) error {
js.pre = pre
if !strings.HasSuffix(js.pre, ".") {
js.pre = js.pre + "."
pre = strings.TrimSuffix(pre, ".")

if pre == ">" {
return ErrJetStreamBadPre
}

toks := strings.Split(pre, ".")
for i, tok := range toks {
if tok == "" || tok == "*" {
return ErrJetStreamBadPre
}

if tok == ">" && i < len(toks)-1 {
return ErrBadSubject
} else if tok == ">" && i == len(toks)-1 {
js.pre = pre
return nil
}
}

js.pre = fmt.Sprintf("%s.", pre)
return nil
})
}
Expand Down
58 changes: 58 additions & 0 deletions nats_test.go
Expand Up @@ -2599,3 +2599,61 @@ func TestMsg_RespondMsg(t *testing.T) {
t.Fatalf("did not get correct response: %q", resp.Data)
}
}

func TestJetStreamAPIPrefix(t *testing.T) {
cases := []struct {
prefix string
wantPrefix string
wantErr bool
}{
{
prefix: "foo",
wantPrefix: "foo.",
},
{
prefix: "foo.",
wantPrefix: "foo.",
},
{
prefix: "foo.>",
wantPrefix: "foo.>",
},
{
prefix: "foo.b*r.baz",
wantPrefix: "foo.b*r.baz.",
},
{
prefix: "foo.*",
wantErr: true,
},
{
prefix: "foo.*.bar",
wantErr: true,
},
{
prefix: "foo.>.bar",
wantErr: true,
},
{
prefix: ">",
wantErr: true,
},
}
for _, c := range cases {
t.Run(c.prefix, func(t *testing.T) {
jsOpt := APIPrefix(c.prefix)

js := new(js)
if err := jsOpt.configureJSContext(js); err != nil && !c.wantErr {
t.Fatal(err)
} else if err == nil && c.wantErr {
t.Fatal("unexpected success")
}

if js.pre != c.wantPrefix {
t.Error("unexpected api prefix")
t.Fatalf("got=%s; want=%s", js.pre, c.wantPrefix)
}
})
}
}

0 comments on commit c863a62

Please sign in to comment.