...

Source file src/github.com/go-openapi/runtime/text_test.go

Documentation: github.com/go-openapi/runtime

     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 runtime
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"fmt"
    21  	"net/http/httptest"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  var consProdText = `The quick brown fox jumped over the lazy dog.`
    29  
    30  func TestTextConsumer(t *testing.T) {
    31  	cons := TextConsumer()
    32  
    33  	// can consume as a string
    34  	var str string
    35  	err1 := cons.Consume(bytes.NewBufferString(consProdText), &str)
    36  	require.NoError(t, err1)
    37  	assert.Equal(t, consProdText, str)
    38  
    39  	var tu textUnmarshalDummy
    40  
    41  	// can consume as a TextUnmarshaler
    42  	err3 := cons.Consume(bytes.NewBufferString(consProdText), &tu)
    43  	require.NoError(t, err3)
    44  	assert.Equal(t, consProdText, tu.str)
    45  
    46  	// text unmarshal objects can return an error as well, this will be propagated
    47  	require.NoError(t, cons.Consume(bytes.NewBuffer(nil), &tu))
    48  
    49  	// when readers can't be read, those errors will be propogated as well
    50  	require.Error(t, cons.Consume(new(nopReader), &tu))
    51  
    52  	// readers can also not be nil
    53  	require.Error(t, cons.Consume(nil, &tu))
    54  
    55  	// can't consume nil ptr's or unsupported types
    56  	require.Error(t, cons.Consume(bytes.NewBufferString(consProdText), nil))
    57  	require.Error(t, cons.Consume(bytes.NewBufferString(consProdText), 42))
    58  	require.Error(t, cons.Consume(bytes.NewBufferString(consProdText), &struct{}{}))
    59  }
    60  
    61  type textUnmarshalDummy struct {
    62  	str string
    63  }
    64  
    65  func (t *textUnmarshalDummy) UnmarshalText(b []byte) error {
    66  	if len(b) == 0 {
    67  		return errors.New("no text given")
    68  	}
    69  
    70  	t.str = string(b)
    71  	return nil
    72  }
    73  
    74  type nopReader struct{}
    75  
    76  func (n *nopReader) Read(_ []byte) (int, error) {
    77  	return 0, errors.New("nop")
    78  }
    79  
    80  func TestTextProducer(t *testing.T) {
    81  	prod := TextProducer()
    82  	rw := httptest.NewRecorder()
    83  	err := prod.Produce(rw, consProdText)
    84  	require.NoError(t, err)
    85  	assert.Equal(t, consProdText, rw.Body.String())
    86  	rw2 := httptest.NewRecorder()
    87  	err2 := prod.Produce(rw2, &consProdText)
    88  	require.NoError(t, err2)
    89  	assert.Equal(t, consProdText, rw2.Body.String())
    90  
    91  	// should always work with type aliases
    92  	// as an alias is sometimes given by generated go-swagger code
    93  	type alias string
    94  	aliasProdText := alias(consProdText)
    95  	rw3 := httptest.NewRecorder()
    96  	err3 := prod.Produce(rw3, aliasProdText)
    97  	require.NoError(t, err3)
    98  	assert.Equal(t, consProdText, rw3.Body.String())
    99  	rw4 := httptest.NewRecorder()
   100  	err4 := prod.Produce(rw4, &aliasProdText)
   101  	require.NoError(t, err4)
   102  	assert.Equal(t, consProdText, rw4.Body.String())
   103  
   104  	const answer = "42"
   105  
   106  	// Should always work with objects implementing Stringer interface
   107  	rw5 := httptest.NewRecorder()
   108  	err5 := prod.Produce(rw5, &stringerDummy{answer})
   109  	require.NoError(t, err5)
   110  	assert.Equal(t, answer, rw5.Body.String())
   111  
   112  	// Should always work with objects implementing TextMarshaler interface
   113  	rw6 := httptest.NewRecorder()
   114  	err6 := prod.Produce(rw6, &textMarshalDummy{answer})
   115  	require.NoError(t, err6)
   116  	assert.Equal(t, answer, rw6.Body.String())
   117  
   118  	rw10 := httptest.NewRecorder()
   119  	err10 := prod.Produce(rw10, errors.New(answer))
   120  	require.NoError(t, err10)
   121  	assert.Equal(t, answer, rw10.Body.String())
   122  
   123  	rw11 := httptest.NewRecorder()
   124  	err11 := prod.Produce(rw11, Error{Message: answer})
   125  	require.NoError(t, err11)
   126  	assert.Equal(t, fmt.Sprintf(`{"message":%q}`, answer), rw11.Body.String())
   127  
   128  	rw12 := httptest.NewRecorder()
   129  	err12 := prod.Produce(rw12, &Error{Message: answer})
   130  	require.NoError(t, err12)
   131  	assert.Equal(t, fmt.Sprintf(`{"message":%q}`, answer), rw12.Body.String())
   132  
   133  	rw13 := httptest.NewRecorder()
   134  	err13 := prod.Produce(rw13, []string{answer})
   135  	require.NoError(t, err13)
   136  	assert.Equal(t, fmt.Sprintf(`[%q]`, answer), rw13.Body.String())
   137  
   138  	// should not work with anything that's not (indirectly) a string
   139  	rw7 := httptest.NewRecorder()
   140  	err7 := prod.Produce(rw7, 42)
   141  	require.Error(t, err7)
   142  	// nil values should also be safely caught with an error
   143  	rw8 := httptest.NewRecorder()
   144  	err8 := prod.Produce(rw8, nil)
   145  	require.Error(t, err8)
   146  
   147  	// writer can not be nil
   148  	require.Error(t, prod.Produce(nil, &textMarshalDummy{answer}))
   149  
   150  	// should not work for a textMarshaler that returns an error during marshalling
   151  	rw9 := httptest.NewRecorder()
   152  	err9 := prod.Produce(rw9, new(textMarshalDummy))
   153  	require.Error(t, err9)
   154  }
   155  
   156  type Error struct {
   157  	Message string `json:"message"`
   158  }
   159  
   160  type stringerDummy struct {
   161  	str string
   162  }
   163  
   164  func (t *stringerDummy) String() string {
   165  	return t.str
   166  }
   167  
   168  type textMarshalDummy struct {
   169  	str string
   170  }
   171  
   172  func (t *textMarshalDummy) MarshalText() ([]byte, error) {
   173  	if t.str == "" {
   174  		return nil, errors.New("no text set")
   175  	}
   176  	return []byte(t.str), nil
   177  }
   178  

View as plain text