github.com/blend/go-sdk@v1.20220411.3/webutil/http_request_event_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  	"context"
    13  	"encoding/json"
    14  	"net/http"
    15  	"net/url"
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/blend/go-sdk/assert"
    20  	"github.com/blend/go-sdk/logger"
    21  )
    22  
    23  func TestNewHTTPRequestEvent(t *testing.T) {
    24  	assert := assert.New(t)
    25  
    26  	hre := NewHTTPRequestEvent(nil,
    27  		OptHTTPRequestRequest(&http.Request{Method: "foo", URL: &url.URL{Scheme: "https", Host: "localhost", Path: "/foo/example-string"}}),
    28  		OptHTTPRequestContentEncoding("utf-8"),
    29  		OptHTTPRequestContentLength(1337),
    30  		OptHTTPRequestContentType("text/html"),
    31  		OptHTTPRequestElapsed(time.Second),
    32  		OptHTTPRequestRoute("/foo/:bar"),
    33  		OptHTTPRequestStatusCode(http.StatusOK),
    34  		OptHTTPRequestHeader(http.Header{"X-Bad": []string{"nope", "definitely nope"}}),
    35  	)
    36  
    37  	assert.Equal("foo", hre.Request.Method)
    38  	assert.Equal("utf-8", hre.ContentEncoding)
    39  	assert.Equal(1337, hre.ContentLength)
    40  	assert.Equal("text/html", hre.ContentType)
    41  	assert.Equal(time.Second, hre.Elapsed)
    42  	assert.Equal("/foo/:bar", hre.Route)
    43  	assert.Equal(http.StatusOK, hre.StatusCode)
    44  	assert.Equal("nope", hre.Header.Get("X-Bad"))
    45  
    46  	noColor := logger.NewTextOutputFormatter(logger.OptTextNoColor())
    47  	buf := new(bytes.Buffer)
    48  	hre.WriteText(noColor, buf)
    49  	assert.NotContains(buf.String(), "/foo/:bar")
    50  	assert.Contains(buf.String(), "/foo/example-string")
    51  	assert.NotContains(buf.String(), "X-Bad", "response headers should not be written to text output")
    52  	assert.NotContains(buf.String(), "definitely nope", "response headers should not be written to text output")
    53  
    54  	contents, err := json.Marshal(hre.Decompose())
    55  	assert.Nil(err)
    56  	assert.Contains(string(contents), "/foo/:bar")
    57  
    58  	assert.NotContains(string(contents), "X-Bad", "response headers should not be written to json output")
    59  	assert.NotContains(string(contents), "definitely nope", "response headers should not be written to json output")
    60  }
    61  
    62  func TestHTTPRequestEventListener(t *testing.T) {
    63  	assert := assert.New(t)
    64  
    65  	var didCall bool
    66  	listener := NewHTTPRequestEventListener(func(_ context.Context, hre HTTPRequestEvent) {
    67  		didCall = true
    68  	})
    69  	listener(context.Background(), logger.NewMessageEvent(logger.Info, "test"))
    70  	assert.False(didCall)
    71  	listener(context.Background(), NewHTTPRequestEvent(nil))
    72  	assert.True(didCall)
    73  }