...

Source file src/github.com/go-openapi/runtime/yamlpc/yaml_test.go

Documentation: github.com/go-openapi/runtime/yamlpc

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yamlpc
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  var consProdYAML = "name: Somebody\nid: 1\n"
    28  
    29  func TestYAMLConsumer(t *testing.T) {
    30  	cons := YAMLConsumer()
    31  	var data struct {
    32  		Name string
    33  		ID   int
    34  	}
    35  	err := cons.Consume(bytes.NewBufferString(consProdYAML), &data)
    36  	require.NoError(t, err)
    37  	assert.Equal(t, "Somebody", data.Name)
    38  	assert.Equal(t, 1, data.ID)
    39  }
    40  
    41  func TestYAMLProducer(t *testing.T) {
    42  	prod := YAMLProducer()
    43  	data := struct {
    44  		Name string `yaml:"name"`
    45  		ID   int    `yaml:"id"`
    46  	}{Name: "Somebody", ID: 1}
    47  
    48  	rw := httptest.NewRecorder()
    49  	err := prod.Produce(rw, data)
    50  	require.NoError(t, err)
    51  	assert.Equal(t, consProdYAML, rw.Body.String())
    52  }
    53  
    54  type failReaderWriter struct {
    55  }
    56  
    57  func (f *failReaderWriter) Read(_ []byte) (n int, err error) {
    58  	return 0, errors.New("expected")
    59  }
    60  
    61  func (f *failReaderWriter) Write(_ []byte) (n int, err error) {
    62  	return 0, errors.New("expected")
    63  }
    64  
    65  func TestFailYAMLWriter(t *testing.T) {
    66  	prod := YAMLProducer()
    67  	require.Error(t, prod.Produce(&failReaderWriter{}, nil))
    68  }
    69  
    70  func TestFailYAMLReader(t *testing.T) {
    71  	cons := YAMLConsumer()
    72  	require.Error(t, cons.Consume(&failReaderWriter{}, nil))
    73  }
    74  
    75  func TestYAMLConsumerObject(t *testing.T) {
    76  	const yamlDoc = `
    77  ---
    78  name: fred
    79  id: 123
    80  attributes:
    81    height: 12.3
    82    weight: 45
    83    list:
    84      - a
    85      - b
    86  `
    87  	cons := YAMLConsumer()
    88  	var data struct {
    89  		Name       string
    90  		ID         int
    91  		Attributes struct {
    92  			Height float64
    93  			Weight uint64
    94  			List   []string
    95  		}
    96  	}
    97  	require.NoError(t,
    98  		cons.Consume(bytes.NewBufferString(yamlDoc), &data),
    99  	)
   100  
   101  	assert.Equal(t, "fred", data.Name)
   102  	assert.Equal(t, 123, data.ID)
   103  	assert.InDelta(t, 12.3, data.Attributes.Height, 1e-9)
   104  	assert.Equal(t, uint64(45), data.Attributes.Weight)
   105  	assert.Len(t, data.Attributes.List, 2)
   106  	assert.Equal(t, "a", data.Attributes.List[0])
   107  }
   108  

View as plain text