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": "" } ]`,
|
|
|
|
ErrInvalidPattern,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "no-starting-slash" } ]`,
|
|
|
|
ErrInvalidPattern,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "ending-slash/" } ]`,
|
|
|
|
ErrInvalidPattern,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "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-03-15 00:37:28 +00:00
|
|
|
ErrInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}a" } ]`,
|
2020-03-15 00:37:28 +00:00
|
|
|
ErrInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}" } ]`,
|
2020-03-16 09:56:26 +00:00
|
|
|
ErrUndefinedBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/s{braces}/abc" } ]`,
|
2020-03-15 00:37:28 +00:00
|
|
|
ErrInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}s/abc" } ]`,
|
2020-03-15 00:37:28 +00:00
|
|
|
ErrInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/abc" } ]`,
|
2020-03-16 09:56:26 +00:00
|
|
|
ErrUndefinedBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{b{races}s/abc" } ]`,
|
2020-03-15 00:37:28 +00:00
|
|
|
ErrInvalidPatternBraceCapture,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ { "method": "GET", "info": "a", "path": "/invalid/{braces}/}abc" } ]`,
|
2020-03-15 00:37:28 +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) {
|
|
|
|
_, err := Parse(strings.NewReader(test.Raw))
|
|
|
|
|
|
|
|
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) {
|
|
|
|
_, err := Parse(strings.NewReader(test.Raw))
|
|
|
|
|
|
|
|
if test.ValidMethod && err != nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err.Error())
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !test.ValidMethod && !errors.Is(err, ErrUnknownMethod) {
|
|
|
|
t.Errorf("expected error <%s> got <%s>", ErrUnknownMethod, err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestParseEmpty(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-03-14 14:24:17 +00:00
|
|
|
reader := strings.NewReader(`[]`)
|
|
|
|
_, err := Parse(reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error (got '%s')", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestParseJsonError(t *testing.T) {
|
|
|
|
reader := strings.NewReader(`{
|
|
|
|
"GET": {
|
|
|
|
"info": "info
|
|
|
|
},
|
|
|
|
}`) // trailing ',' is invalid JSON
|
|
|
|
_, err := Parse(reader)
|
|
|
|
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) {
|
|
|
|
_, err := Parse(strings.NewReader(test.Raw))
|
|
|
|
|
|
|
|
if test.ValidDescription && err != nil {
|
|
|
|
t.Errorf("unexpected error: '%s'", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !test.ValidDescription && !errors.Is(err, ErrMissingDescription) {
|
|
|
|
t.Errorf("expected error <%s> got <%s>", ErrMissingDescription, err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParamEmptyRenameNoRename(t *testing.T) {
|
2020-03-16 09:56:26 +00:00
|
|
|
t.Parallel()
|
2020-03-14 14:24:17 +00:00
|
|
|
reader := strings.NewReader(`[
|
|
|
|
{
|
|
|
|
"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-03-14 23:27:54 +00:00
|
|
|
srv, err := Parse(reader, builtin.AnyDataType{})
|
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-03-14 14:24:17 +00:00
|
|
|
reader := strings.NewReader(`[
|
|
|
|
{
|
|
|
|
"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-03-14 23:27:54 +00:00
|
|
|
srv, err := Parse(reader, builtin.AnyDataType{}, builtin.BoolDataType{})
|
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-03-15 00:37:28 +00:00
|
|
|
ErrMissingParamDesc,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
{ // invalid param name suffix
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1_": { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
2020-03-15 00:37:28 +00:00
|
|
|
ErrMissingParamDesc,
|
2020-03-14 14:24:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{ // missing param description
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrMissingParamDesc,
|
|
|
|
},
|
|
|
|
{ // empty param description
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { "info": "" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrMissingParamDesc,
|
|
|
|
},
|
|
|
|
|
|
|
|
{ // missing param type
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { "info": "valid" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrMissingParamType,
|
|
|
|
},
|
|
|
|
{ // empty param type
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { "info": "valid", "type": "" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrMissingParamType,
|
|
|
|
},
|
|
|
|
{ // invalid type (optional mark only)
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"param1": { "info": "valid", "type": "?" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
|
|
|
|
ErrMissingParamType,
|
|
|
|
},
|
|
|
|
{ // 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
|
|
|
|
ErrParamNameConflict,
|
|
|
|
},
|
|
|
|
{ // 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
|
|
|
|
ErrParamNameConflict,
|
|
|
|
},
|
|
|
|
{ // 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
|
|
|
|
ErrParamNameConflict,
|
|
|
|
},
|
|
|
|
|
|
|
|
{ // 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" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrMandatoryRename,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"GET@abc": { "info": "valid", "type": "any" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrMandatoryRename,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"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" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrIllegalOptionalURIParam,
|
|
|
|
},
|
|
|
|
{ // URI parameter not specified
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/",
|
|
|
|
"info": "info",
|
|
|
|
"in": {
|
|
|
|
"{uri}": { "info": "valid", "type": "?any", "name": "freename" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrUnspecifiedBraceCapture,
|
|
|
|
},
|
|
|
|
{ // URI parameter not defined
|
|
|
|
`[
|
|
|
|
{
|
|
|
|
"method": "GET",
|
|
|
|
"path": "/{uri}",
|
|
|
|
"info": "info",
|
|
|
|
"in": { }
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrUndefinedBraceCapture,
|
|
|
|
},
|
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-03-14 23:27:54 +00:00
|
|
|
_, err := Parse(strings.NewReader(test.Raw), builtin.AnyDataType{})
|
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": {}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrPatternCollision,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "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
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrPatternCollision,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "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
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrPatternCollision,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "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
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrPatternCollision,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "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
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrPatternCollision,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "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
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
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": "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
|
|
|
}
|
|
|
|
}
|
|
|
|
]`,
|
|
|
|
ErrPatternCollision,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[
|
|
|
|
{ "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) {
|
|
|
|
_, err := Parse(strings.NewReader(test.Config), builtin.StringDataType{}, builtin.UintDataType{})
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`[ {
|
|
|
|
"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) {
|
|
|
|
srv, err := Parse(strings.NewReader(test.Config), builtin.AnyDataType{}, builtin.IntDataType{}, builtin.BoolDataType{})
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|