github.com/xmidt-org/webpa-common@v1.11.9/xhttp/bufferedWriter_test.go (about)

     1  package xhttp
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func testBufferedWriterClose(t *testing.T) {
    13  	const text = "close test"
    14  
    15  	var (
    16  		assert = assert.New(t)
    17  		writer BufferedWriter
    18  	)
    19  
    20  	assert.NotNil(writer.Header())
    21  	c, err := writer.Write([]byte(text))
    22  	assert.Equal(len(text), c)
    23  	assert.NoError(err)
    24  	assert.NoError(writer.Close())
    25  
    26  	assert.NotNil(writer.Header())
    27  	c, err = writer.Write([]byte(text))
    28  	assert.Zero(c)
    29  	assert.Error(err)
    30  	assert.Error(writer.Close())
    31  }
    32  
    33  func testBufferedWriterWriteToEmpty(t *testing.T) {
    34  	var (
    35  		assert = assert.New(t)
    36  		writer BufferedWriter
    37  
    38  		response = httptest.NewRecorder()
    39  	)
    40  
    41  	c, err := writer.WriteTo(response)
    42  	assert.Zero(c)
    43  	assert.NoError(err)
    44  	assert.Equal(http.StatusOK, response.Code)
    45  	assert.Empty(response.HeaderMap)
    46  	assert.Empty(response.Body)
    47  	assert.False(response.Flushed)
    48  
    49  	assert.Error(writer.Close())
    50  	c, err = writer.WriteTo(response)
    51  	assert.Zero(c)
    52  	assert.Error(err)
    53  }
    54  
    55  func testBufferedWriterWriteToWithContent(t *testing.T) {
    56  	const text = "hello, world!"
    57  
    58  	var (
    59  		assert = assert.New(t)
    60  		writer BufferedWriter
    61  
    62  		response = httptest.NewRecorder()
    63  	)
    64  
    65  	writer.Header().Set("Content-Type", "text/plain")
    66  	writer.Header().Set("X-Custom", "zippidee doo da")
    67  	writer.Header().Set("X-Value", "1")
    68  	writer.Header().Add("X-Value", "2")
    69  	c, err := writer.Write([]byte(text))
    70  	assert.Equal(len(text), c)
    71  	assert.NoError(err)
    72  
    73  	c, err = writer.WriteTo(response)
    74  	assert.Equal(len(text), c)
    75  	assert.NoError(err)
    76  	assert.Equal(http.StatusOK, response.Code)
    77  	assert.Equal(
    78  		http.Header{
    79  			"Content-Type":   {"text/plain"},
    80  			"X-Custom":       {"zippidee doo da"},
    81  			"X-Value":        {"1", "2"},
    82  			"Content-Length": {strconv.Itoa(len(text))},
    83  		},
    84  		response.HeaderMap,
    85  	)
    86  	assert.Equal(text, response.Body.String())
    87  	assert.False(response.Flushed)
    88  
    89  	assert.Error(writer.Close())
    90  	c, err = writer.WriteTo(response)
    91  	assert.Zero(c)
    92  	assert.Error(err)
    93  }
    94  
    95  func testBufferedWriterWriteToCustomResponseCode(t *testing.T) {
    96  	const text = "with custom response code!"
    97  
    98  	var (
    99  		assert = assert.New(t)
   100  		writer BufferedWriter
   101  
   102  		response = httptest.NewRecorder()
   103  	)
   104  
   105  	writer.Header().Set("Content-Type", "text/plain")
   106  	writer.Header().Set("X-Custom", "zippidee doo da")
   107  	writer.Header().Set("X-Value", "1")
   108  	writer.Header().Add("X-Value", "2")
   109  	writer.WriteHeader(499)
   110  	c, err := writer.Write([]byte(text))
   111  	assert.Equal(len(text), c)
   112  	assert.NoError(err)
   113  	writer.WriteHeader(333) // should be ignored
   114  
   115  	c, err = writer.WriteTo(response)
   116  	assert.Equal(len(text), c)
   117  	assert.NoError(err)
   118  	assert.Equal(499, response.Code)
   119  	assert.Equal(
   120  		http.Header{
   121  			"Content-Type":   {"text/plain"},
   122  			"X-Custom":       {"zippidee doo da"},
   123  			"X-Value":        {"1", "2"},
   124  			"Content-Length": {strconv.Itoa(len(text))},
   125  		},
   126  		response.HeaderMap,
   127  	)
   128  	assert.Equal(text, response.Body.String())
   129  	assert.False(response.Flushed)
   130  
   131  	assert.Error(writer.Close())
   132  	c, err = writer.WriteTo(response)
   133  	assert.Zero(c)
   134  	assert.Error(err)
   135  }
   136  
   137  func testBufferedWriterWriteHeaderBadCode(t *testing.T) {
   138  	var (
   139  		assert = assert.New(t)
   140  		writer BufferedWriter
   141  	)
   142  
   143  	assert.Panics(func() {
   144  		writer.WriteHeader(1)
   145  	})
   146  }
   147  
   148  func TestBufferedWriter(t *testing.T) {
   149  	t.Run("Close", testBufferedWriterClose)
   150  	t.Run("WriteTo", func(t *testing.T) {
   151  		t.Run("Empty", testBufferedWriterWriteToEmpty)
   152  		t.Run("WithContent", testBufferedWriterWriteToWithContent)
   153  		t.Run("CustomResponseCode", testBufferedWriterWriteToCustomResponseCode)
   154  	})
   155  	t.Run("WriteHeader", func(t *testing.T) {
   156  		t.Run("BadCode", testBufferedWriterWriteHeaderBadCode)
   157  	})
   158  }