added tests for v3 request_body

after a few drinks, not so easy.
This commit is contained in:
Dave Shanley
2022-08-15 21:34:28 -04:00
parent 10bf40ad97
commit 93fc2a79fe
3 changed files with 63 additions and 1 deletions

View File

@@ -223,7 +223,7 @@ func TestPaths_Build_BadRef(t *testing.T) {
func TestPathItem_Build_GoodRef(t *testing.T) { func TestPathItem_Build_GoodRef(t *testing.T) {
// this is kinda nuts, it's also not illegal, however the mechanics still need to work. // this is kinda nuts, it's also not illegal, however the mechanics still need to work.bfes
yml := `"/some/path": yml := `"/some/path":
description: this is some path description: this is some path
get: get:

View File

@@ -1,3 +1,6 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package v3 package v3
import ( import (
@@ -13,6 +16,10 @@ type RequestBody struct {
Extensions map[low.KeyReference[string]]low.ValueReference[any] Extensions map[low.KeyReference[string]]low.ValueReference[any]
} }
func (rb *RequestBody) FindExtension(ext string) *low.ValueReference[any] {
return low.FindItemInMap[any](ext, rb.Extensions)
}
func (rb *RequestBody) FindContent(cType string) *low.ValueReference[*MediaType] { func (rb *RequestBody) FindContent(cType string) *low.ValueReference[*MediaType] {
return low.FindItemInMap[*MediaType](cType, rb.Content.Value) return low.FindItemInMap[*MediaType](cType, rb.Content.Value)
} }

View File

@@ -0,0 +1,55 @@
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package v3
import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestRequestBody_Build(t *testing.T) {
yml := `description: a nice request
required: true
content:
fresh/fish:
example: nice.
x-requesto: presto`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var n RequestBody
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx)
assert.NoError(t, err)
assert.Equal(t, "a nice request", n.Description.Value)
assert.True(t, n.Required.Value)
assert.Equal(t, "nice.", n.FindContent("fresh/fish").Value.Example.Value)
assert.Equal(t, "presto", n.FindExtension("x-requesto").Value)
}
func TestRequestBody_Fail(t *testing.T) {
yml := `content:
$ref: #illegal`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var n RequestBody
err := low.BuildModel(&idxNode, &n)
assert.NoError(t, err)
err = n.Build(idxNode.Content[0], idx)
assert.Error(t, err)
}