...

Source file src/github.com/go-openapi/runtime/client_response_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  	"io"
    21  	"io/fs"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  type response struct {
    30  }
    31  
    32  func (r response) Code() int {
    33  	return 490
    34  }
    35  func (r response) Message() string {
    36  	return "the message"
    37  }
    38  func (r response) GetHeader(_ string) string {
    39  	return "the header"
    40  }
    41  func (r response) GetHeaders(_ string) []string {
    42  	return []string{"the headers", "the headers2"}
    43  }
    44  func (r response) Body() io.ReadCloser {
    45  	return io.NopCloser(bytes.NewBufferString("the content"))
    46  }
    47  
    48  func TestResponseReaderFunc(t *testing.T) {
    49  	var actual struct {
    50  		Header, Message, Body string
    51  		Code                  int
    52  	}
    53  	reader := ClientResponseReaderFunc(func(r ClientResponse, _ Consumer) (interface{}, error) {
    54  		b, _ := io.ReadAll(r.Body())
    55  		actual.Body = string(b)
    56  		actual.Code = r.Code()
    57  		actual.Message = r.Message()
    58  		actual.Header = r.GetHeader("blah")
    59  		return actual, nil
    60  	})
    61  	_, _ = reader.ReadResponse(response{}, nil)
    62  	assert.Equal(t, "the content", actual.Body)
    63  	assert.Equal(t, "the message", actual.Message)
    64  	assert.Equal(t, "the header", actual.Header)
    65  	assert.Equal(t, 490, actual.Code)
    66  }
    67  
    68  func TestResponseReaderFuncError(t *testing.T) {
    69  	reader := ClientResponseReaderFunc(func(r ClientResponse, _ Consumer) (interface{}, error) {
    70  		_, _ = io.ReadAll(r.Body())
    71  		return nil, NewAPIError("fake", errors.New("writer closed"), 490)
    72  	})
    73  	_, err := reader.ReadResponse(response{}, nil)
    74  	require.Error(t, err)
    75  	assert.True(t, strings.Contains(err.Error(), "writer closed"), err.Error())
    76  
    77  	reader = func(r ClientResponse, _ Consumer) (interface{}, error) {
    78  		_, _ = io.ReadAll(r.Body())
    79  		err := &fs.PathError{
    80  			Op:   "write",
    81  			Path: "path/to/fake",
    82  			Err:  fs.ErrClosed,
    83  		}
    84  		return nil, NewAPIError("fake", err, 200)
    85  	}
    86  	_, err = reader.ReadResponse(response{}, nil)
    87  	require.Error(t, err)
    88  	assert.Contains(t, err.Error(), "file already closed")
    89  
    90  }
    91  

View as plain text