You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
kratos/cmd/protoc-gen-go-http/http_test.go

54 lines
1.4 KiB

package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNoParameters(t *testing.T) {
path := "/test/noparams"
m := buildPathVars(path)
assert.Emptyf(t, m, "Map should be empty")
}
func TestSingleParam(t *testing.T) {
path := "/test/{message.id}"
m := buildPathVars(path)
assert.Len(t, m, 1)
assert.Empty(t, m["message.id"])
}
func TestTwoParametersReplacement(t *testing.T) {
path := "/test/{message.id}/{message.name=messages/*}"
m := buildPathVars(path)
assert.Len(t, m, 2)
assert.Empty(t, m["message.id"])
assert.NotEmpty(t, m["message.name"])
assert.Equal(t, *m["message.name"], "messages/*")
}
func TestNoReplacePath(t *testing.T) {
path := "/test/{message.id}"
assert.Equal(t, path, replacePath("message.id", "", path))
path = "/test/{message.id=test}"
assert.Equal(t, "/test/{message.id:test}", replacePath("message.id", "test", path))
}
func TestReplacePath(t *testing.T) {
path := "/test/{message.id}/{message.name=messages/*}"
newPath := replacePath("message.name", "messages/*", path)
assert.Equal(t, "/test/{message.id}/{message.name:messages/.*}", newPath)
}
func TestIteration(t *testing.T) {
path := "/test/{message.id}/{message.name=messages/*}"
vars := buildPathVars(path)
for v, s := range vars {
if s != nil {
path = replacePath(v, *s, path)
}
}
assert.Equal(t, "/test/{message.id}/{message.name:messages/.*}", path)
}