Skip to content

Commit

Permalink
add error test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianyi Wang committed Nov 14, 2024
1 parent 0b93898 commit 668e56a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 0 additions & 1 deletion encoding/httpbinding/path_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func replacePathElement(path, fieldBuf []byte, key, val string, escape bool) ([]

start = bytes.Index(path, fieldBuf)
if start < 0 {
// TODO what to do about error?
return path, fieldBuf, fmt.Errorf("invalid path index, start=%d. %s", start, path)
}
encodeSep = false
Expand Down
17 changes: 16 additions & 1 deletion encoding/httpbinding/path_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func TestPathReplace(t *testing.T) {
cases := []struct {
Orig, ExpPath, ExpRawPath []byte
Key, Val string
ExpectErr bool
}{
{
Orig: []byte("/{bucket}/{key+}"),
Expand Down Expand Up @@ -52,6 +53,11 @@ func TestPathReplace(t *testing.T) {
ExpRawPath: []byte("/value/{namespace}"),
Key: "name", Val: "value",
},
{
Orig: []byte("/{namespace}/{name+}"),
Key: "nam", Val: "value",
ExpectErr: true,
},
}

var buffer [64]byte
Expand All @@ -62,8 +68,17 @@ func TestPathReplace(t *testing.T) {

path, _, err := replacePathElement(c.Orig, buffer[:0], c.Key, c.Val, false)
if err != nil {
t.Fatalf("expected no error, got %v", err)
if !c.ExpectErr {
t.Fatalf("expected no error, got %v", err)
}
} else if c.ExpectErr {
t.Fatalf("expect error, got none")
}

if c.ExpectErr {
return
}

rawPath, _, err := replacePathElement(origRaw, buffer[:0], c.Key, c.Val, true)
if err != nil {
t.Fatalf("expected no error, got %v", err)
Expand Down

0 comments on commit 668e56a

Please sign in to comment.