2020-03-14 14:24:17 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-03-14 23:27:54 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-03-14 14:24:17 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-03-14 23:27:54 +00:00
|
|
|
|
2020-03-16 08:20:00 +00:00
|
|
|
"git.xdrm.io/go/aicra/datatype/builtin"
|
2020-03-14 14:24:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLegalServiceName(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-14 14:24:17 +00:00
|
|
|
tests := []struct {
|
|
|
|
Raw string
|
|
|
|
Error error
|
|
|
|
}{
|
|
|
|
// empty
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPattern,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "no-starting-slash" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPattern,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "ending-slash/" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPattern,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/" } ]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/valid-name" } ]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/valid/nested/name" } ]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}a" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errUndefinedBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}/abc" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}s/abc" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/abc" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errUndefinedBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{b{races}s/abc" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/}abc" } ]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("service.%d", i), func(t *testing.T) {
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
err := srv.Parse(strings.NewReader(test.Raw))
|
2020-03-14 14:24:17 +00:00
|
|
|
|
|
|
|
if err == nil && test.Error != nil {
|
|
|
|
t.Errorf("expected an error: '%s'", test.Error.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if err != nil && test.Error == nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && test.Error != nil {
|
|
|
|
if !errors.Is(err, test.Error) {
|
|
|
|
t.Errorf("expected the error '%s' (got '%s')", test.Error.Error(), err.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestAvailableMethods(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-03-14 14:24:17 +00:00
|
|
|
tests := []struct {
|
|
|
|
Raw string
|
|
|
|
ValidMethod bool
|
|
|
|
}{
|
|
|
|
{ // missing description
|
|
|
|
`[ { "method": "GET", "path": "/", "info": "valid-description" }]`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{ // missing description
|
|
|
|
`[ { "method": "POST", "path": "/", "info": "valid-description" }]`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{ // empty description
|
|
|
|
`[ { "method": "PUT", "path": "/", "info": "valid-description" }]`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{ // empty trimmed description
|
|
|
|
`[ { "method": "DELETE", "path": "/", "info": "valid-description" }]`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{ // valid description
|
|
|
|
`[ { "method": "get", "path": "/", "info": "valid-description" }]`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // valid description
|
|
|
|
`[ { "method": "UNknOwN", "path": "/", "info": "valid-description" }]`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("service.%d", i), func(t *testing.T) {
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
err := srv.Parse(strings.NewReader(test.Raw))
|
2020-03-14 14:24:17 +00:00
|
|
|
|
|
|
|
if test.ValidMethod && err != nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:34:20 +00:00
|
|
|
if !test.ValidMethod && !errors.Is(err, errUnknownMethod) {
|
|
|
|
t.Errorf("expected error <%s> got <%s>", errUnknownMethod, err)
|
2020-03-14 14:24:17 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestParseEmpty(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-04-04 09:45:49 +00:00
|
|
|
r := strings.NewReader(`[]`)
|
|
|
|
srv := &Server{}
|
|
|
|
err := srv.Parse(r)
|
2020-03-14 14:24:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error (got '%s')", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestParseJsonError(t *testing.T) {
|
2020-04-04 09:45:49 +00:00
|
|
|
r := strings.NewReader(`{
|
2020-03-14 14:24:17 +00:00
|
|
|
"GET": {
|
|
|
|
"info": "info
|
|
|
|
},
|
|
|
|
}`) // trailing ',' is invalid JSON
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
err := srv.Parse(r)
|
2020-03-14 14:24:17 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseMissingMethodDescription(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-03-14 14:24:17 +00:00
|
|
|
tests := []struct {
|
|
|
|
Raw string
|
|
|
|
ValidDescription bool
|
|
|
|
}{
|
|
|
|
{ // missing description
|
|
|
|
`[ { "method": "GET", "path": "/" }]`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // missing description
|
|
|
|
`[ { "method": "GET", "path": "/subservice" }]`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // empty description
|
|
|
|
`[ { "method": "GET", "path": "/", "info": "" }]`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // empty trimmed description
|
|
|
|
`[ { "method": "GET", "path": "/", "info": " " }]`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // valid description
|
|
|
|
`[ { "method": "GET", "path": "/", "info": "a" }]`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{ // valid description
|
|
|
|
`[ { "method": "GET", "path": "/", "info": "some description" }]`,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) {
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
err := srv.Parse(strings.NewReader(test.Raw))
|
2020-03-14 14:24:17 +00:00
|
|
|
|
|
|
|
if test.ValidDescription && err != nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:34:20 +00:00
|
|
|
if !test.ValidDescription && !errors.Is(err, errMissingDescription) {
|
|
|
|
t.Errorf("expected error <%s> got <%s>", errMissingDescription, err)
|
2020-03-14 14:24:17 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamEmptyRenameNoRename(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-04-04 09:45:49 +00:00
|
|
|
r := strings.NewReader(`[
|
2020-03-14 14:24:17 +00:00
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "valid-description",
|
|
|
|
"in": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"original": { "info": "valid-desc", "type": "any", "name": "" }
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`)
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
srv.Types = append(srv.Types, builtin.AnyDataType{})
|
|
|
|
err := srv.Parse(r)
|
2020-03-14 14:24:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2020-03-16 08:01:51 +00:00
|
|
|
if len(srv.Services) < 1 {
|
2020-03-14 14:24:17 +00:00
|
|
|
t.Errorf("expected a service")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2020-03-16 08:01:51 +00:00
|
|
|
for _, param := range srv.Services[0].Input {
|
2020-03-14 14:24:17 +00:00
|
|
|
if param.Rename != "original" {
|
|
|
|
t.Errorf("expected the parameter 'original' not to be renamed to '%s'", param.Rename)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestOptionalParam(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-04-04 09:45:49 +00:00
|
|
|
r := strings.NewReader(`[
|
2020-03-14 14:24:17 +00:00
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "valid-description",
|
|
|
|
"in": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"optional": { "info": "optional-type", "type": "?bool" },
|
|
|
|
"required": { "info": "required-type", "type": "bool" },
|
|
|
|
"required2": { "info": "required", "type": "any" },
|
|
|
|
"optional2": { "info": "optional", "type": "?any" }
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`)
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
srv.Types = append(srv.Types, builtin.AnyDataType{})
|
|
|
|
srv.Types = append(srv.Types, builtin.BoolDataType{})
|
|
|
|
err := srv.Parse(r)
|
2020-03-14 14:24:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2020-03-16 08:01:51 +00:00
|
|
|
if len(srv.Services) < 1 {
|
2020-03-14 14:24:17 +00:00
|
|
|
t.Errorf("expected a service")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2020-03-16 08:01:51 +00:00
|
|
|
for pName, param := range srv.Services[0].Input {
|
2020-03-14 14:24:17 +00:00
|
|
|
|
|
|
|
if pName == "optional" || pName == "optional2" {
|
|
|
|
if !param.Optional {
|
|
|
|
t.Errorf("expected parameter '%s' to be optional", pName)
|
|
|
|
t.Failed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pName == "required" || pName == "required2" {
|
|
|
|
if param.Optional {
|
|
|
|
t.Errorf("expected parameter '%s' to be required", pName)
|
|
|
|
t.Failed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestParseParameters(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-03-14 14:24:17 +00:00
|
|
|
tests := []struct {
|
|
|
|
Raw string
|
|
|
|
Error error
|
|
|
|
}{
|
|
|
|
{ // invalid param name prefix
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"_param1": { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errMissingParamDesc,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{ // invalid param name suffix
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1_": { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errMissingParamDesc,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{ // missing param description
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errMissingParamDesc,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{ // empty param description
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { "info": "" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errMissingParamDesc,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{ // missing param type
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { "info": "valid" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errMissingParamType,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{ // empty param type
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { "info": "valid", "type": "" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errMissingParamType,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{ // invalid type (optional mark only)
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { "info": "valid", "type": "?" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
|
2020-04-04 12:34:20 +00:00
|
|
|
errMissingParamType,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{ // valid description + valid type
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"param1": { "info": "valid", "type": "any" }
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{ // valid description + valid OPTIONAL type
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"param1": { "info": "valid", "type": "?any" }
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
{ // name conflict with rename
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"param1": { "info": "valid", "type": "any" },
|
|
|
|
"param2": { "info": "valid", "type": "any", "name": "param1" }
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
// 2 possible errors as map order is not deterministic
|
2020-04-04 12:34:20 +00:00
|
|
|
errParamNameConflict,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{ // rename conflict with name
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"param1": { "info": "valid", "type": "any", "name": "param2" },
|
|
|
|
"param2": { "info": "valid", "type": "any" }
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
// 2 possible errors as map order is not deterministic
|
2020-04-04 12:34:20 +00:00
|
|
|
errParamNameConflict,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{ // rename conflict with rename
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"param1": { "info": "valid", "type": "any", "name": "conflict" },
|
|
|
|
"param2": { "info": "valid", "type": "any", "name": "conflict" }
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
// 2 possible errors as map order is not deterministic
|
2020-04-04 12:34:20 +00:00
|
|
|
errParamNameConflict,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{ // both renamed with no conflict
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"param1": { "info": "valid", "type": "any", "name": "freename" },
|
|
|
|
"param2": { "info": "valid", "type": "any", "name": "freename2" }
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
2020-03-29 12:18:05 +00:00
|
|
|
// missing rename
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/{uri}",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"{uri}": { "info": "valid", "type": "any" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errMandatoryRename,
|
2020-03-29 12:18:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"GET@abc": { "info": "valid", "type": "any" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errMandatoryRename,
|
2020-03-29 12:18:05 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"GET@abc": { "info": "valid", "type": "any", "name": "abc" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
2020-03-16 09:56:26 +00:00
|
|
|
|
|
|
|
{ // URI parameter
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/{uri}",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"{uri}": { "info": "valid", "type": "any", "name": "freename" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{ // URI parameter cannot be optional
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/{uri}",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"{uri}": { "info": "valid", "type": "?any", "name": "freename" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errIllegalOptionalURIParam,
|
2020-03-16 09:56:26 +00:00
|
|
|
},
|
|
|
|
{ // URI parameter not specified
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"{uri}": { "info": "valid", "type": "?any", "name": "freename" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errUnspecifiedBraceCapture,
|
2020-03-16 09:56:26 +00:00
|
|
|
},
|
|
|
|
{ // URI parameter not defined
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/{uri}",
|
|
|
|
"info": "info",
|
|
|
|
"in": { }
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errUndefinedBraceCapture,
|
2020-03-16 09:56:26 +00:00
|
|
|
},
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) {
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
srv.Types = append(srv.Types, builtin.AnyDataType{})
|
|
|
|
err := srv.Parse(strings.NewReader(test.Raw))
|
2020-03-14 14:24:17 +00:00
|
|
|
|
|
|
|
if err == nil && test.Error != nil {
|
|
|
|
t.Errorf("expected an error: '%s'", test.Error.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if err != nil && test.Error == nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && test.Error != nil {
|
|
|
|
if !errors.Is(err, test.Error) {
|
|
|
|
t.Errorf("expected the error <%s> got <%s>", test.Error, err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-21 14:49:07 +00:00
|
|
|
func TestServiceCollision(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
Config string
|
|
|
|
Error error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/b",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errPatternCollision,
|
2020-03-21 14:49:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/b",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/b",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/b",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "string", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errPatternCollision,
|
2020-03-21 14:49:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/b",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/b/d",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}/d",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "string", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errPatternCollision,
|
2020-03-21 14:49:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/123",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "string", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errPatternCollision,
|
2020-03-21 14:49:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/123/d",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "string", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/123",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}/d",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "string", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/123",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errPatternCollision,
|
2020-03-21 14:49:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/123/d",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/123",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}/d",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/123/d",
|
|
|
|
"info": "info", "in": {}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}/d",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errPatternCollision,
|
2020-03-21 14:49:07 +00:00
|
|
|
},
|
2020-03-21 14:52:07 +00:00
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/{b}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{b}": { "info":"info", "type": "uint", "name": "b" }
|
2020-03-21 14:52:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{ "method": "GET", "path": "/a/{c}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
2020-03-21 14:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-04-04 12:34:20 +00:00
|
|
|
errPatternCollision,
|
2020-03-21 14:52:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/{b}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{b}": { "info":"info", "type": "uint", "name": "b" }
|
2020-03-21 14:52:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{ "method": "PUT", "path": "/a/{c}",
|
|
|
|
"info": "info", "in": {
|
2020-03-29 12:18:05 +00:00
|
|
|
"{c}": { "info":"info", "type": "uint", "name": "c" }
|
2020-03-21 14:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
nil, // different methods
|
|
|
|
},
|
2020-03-21 14:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) {
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
srv.Types = append(srv.Types, builtin.StringDataType{})
|
|
|
|
srv.Types = append(srv.Types, builtin.UintDataType{})
|
|
|
|
err := srv.Parse(strings.NewReader(test.Config))
|
2020-03-21 14:49:07 +00:00
|
|
|
|
|
|
|
if err == nil && test.Error != nil {
|
|
|
|
t.Errorf("expected an error: '%s'", test.Error.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if err != nil && test.Error == nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && test.Error != nil {
|
|
|
|
if !errors.Is(err, test.Error) {
|
|
|
|
t.Errorf("expected the error <%s> got <%s>", test.Error, err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 23:27:54 +00:00
|
|
|
func TestMatchSimple(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-03-14 23:27:54 +00:00
|
|
|
tests := []struct {
|
|
|
|
Config string
|
|
|
|
URL string
|
|
|
|
Match bool
|
|
|
|
}{
|
|
|
|
{ // false positive -1
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a",
|
|
|
|
"info": "info",
|
|
|
|
"in": {}
|
|
|
|
} ]`,
|
|
|
|
"/",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // false positive +1
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {}
|
|
|
|
} ]`,
|
|
|
|
"/a",
|
|
|
|
false,
|
|
|
|
},
|
2020-04-04 13:39:00 +00:00
|
|
|
{ // root url
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a",
|
|
|
|
"info": "info",
|
|
|
|
"in": {}
|
|
|
|
} ]`,
|
|
|
|
"/",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a",
|
|
|
|
"info": "info",
|
|
|
|
"in": {}
|
|
|
|
} ]`,
|
|
|
|
"/",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {}
|
|
|
|
} ]`,
|
|
|
|
"/",
|
|
|
|
true,
|
|
|
|
},
|
2020-03-14 23:27:54 +00:00
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a",
|
|
|
|
"info": "info",
|
|
|
|
"in": {}
|
|
|
|
} ]`,
|
|
|
|
"/a",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a",
|
|
|
|
"info": "info",
|
|
|
|
"in": {}
|
|
|
|
} ]`,
|
|
|
|
"/a/",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a/{id}",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"{id}": {
|
|
|
|
"info": "info",
|
2020-03-29 12:18:05 +00:00
|
|
|
"type": "bool",
|
|
|
|
"name": "id"
|
2020-03-14 23:27:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} ]`,
|
|
|
|
"/a/12/",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a/{id}",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"{id}": {
|
|
|
|
"info": "info",
|
2020-03-29 12:18:05 +00:00
|
|
|
"type": "int",
|
|
|
|
"name": "id"
|
2020-03-14 23:27:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} ]`,
|
|
|
|
"/a/12/",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a/{valid}",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
2020-03-15 00:37:28 +00:00
|
|
|
"{valid}": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"info": "info",
|
2020-03-29 12:18:05 +00:00
|
|
|
"type": "bool",
|
|
|
|
"name": "valid"
|
2020-03-14 23:27:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} ]`,
|
|
|
|
"/a/12/",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/a/{valid}",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
2020-03-15 00:37:28 +00:00
|
|
|
"{valid}": {
|
2020-03-14 23:27:54 +00:00
|
|
|
"info": "info",
|
2020-03-29 12:18:05 +00:00
|
|
|
"type": "bool",
|
|
|
|
"name": "valid"
|
2020-03-14 23:27:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} ]`,
|
|
|
|
"/a/true/",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) {
|
2020-04-04 09:45:49 +00:00
|
|
|
srv := &Server{}
|
|
|
|
srv.Types = append(srv.Types, builtin.AnyDataType{})
|
|
|
|
srv.Types = append(srv.Types, builtin.IntDataType{})
|
|
|
|
srv.Types = append(srv.Types, builtin.BoolDataType{})
|
|
|
|
err := srv.Parse(strings.NewReader(test.Config))
|
2020-03-14 23:27:54 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2020-03-16 08:01:51 +00:00
|
|
|
if len(srv.Services) != 1 {
|
|
|
|
t.Errorf("expected to have 1 service, got %d", len(srv.Services))
|
2020-03-14 23:27:54 +00:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, test.URL, nil)
|
|
|
|
|
2020-03-16 08:01:51 +00:00
|
|
|
match := srv.Services[0].Match(req)
|
2020-03-14 23:27:54 +00:00
|
|
|
if test.Match && !match {
|
|
|
|
t.Errorf("expected '%s' to match", test.URL)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if !test.Match && match {
|
|
|
|
t.Errorf("expected '%s' NOT to match", test.URL)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-04 13:39:00 +00:00
|
|
|
|
|
|
|
func TestFindPriority(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
Config string
|
|
|
|
URL string
|
|
|
|
MatchingDesc string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a", "info": "s1" },
|
|
|
|
{ "method": "GET", "path": "/", "info": "s2" }
|
|
|
|
]`,
|
|
|
|
"/",
|
|
|
|
"s2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/", "info": "s2" },
|
|
|
|
{ "method": "GET", "path": "/a", "info": "s1" }
|
|
|
|
]`,
|
|
|
|
"/",
|
|
|
|
"s2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a", "info": "s1" },
|
|
|
|
{ "method": "GET", "path": "/", "info": "s2" }
|
|
|
|
]`,
|
|
|
|
"/a",
|
|
|
|
"s1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/b/c", "info": "s1" },
|
|
|
|
{ "method": "GET", "path": "/a/b", "info": "s2" }
|
|
|
|
]`,
|
|
|
|
"/a/b/c",
|
|
|
|
"s1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "method": "GET", "path": "/a/b/c", "info": "s1" },
|
|
|
|
{ "method": "GET", "path": "/a/b", "info": "s2" }
|
|
|
|
]`,
|
|
|
|
"/a/b/",
|
|
|
|
"s2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) {
|
|
|
|
srv := &Server{}
|
|
|
|
srv.Types = append(srv.Types, builtin.AnyDataType{})
|
|
|
|
srv.Types = append(srv.Types, builtin.IntDataType{})
|
|
|
|
srv.Types = append(srv.Types, builtin.BoolDataType{})
|
|
|
|
err := srv.Parse(strings.NewReader(test.Config))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, test.URL, nil)
|
|
|
|
service := srv.Find(req)
|
|
|
|
if service == nil {
|
|
|
|
t.Errorf("expected to find a service")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if service.Description != test.MatchingDesc {
|
|
|
|
t.Errorf("expected description '%s', got '%s'", test.MatchingDesc, service.Description)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|