1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
package openapi3
import (
"context"
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestEncodingJSON(t *testing.T) {
t.Log("Marshal *openapi3.Encoding to JSON")
data, err := json.Marshal(encoding())
require.NoError(t, err)
require.NotEmpty(t, data)
t.Log("Unmarshal *openapi3.Encoding from JSON")
docA := &Encoding{}
err = json.Unmarshal(encodingJSON, &docA)
require.NoError(t, err)
require.NotEmpty(t, docA)
t.Log("Validate *openapi3.Encoding")
err = docA.Validate(context.Background())
require.NoError(t, err)
t.Log("Ensure representations match")
dataA, err := json.Marshal(docA)
require.NoError(t, err)
require.JSONEq(t, string(data), string(encodingJSON))
require.JSONEq(t, string(data), string(dataA))
}
var encodingJSON = []byte(`
{
"contentType": "application/json",
"headers": {
"someHeader": {}
},
"style": "form",
"explode": true,
"allowReserved": true
}
`)
func encoding() *Encoding {
return &Encoding{
ContentType: "application/json",
Headers: map[string]*HeaderRef{
"someHeader": {
Value: &Header{},
},
},
Style: "form",
Explode: BoolPtr(true),
AllowReserved: true,
}
}
func TestEncodingSerializationMethod(t *testing.T) {
testCases := []struct {
name string
enc *Encoding
want *SerializationMethod
}{
{
name: "default",
want: &SerializationMethod{Style: SerializationForm, Explode: true},
},
{
name: "encoding with style",
enc: &Encoding{Style: SerializationSpaceDelimited},
want: &SerializationMethod{Style: SerializationSpaceDelimited, Explode: true},
},
{
name: "encoding with explode",
enc: &Encoding{Explode: BoolPtr(true)},
want: &SerializationMethod{Style: SerializationForm, Explode: true},
},
{
name: "encoding with no explode",
enc: &Encoding{Explode: BoolPtr(false)},
want: &SerializationMethod{Style: SerializationForm, Explode: false},
},
{
name: "encoding with style and explode ",
enc: &Encoding{Style: SerializationSpaceDelimited, Explode: BoolPtr(false)},
want: &SerializationMethod{Style: SerializationSpaceDelimited, Explode: false},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := tc.enc.SerializationMethod()
require.EqualValues(t, got, tc.want, "got %#v, want %#v", got, tc.want)
})
}
}
|