github.com/blend/go-sdk@v1.20220411.3/web/cached_static_file_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 web
     9  
    10  import (
    11  	"bytes"
    12  	"io"
    13  	"net/http"
    14  	"testing"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  	"github.com/blend/go-sdk/logger"
    18  	"github.com/blend/go-sdk/webutil"
    19  )
    20  
    21  func Test_NewCachedStaticFile(t *testing.T) {
    22  	its := assert.New(t)
    23  
    24  	csf, err := NewCachedStaticFile("testdata/test_file.html")
    25  	its.Nil(err)
    26  	its.Equal("testdata/test_file.html", csf.Path)
    27  	its.Equal(190, csf.Size)
    28  	its.False(csf.ModTime.IsZero())
    29  	its.Equal("da9a836ffc32feea4b26a536d3d0eccc", csf.ETag)
    30  
    31  	contents, err := io.ReadAll(csf.Contents)
    32  	its.Nil(err)
    33  	its.Contains(string(contents), `<title>Test!</title>`)
    34  }
    35  
    36  func Test_CachedStaticFile_Render(t *testing.T) {
    37  	its := assert.New(t)
    38  
    39  	csf, err := NewCachedStaticFile("testdata/test_file.html")
    40  	its.Nil(err)
    41  
    42  	buf := new(bytes.Buffer)
    43  	ctx := MockCtxWithBuffer(http.MethodGet, "index.html", buf)
    44  	err = csf.Render(ctx)
    45  	its.Nil(err)
    46  	its.Equal("da9a836ffc32feea4b26a536d3d0eccc", ctx.Response.Header().Get(webutil.HeaderETag))
    47  	its.Equal("text/html; charset=utf-8", ctx.Response.Header().Get(webutil.HeaderContentType))
    48  
    49  	its.Equal("testdata/test_file.html", logger.GetLabels(ctx.Context())["web.static_file_cached"])
    50  	its.Contains(buf.String(), `<title>Test!</title>`)
    51  }