github.com/blend/go-sdk@v1.20220411.3/webutil/http_logged_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  	"net/http"
    13  	"net/http/httptest"
    14  	"testing"
    15  
    16  	"github.com/blend/go-sdk/ansi"
    17  	"github.com/blend/go-sdk/assert"
    18  	"github.com/blend/go-sdk/logger"
    19  )
    20  
    21  func TestHTTPLogged(t *testing.T) {
    22  	assert := assert.New(t)
    23  
    24  	buf := new(bytes.Buffer)
    25  	log, err := logger.New(
    26  		logger.OptOutput(buf),
    27  		logger.OptAll(),
    28  	)
    29  	assert.Nil(err)
    30  
    31  	var didCall bool
    32  	server := httptest.NewServer(NestMiddleware(func(rw http.ResponseWriter, req *http.Request) {
    33  		didCall = true
    34  	}, HTTPLogged(log)))
    35  
    36  	res, err := http.Get(server.URL)
    37  	assert.Nil(err)
    38  	defer res.Body.Close()
    39  	assert.True(didCall)
    40  }
    41  
    42  func TestFormatHeaders(t *testing.T) {
    43  	assert := assert.New(t)
    44  
    45  	tf := logger.NewTextOutputFormatter(logger.OptTextNoColor())
    46  	actual := FormatHeaders(tf, ansi.ColorBlue, http.Header{"Foo": []string{"bar"}, "Moo": []string{"loo"}})
    47  	assert.Equal("{ Foo:bar Moo:loo }", actual)
    48  
    49  	actual = FormatHeaders(tf, ansi.ColorBlue, http.Header{"Moo": []string{"loo"}, "Foo": []string{"bar"}})
    50  	assert.Equal("{ Foo:bar Moo:loo }", actual)
    51  
    52  	tf = logger.NewTextOutputFormatter()
    53  	actual = FormatHeaders(tf, ansi.ColorBlue, http.Header{"Foo": []string{"bar"}, "Moo": []string{"loo"}})
    54  	assert.Equal("{ "+ansi.ColorBlue.Apply("Foo")+":bar "+ansi.ColorBlue.Apply("Moo")+":loo }", actual)
    55  }
    56  
    57  func TestColorizeByStatusCode(t *testing.T) {
    58  	assert := assert.New(t)
    59  
    60  	testCases := [...]struct {
    61  		StatusCode int
    62  		Value      string
    63  		Expected   string
    64  	}{
    65  		{StatusCode: http.StatusInternalServerError, Value: "this is a server error", Expected: ansi.ColorRed.Apply("this is a server error")},
    66  		{StatusCode: http.StatusBadRequest, Value: "this is a bad request", Expected: ansi.ColorYellow.Apply("this is a bad request")},
    67  		{StatusCode: http.StatusOK, Value: "this is ok", Expected: ansi.ColorGreen.Apply("this is ok")},
    68  	}
    69  
    70  	for _, tc := range testCases {
    71  		assert.Equal(tc.Expected, ColorizeByStatusCode(tc.StatusCode, tc.Value))
    72  	}
    73  }
    74  
    75  func TestColorizebyStatusCodeWithFormatter(t *testing.T) {
    76  	assert := assert.New(t)
    77  
    78  	noColor := logger.TextOutputFormatter{
    79  		NoColor: true,
    80  	}
    81  	color := logger.TextOutputFormatter{
    82  		NoColor: false,
    83  	}
    84  
    85  	testCases := [...]struct {
    86  		StatusCode int
    87  		Formatter  logger.TextFormatter
    88  		Value      string
    89  		Expected   string
    90  	}{
    91  		// Color
    92  		{StatusCode: http.StatusInternalServerError, Value: "this is a server error", Formatter: color, Expected: ansi.ColorRed.Apply("this is a server error")},
    93  		{StatusCode: http.StatusBadRequest, Value: "this is a bad request", Formatter: color, Expected: ansi.ColorYellow.Apply("this is a bad request")},
    94  		{StatusCode: http.StatusOK, Value: "this is ok", Formatter: color, Expected: ansi.ColorGreen.Apply("this is ok")},
    95  
    96  		// NoColor
    97  		{StatusCode: http.StatusInternalServerError, Value: "this is a server error", Formatter: noColor, Expected: "this is a server error"},
    98  		{StatusCode: http.StatusBadRequest, Value: "this is a bad request", Formatter: noColor, Expected: "this is a bad request"},
    99  		{StatusCode: http.StatusOK, Value: "this is ok", Formatter: noColor, Expected: "this is ok"},
   100  	}
   101  
   102  	for _, tc := range testCases {
   103  		assert.Equal(tc.Expected, ColorizeByStatusCodeWithFormatter(tc.Formatter, tc.StatusCode, tc.Value))
   104  	}
   105  }
   106  
   107  func TestColorizeStatusCode(t *testing.T) {
   108  	assert := assert.New(t)
   109  
   110  	testCases := [...]struct {
   111  		StatusCode int
   112  		Expected   string
   113  	}{
   114  		{StatusCode: http.StatusInternalServerError, Expected: ansi.ColorRed.Apply("500")},
   115  		{StatusCode: http.StatusBadRequest, Expected: ansi.ColorYellow.Apply("400")},
   116  		{StatusCode: http.StatusOK, Expected: ansi.ColorGreen.Apply("200")},
   117  	}
   118  
   119  	for _, tc := range testCases {
   120  		assert.Equal(tc.Expected, ColorizeStatusCode(tc.StatusCode))
   121  	}
   122  }
   123  
   124  func TestColorizeStatusCodeWithFormatter(t *testing.T) {
   125  	assert := assert.New(t)
   126  
   127  	noColor := logger.TextOutputFormatter{
   128  		NoColor: true,
   129  	}
   130  	color := logger.TextOutputFormatter{
   131  		NoColor: false,
   132  	}
   133  
   134  	testCases := [...]struct {
   135  		StatusCode int
   136  		Formatter  logger.TextFormatter
   137  		Expected   string
   138  	}{
   139  		// Color
   140  		{StatusCode: http.StatusInternalServerError, Formatter: color, Expected: ansi.ColorRed.Apply("500")},
   141  		{StatusCode: http.StatusBadRequest, Formatter: color, Expected: ansi.ColorYellow.Apply("400")},
   142  		{StatusCode: http.StatusOK, Formatter: color, Expected: ansi.ColorGreen.Apply("200")},
   143  
   144  		// NoColor
   145  		{StatusCode: http.StatusInternalServerError, Formatter: noColor, Expected: "500"},
   146  		{StatusCode: http.StatusBadRequest, Formatter: noColor, Expected: "400"},
   147  		{StatusCode: http.StatusOK, Formatter: noColor, Expected: "200"},
   148  	}
   149  
   150  	for _, tc := range testCases {
   151  		assert.Equal(tc.Expected, ColorizeStatusCodeWithFormatter(tc.Formatter, tc.StatusCode))
   152  	}
   153  }