github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/courier/transport_http/encode_test.go (about)

     1  package transport_http
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/johnnyeven/libtools/courier/httpx"
    12  	"github.com/johnnyeven/libtools/courier/status_error"
    13  	"github.com/johnnyeven/libtools/courier/transport_http/transform"
    14  )
    15  
    16  func NewTestRespWriter() *TestRespWriter {
    17  	return &TestRespWriter{
    18  		H: http.Header{},
    19  	}
    20  }
    21  
    22  type TestRespWriter struct {
    23  	bytes.Buffer
    24  	Status int
    25  	H      http.Header
    26  }
    27  
    28  func (rw *TestRespWriter) Header() http.Header {
    29  	return rw.H
    30  }
    31  
    32  func (rw *TestRespWriter) WriteHeader(code int) {
    33  	rw.Status = code
    34  }
    35  
    36  func TestEncodeHttpResponse_NoContent(t *testing.T) {
    37  	tt := assert.New(t)
    38  	rw := NewTestRespWriter()
    39  	req, _ := transform.NewRequest("GET", "/", nil)
    40  
    41  	err := encodeHttpResponse(context.Background(), rw, req, nil)
    42  	tt.NoError(err)
    43  	tt.Equal(http.StatusNoContent, rw.Status)
    44  }
    45  
    46  func TestEncodeHttpResponse_WithMetaAndContentType(t *testing.T) {
    47  	tt := assert.New(t)
    48  	rw := NewTestRespWriter()
    49  	req, _ := transform.NewRequest("GET", "/", nil)
    50  
    51  	file := NewFile("123.txt", "text/plain")
    52  	file.Write([]byte("123123"))
    53  
    54  	err := encodeHttpResponse(context.Background(), rw, req, file)
    55  	tt.NoError(err)
    56  	tt.Equal(http.StatusOK, rw.Status)
    57  	tt.Equal([]byte("123123"), rw.Bytes())
    58  	tt.Equal("attachment; filename=123.txt", rw.Header().Get("Content-Disposition"))
    59  	tt.Equal("text/plain;charset=utf-8", rw.Header().Get("Content-Type"))
    60  }
    61  
    62  func TestEncodeHttpResponse_WithStatus(t *testing.T) {
    63  	tt := assert.New(t)
    64  	rw := NewTestRespWriter()
    65  	req, _ := transform.NewRequest("GET", "/", nil)
    66  
    67  	err := encodeHttpResponse(context.Background(), rw, req, status_error.InvalidStruct.StatusError())
    68  	tt.NoError(err)
    69  	tt.Equal(status_error.InvalidStruct.StatusError().Status(), rw.Status)
    70  }
    71  
    72  func TestEncodeHttpResponse_SomeJSONForGet(t *testing.T) {
    73  	tt := assert.New(t)
    74  	rw := NewTestRespWriter()
    75  
    76  	respData := struct {
    77  		A string `json:"a"`
    78  		B string `json:"b"`
    79  	}{
    80  		A: "a",
    81  		B: "b",
    82  	}
    83  
    84  	req, _ := transform.NewRequest("GET", "/", nil)
    85  
    86  	err := encodeHttpResponse(context.Background(), rw, req, respData)
    87  	tt.NoError(err)
    88  	tt.Equal(http.StatusOK, rw.Status)
    89  	tt.Equal(`{"a":"a","b":"b"}`, rw.String())
    90  }
    91  
    92  type XMLData struct {
    93  	A string `json:"a"`
    94  	B string `json:"b"`
    95  }
    96  
    97  func (XMLData) ContentType() string {
    98  	return httpx.MIMEXML
    99  }
   100  
   101  func TestEncodeHttpResponse_SomeXMLForGet(t *testing.T) {
   102  	tt := assert.New(t)
   103  	rw := NewTestRespWriter()
   104  
   105  	respData := XMLData{
   106  		A: "a",
   107  		B: "b",
   108  	}
   109  
   110  	req, _ := transform.NewRequest("GET", "/", nil)
   111  
   112  	err := encodeHttpResponse(context.Background(), rw, req, respData)
   113  	tt.NoError(err)
   114  	tt.Equal(http.StatusOK, rw.Status)
   115  	tt.Equal(`<XMLData><A>a</A><B>b</B></XMLData>`, rw.String())
   116  }
   117  
   118  func TestEncodeHttpResponse_SomeJSONForPOST(t *testing.T) {
   119  	tt := assert.New(t)
   120  	rw := NewTestRespWriter()
   121  
   122  	respData := struct {
   123  		A string `json:"a"`
   124  		B string `json:"b"`
   125  	}{
   126  		A: "a",
   127  		B: "b",
   128  	}
   129  
   130  	req, _ := transform.NewRequest("POST", "/", nil)
   131  
   132  	err := encodeHttpResponse(context.Background(), rw, req, respData)
   133  	tt.NoError(err)
   134  	tt.Equal(http.StatusCreated, rw.Status)
   135  	tt.Equal(`{"a":"a","b":"b"}`, rw.String())
   136  }
   137  
   138  func TestEncodeHttpResponse_ByteDirectly(t *testing.T) {
   139  	tt := assert.New(t)
   140  	rw := NewTestRespWriter()
   141  
   142  	req, _ := transform.NewRequest("POST", "/", nil)
   143  
   144  	err := encodeHttpResponse(context.Background(), rw, req, []byte("123"))
   145  	tt.NoError(err)
   146  	tt.Equal(http.StatusCreated, rw.Status)
   147  	tt.Equal("123", rw.String())
   148  }