github.com/blend/go-sdk@v1.20220411.3/webutil/write_helpers_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package webutil
     9  
    10  import (
    11  	"bytes"
    12  	"encoding/json"
    13  	"fmt"
    14  	"io"
    15  	"net"
    16  	"net/http"
    17  	"testing"
    18  
    19  	"github.com/blend/go-sdk/assert"
    20  	"github.com/blend/go-sdk/ex"
    21  )
    22  
    23  func TestWriteNoContent(t *testing.T) {
    24  	assert := assert.New(t)
    25  
    26  	buf := new(bytes.Buffer)
    27  	res := NewMockResponse(buf)
    28  	assert.Nil(WriteNoContent(res))
    29  	assert.Equal(http.StatusNoContent, res.StatusCode())
    30  	assert.Zero(buf.Len())
    31  }
    32  
    33  func TestWriteRawContent(t *testing.T) {
    34  	assert := assert.New(t)
    35  
    36  	buf := new(bytes.Buffer)
    37  	res := NewMockResponse(buf)
    38  	assert.Nil(WriteRawContent(res, http.StatusOK, []byte("foo bar baz")))
    39  	assert.Equal(http.StatusOK, res.StatusCode())
    40  	assert.Equal("foo bar baz", buf.String())
    41  }
    42  
    43  func TestWriteJSON(t *testing.T) {
    44  	assert := assert.New(t)
    45  
    46  	buf := new(bytes.Buffer)
    47  	res := NewMockResponse(buf)
    48  	assert.Nil(WriteJSON(res, http.StatusOK, map[string]interface{}{"foo": "bar"}))
    49  	assert.Equal(http.StatusOK, res.StatusCode())
    50  	assert.Equal("{\"foo\":\"bar\"}\n", buf.String())
    51  }
    52  
    53  type flakyWriter struct {
    54  	Err error
    55  }
    56  
    57  func (fw flakyWriter) Write(_ []byte) (int, error) {
    58  	return 0, fw.Err
    59  }
    60  
    61  func TestWriteJSON_Error(t *testing.T) {
    62  	assert := assert.New(t)
    63  
    64  	flaky := flakyWriter{
    65  		Err: fmt.Errorf("flaky error"),
    66  	}
    67  	res := NewMockResponse(flaky)
    68  	err := WriteJSON(res, http.StatusOK, map[string]interface{}{"foo": "bar"})
    69  	assert.NotNil(err)
    70  	assert.Equal("flaky error", err.Error())
    71  	assert.Equal(http.StatusOK, res.StatusCode())
    72  }
    73  
    74  func TestWriteJSON_Error_NetOp(t *testing.T) {
    75  	assert := assert.New(t)
    76  
    77  	flaky := flakyWriter{
    78  		Err: &net.OpError{
    79  			Op: "test",
    80  		},
    81  	}
    82  	res := NewMockResponse(flaky)
    83  	err := WriteJSON(res, http.StatusOK, map[string]interface{}{"foo": "bar"})
    84  	assert.NotNil(err)
    85  	assert.Equal(ErrNetWrite, ex.ErrClass(err))
    86  	assert.Equal(http.StatusOK, res.StatusCode())
    87  }
    88  
    89  type xmltest struct {
    90  	Foo string `xml:"foo"`
    91  }
    92  
    93  func TestWriteXML(t *testing.T) {
    94  	assert := assert.New(t)
    95  
    96  	buf := new(bytes.Buffer)
    97  	res := NewMockResponse(buf)
    98  	assert.Nil(WriteXML(res, http.StatusOK, xmltest{Foo: "bar"}))
    99  	assert.Equal(http.StatusOK, res.StatusCode())
   100  	assert.Equal("<xmltest><foo>bar</foo></xmltest>", buf.String())
   101  }
   102  
   103  func TestWriteXML_Error(t *testing.T) {
   104  	assert := assert.New(t)
   105  
   106  	flaky := flakyWriter{
   107  		Err: fmt.Errorf("flaky error"),
   108  	}
   109  	res := NewMockResponse(flaky)
   110  	err := WriteXML(res, http.StatusOK, xmltest{Foo: "bar"})
   111  	assert.NotNil(err)
   112  	assert.Equal("flaky error", err.Error())
   113  	assert.Equal(http.StatusOK, res.StatusCode())
   114  }
   115  
   116  func TestWriteXML_Error_NetOp(t *testing.T) {
   117  	assert := assert.New(t)
   118  
   119  	flaky := flakyWriter{
   120  		Err: &net.OpError{
   121  			Op: "test",
   122  		},
   123  	}
   124  	res := NewMockResponse(flaky)
   125  	err := WriteXML(res, http.StatusOK, xmltest{Foo: "bar"})
   126  	assert.NotNil(err)
   127  	assert.Equal(ErrNetWrite, ex.ErrClass(err))
   128  	assert.Equal(http.StatusOK, res.StatusCode())
   129  }
   130  
   131  func TestDeserializeReaderAsJSON(t *testing.T) {
   132  	assert := assert.New(t)
   133  
   134  	contents, err := json.Marshal(map[string]interface{}{"foo": "bar"})
   135  	assert.Nil(err)
   136  
   137  	output := make(map[string]interface{})
   138  
   139  	assert.Nil(DeserializeReaderAsJSON(&output, io.NopCloser(bytes.NewBuffer(contents))))
   140  	assert.Equal("bar", output["foo"])
   141  }