mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 12:37:48 +00:00
now things are robust, we can move things around a little to prepare for the next set of incoming models. The extraction and builder functions have all been moved to the low packakge, and out of the v3 package.
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
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 TestEncoding_Build_Success(t *testing.T) {
|
|
|
|
yml := `contentType: hot/cakes
|
|
headers:
|
|
ohMyStars:
|
|
description: this is a header
|
|
required: true
|
|
allowEmptyValue: true
|
|
allowReserved: true
|
|
explode: true`
|
|
|
|
var idxNode yaml.Node
|
|
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
|
|
assert.NoError(t, mErr)
|
|
idx := index.NewSpecIndex(&idxNode)
|
|
|
|
var n Encoding
|
|
err := low.BuildModel(&idxNode, &n)
|
|
assert.NoError(t, err)
|
|
|
|
err = n.Build(idxNode.Content[0], idx)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "hot/cakes", n.ContentType.Value)
|
|
assert.Equal(t, true, n.AllowReserved.Value)
|
|
assert.Equal(t, true, n.Explode.Value)
|
|
|
|
header := n.FindHeader("ohMyStars")
|
|
assert.NotNil(t, header.Value)
|
|
assert.Equal(t, "this is a header", header.Value.Description.Value)
|
|
assert.Equal(t, true, header.Value.Required.Value)
|
|
assert.Equal(t, true, header.Value.AllowEmptyValue.Value)
|
|
}
|
|
|
|
func TestEncoding_Build_Error(t *testing.T) {
|
|
|
|
yml := `contentType: hot/cakes
|
|
headers:
|
|
$ref: #/borked`
|
|
|
|
var idxNode yaml.Node
|
|
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
|
|
assert.NoError(t, mErr)
|
|
idx := index.NewSpecIndex(&idxNode)
|
|
|
|
var n Encoding
|
|
err := low.BuildModel(&idxNode, &n)
|
|
assert.NoError(t, err)
|
|
|
|
err = n.Build(idxNode.Content[0], idx)
|
|
assert.Error(t, err)
|
|
}
|