github.com/blend/go-sdk@v1.20220411.3/web/view_result_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  	"fmt"
    13  	"html/template"
    14  	"net/http"
    15  	"strings"
    16  	"testing"
    17  
    18  	"github.com/blend/go-sdk/assert"
    19  	"github.com/blend/go-sdk/env"
    20  	"github.com/blend/go-sdk/ex"
    21  	"github.com/blend/go-sdk/webutil"
    22  )
    23  
    24  type testViewModel struct {
    25  	Text string
    26  }
    27  
    28  type errorWriter struct {
    29  	ResponseWriter
    30  }
    31  
    32  func (ew *errorWriter) Write(_ []byte) (int, error) {
    33  	return -1, fmt.Errorf("error")
    34  }
    35  
    36  func TestViewResultRender(t *testing.T) {
    37  	assert := assert.New(t)
    38  
    39  	buffer := bytes.NewBuffer([]byte{})
    40  	rc := NewCtx(webutil.NewMockResponse(buffer), nil)
    41  
    42  	assert.True(ex.Is((&ViewResult{}).Render(nil), ErrUnsetViewTemplate))
    43  
    44  	testView := template.New("testView")
    45  	_, err := testView.Parse("{{ .ViewModel.Text }}")
    46  	assert.Nil(err)
    47  
    48  	vr := &ViewResult{
    49  		StatusCode: http.StatusOK,
    50  		ViewModel:  testViewModel{Text: "bar"},
    51  		Template:   testView,
    52  	}
    53  
    54  	err = vr.Render(rc)
    55  	assert.Nil(err)
    56  
    57  	assert.NotZero(buffer.Len())
    58  	assert.True(strings.Contains(buffer.String(), "bar"))
    59  
    60  	testView = template.New("testView")
    61  	_, err = testView.Parse("{{ .Env.String \"HELLO\" }}")
    62  	assert.Nil(err)
    63  
    64  	expected := "world"
    65  
    66  	curr := env.Env()
    67  	defer func() {
    68  		env.SetEnv(curr)
    69  	}()
    70  
    71  	env.SetEnv(env.New())
    72  	env.Env().Set("HELLO", expected)
    73  
    74  	vr = &ViewResult{
    75  		StatusCode: http.StatusOK,
    76  		ViewModel:  nil,
    77  		Template:   testView,
    78  	}
    79  
    80  	err = vr.Render(rc)
    81  	assert.Nil(err)
    82  
    83  	assert.NotZero(buffer.Len())
    84  	assert.True(strings.Contains(buffer.String(), expected))
    85  }
    86  
    87  func TestViewResultRenderErrorResponse(t *testing.T) {
    88  	assert := assert.New(t)
    89  
    90  	buffer := bytes.NewBuffer([]byte{})
    91  	rc := NewCtx(webutil.NewMockResponse(buffer), nil)
    92  
    93  	assert.True(ex.Is((&ViewResult{}).Render(nil), ErrUnsetViewTemplate))
    94  
    95  	testView := template.New("testView")
    96  	_, err := testView.Parse("{{ .ViewModel.Text }}")
    97  	assert.Nil(err)
    98  
    99  	vr := &ViewResult{
   100  		StatusCode: http.StatusOK,
   101  		ViewModel:  testViewModel{Text: "bar"},
   102  		Template:   testView,
   103  	}
   104  
   105  	rc.Response = &errorWriter{ResponseWriter: rc.Response}
   106  
   107  	err = vr.Render(rc)
   108  	assert.NotNil(err)
   109  }
   110  
   111  func TestViewResultRenderError(t *testing.T) {
   112  	assert := assert.New(t)
   113  
   114  	buffer := bytes.NewBuffer([]byte{})
   115  	rc := NewCtx(webutil.NewMockResponse(buffer), nil)
   116  
   117  	testView := template.New("testView")
   118  	_, err := testView.Parse("{{.ViewModel.Foo}}")
   119  	assert.Nil(err)
   120  
   121  	vr := &ViewResult{
   122  		StatusCode: http.StatusOK,
   123  		ViewModel:  testViewModel{Text: "bar"},
   124  		Template:   testView,
   125  	}
   126  
   127  	err = vr.Render(rc)
   128  	assert.NotNil(err)
   129  	assert.NotZero(buffer.Len())
   130  }
   131  
   132  func TestViewResultRenderErrorTemplate(t *testing.T) {
   133  	assert := assert.New(t)
   134  
   135  	buffer := bytes.NewBuffer([]byte{})
   136  	rc := NewCtx(webutil.NewMockResponse(buffer), nil)
   137  
   138  	views := template.New("main")
   139  
   140  	errorTemplate := views.New(DefaultTemplateNameInternalError)
   141  	_, err := errorTemplate.Parse("{{.}}")
   142  	assert.Nil(err)
   143  
   144  	testView := views.New("testView")
   145  	_, err = testView.Parse("Foo: {{.ViewModel.Foo}}")
   146  	assert.Nil(err)
   147  
   148  	vr := &ViewResult{
   149  		StatusCode: http.StatusOK,
   150  		ViewModel:  testViewModel{Text: "bar"},
   151  		Template:   testView,
   152  	}
   153  
   154  	err = vr.Render(rc)
   155  	assert.NotNil(err)
   156  }
   157  
   158  func TestViewResultErrorNestedViews(t *testing.T) {
   159  	assert := assert.New(t)
   160  
   161  	buffer := bytes.NewBuffer([]byte{})
   162  	rc := NewCtx(webutil.NewMockResponse(buffer), nil)
   163  
   164  	views := template.New("main")
   165  
   166  	errorTemplate := views.New(DefaultTemplateNameInternalError)
   167  	_, err := errorTemplate.Parse("{{.}}")
   168  	assert.Nil(err)
   169  
   170  	outerView := views.New("outerView")
   171  	_, err = outerView.Parse("Inner: {{ template \"innerView\" . }}")
   172  	assert.Nil(err)
   173  
   174  	testView := views.New("innerView")
   175  	_, err = testView.Parse("Foo: {{.ViewModel.Foo}}")
   176  	assert.Nil(err)
   177  
   178  	vr := &ViewResult{
   179  		StatusCode: http.StatusOK,
   180  		ViewModel:  testViewModel{Text: "bar"},
   181  		Template:   outerView,
   182  	}
   183  
   184  	err = vr.Render(rc)
   185  	assert.NotNil(err)
   186  }