expand internal/config test coverage

- make parameter type "?" invalid as it marks it as optional only
- check optional vs. required parameters
- test subservice in method description error check
This commit is contained in:
Adrien Marquès 2019-11-21 22:18:00 +01:00
parent 61b58a9f98
commit e9c7160d2e
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
2 changed files with 85 additions and 2 deletions

View File

@ -146,6 +146,18 @@ func TestParseMissingMethodDescription(t *testing.T) {
}`,
ErrFormat.Wrap(ErrMissingMethodDesc.WrapString("GET /")),
},
{ // missing description
`{
"/": {
"subservice": {
"GET": {
}
}
}
}`,
ErrFormat.Wrap(ErrMissingMethodDesc.WrapString("GET subservice")),
},
{ // empty description
`{
"GET": {
@ -197,6 +209,46 @@ func TestParseMissingMethodDescription(t *testing.T) {
}
func TestOptionalParam(t *testing.T) {
reader := strings.NewReader(`{
"GET": {
"info": "info",
"in": {
"optional": { "info": "valid-desc", "type": "?optional-type" },
"required": { "info": "valid-desc", "type": "required-type" },
"required2": { "info": "valid-desc", "type": "a" },
"optional2": { "info": "valid-desc", "type": "?a" }
}
}
}`)
srv, err := Parse(reader)
if err != nil {
t.Errorf("unexpected error: '%s'", err)
t.FailNow()
}
method := srv.Method(http.MethodGet)
if method == nil {
t.Errorf("expected GET method not to be nil")
t.FailNow()
}
for pName, param := range method.Parameters {
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) {
tests := []struct {
Raw string
@ -284,6 +336,22 @@ func TestParseParameters(t *testing.T) {
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
nil,
},
{ // invalid type (optional mark only)
`{
"GET": {
"info": "info",
"in": {
"param1": {
"info": "valid",
"type": "?"
}
}
}
}`,
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
},
{ // valid description + valid type
`{
"GET": {
@ -291,7 +359,22 @@ func TestParseParameters(t *testing.T) {
"in": {
"param1": {
"info": "valid",
"type": "valid"
"type": "a"
}
}
}
}`,
nil,
nil,
},
{ // valid description + valid OPTIONAL type
`{
"GET": {
"info": "info",
"in": {
"param1": {
"info": "valid",
"type": "?valid"
}
}
}

View File

@ -61,7 +61,7 @@ func (methodDef *Method) checkAndFormat(servicePath string, httpMethod string) e
}
// 3.4. Manage invalid type
if len(pData.Type) < 1 {
if len(pData.Type) < 1 || pData.Type == "?" {
return ErrMissingParamType.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
}