From 0327f30e3cbcdec47f048be609e019bb84c356c8 Mon Sep 17 00:00:00 2001 From: Dominic Wong Date: Wed, 10 Jun 2020 11:18:03 +0100 Subject: [PATCH] Fix regex detection. Fixes #1663 (#1696) --- api/router/registry/registry.go | 2 +- api/router/registry/registry_test.go | 34 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 api/router/registry/registry_test.go diff --git a/api/router/registry/registry.go b/api/router/registry/registry.go index 478a2e43..cdd3de22 100644 --- a/api/router/registry/registry.go +++ b/api/router/registry/registry.go @@ -188,7 +188,7 @@ func (r *registryRouter) store(services []*registry.Service) { for _, p := range ep.Endpoint.Path { var pcreok bool - if p[0] == '^' && p[len(p)-1] != '$' { + if p[0] == '^' && p[len(p)-1] == '$' { pcrereg, err := regexp.CompilePOSIX(p) if err == nil { cep.pcreregs = append(cep.pcreregs, pcrereg) diff --git a/api/router/registry/registry_test.go b/api/router/registry/registry_test.go new file mode 100644 index 00000000..5d481386 --- /dev/null +++ b/api/router/registry/registry_test.go @@ -0,0 +1,34 @@ +package registry + +import ( + "testing" + + "github.com/micro/go-micro/v2/registry" + "github.com/stretchr/testify/assert" +) + +func TestStoreRegex(t *testing.T) { + router := newRouter() + router.store([]*registry.Service{ + { + Name: "Foobar", + Version: "latest", + Endpoints: []*registry.Endpoint{ + { + Name: "foo", + Metadata: map[string]string{ + "endpoint": "FooEndpoint", + "description": "Some description", + "method": "POST", + "path": "^/foo/$", + "handler": "rpc", + }, + }, + }, + Metadata: map[string]string{}, + }, + }, + ) + + assert.Len(t, router.ceps["Foobar.foo"].pcreregs, 1) +}