Skip to content

Commit

Permalink
fix(rpc): disallow duplicated rpc path (#405)
Browse files Browse the repository at this point in the history
Co-authored-by: Jian Zeng <[email protected]>
  • Loading branch information
caicloud-bot and Jian Zeng authored Dec 1, 2020
1 parent 75f3156 commit 8aa6ef5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion service/rpc/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func (b *builder) AddDescriptor(descriptors ...interface{}) error {
path = "/"
}
for _, action := range descriptor.Actions {
b.bindings[genRPCPath(path, action.Version, action.Name)] = &binding{
rpcPath := genRPCPath(path, action.Version, action.Name)
if _, ok := b.bindings[rpcPath]; ok {
return fmt.Errorf("duplicated rpc path: %s", rpcPath)
}
b.bindings[rpcPath] = &binding{
middlewares: descriptor.Middlewares,
definition: b.genDefinition(action, descriptor.Consumes, descriptor.Produces, descriptor.Tags),
}
Expand Down
27 changes: 27 additions & 0 deletions service/rpc/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,33 @@ func TestDefinitions(t *testing.T) {
}
}

func TestDuplicatedRPCPath(t *testing.T) {
const version = "2020-10-10"
action := definition.RPCAction{
Name: "GetFoo",
Version: version,
Function: func() (string, error) {
return "", nil
},
Results: definition.DataErrorResults(""),
}
desc := definition.RPCDescriptor{
Path: "/",
Description: "Test",
Consumes: []string{definition.MIMEAll},
Produces: []string{definition.MIMEAll},
Actions: []definition.RPCAction{action, action},
}
builder := NewBuilder()
err := builder.AddDescriptor(desc)
if err == nil {
t.Fatal("Unexpected success")
}
if !strings.Contains(err.Error(), "duplicated rpc path") {
t.Fatalf("Unexpected error: %v", err)
}
}

func BenchmarkServer(b *testing.B) {
u, _ := url.Parse("/?Action=GetEcho&Version=2020-01-01&name=alice")

Expand Down

0 comments on commit 8aa6ef5

Please sign in to comment.