github.com/cloudwego/hertz@v0.9.3/pkg/app/server/render/html_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package render
    18  
    19  import (
    20  	"html/template"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    27  	"github.com/cloudwego/hertz/pkg/common/utils"
    28  	"github.com/cloudwego/hertz/pkg/protocol"
    29  )
    30  
    31  func TestHTMLDebug_StartChecker_timer(t *testing.T) {
    32  	render := &HTMLDebug{
    33  		RefreshInterval: time.Second,
    34  		Delims:          Delims{Left: "{[{", Right: "}]}"},
    35  		FuncMap:         template.FuncMap{},
    36  		Files:           []string{"../../../common/testdata/template/index.tmpl"},
    37  	}
    38  	select {
    39  	case <-render.reloadCh:
    40  		t.Fatalf("should not be triggered")
    41  	default:
    42  	}
    43  	render.startChecker()
    44  	select {
    45  	case <-time.After(render.RefreshInterval + 500*time.Millisecond):
    46  		t.Fatalf("should be triggered in 1.5 second")
    47  	case <-render.reloadCh:
    48  		render.reload()
    49  	}
    50  }
    51  
    52  func TestHTMLDebug_StartChecker_fs_watcher(t *testing.T) {
    53  	f, _ := ioutil.TempFile("./", "test.tmpl")
    54  	defer func() {
    55  		f.Close()
    56  		os.Remove(f.Name())
    57  	}()
    58  	render := &HTMLDebug{Files: []string{f.Name()}}
    59  	select {
    60  	case <-render.reloadCh:
    61  		t.Fatalf("should not be triggered")
    62  	default:
    63  	}
    64  	render.startChecker()
    65  	f.Write([]byte("hello"))
    66  	f.Sync()
    67  	select {
    68  	case <-time.After(50 * time.Millisecond):
    69  		t.Fatalf("should be triggered immediately")
    70  	case <-render.reloadCh:
    71  	}
    72  	select {
    73  	case <-render.reloadCh:
    74  		t.Fatalf("should not be triggered")
    75  	default:
    76  	}
    77  }
    78  
    79  func TestRenderHTML(t *testing.T) {
    80  	resp := &protocol.Response{}
    81  
    82  	tmpl := template.Must(template.New("").
    83  		Delims("{[{", "}]}").
    84  		Funcs(template.FuncMap{}).
    85  		ParseFiles("../../../common/testdata/template/index.tmpl"))
    86  
    87  	r := &HTMLProduction{Template: tmpl}
    88  
    89  	html := r.Instance("index.tmpl", utils.H{
    90  		"title": "Main website",
    91  	})
    92  
    93  	err := r.Close()
    94  	assert.Nil(t, err)
    95  
    96  	html.WriteContentType(resp)
    97  	assert.DeepEqual(t, []byte("text/html; charset=utf-8"), resp.Header.Peek("Content-Type"))
    98  
    99  	err = html.Render(resp)
   100  
   101  	assert.Nil(t, err)
   102  	assert.DeepEqual(t, []byte("text/html; charset=utf-8"), resp.Header.Peek("Content-Type"))
   103  	assert.DeepEqual(t, []byte("<html><h1>Main website</h1></html>"), resp.Body())
   104  
   105  	respDebug := &protocol.Response{}
   106  
   107  	rDebug := &HTMLDebug{
   108  		Template: tmpl,
   109  		Delims:   Delims{Left: "{[{", Right: "}]}"},
   110  		FuncMap:  template.FuncMap{},
   111  		Files:    []string{"../../../common/testdata/template/index.tmpl"},
   112  	}
   113  
   114  	htmlDebug := rDebug.Instance("index.tmpl", utils.H{
   115  		"title": "Main website",
   116  	})
   117  
   118  	err = rDebug.Close()
   119  	assert.Nil(t, err)
   120  
   121  	htmlDebug.WriteContentType(respDebug)
   122  	assert.DeepEqual(t, []byte("text/html; charset=utf-8"), respDebug.Header.Peek("Content-Type"))
   123  
   124  	err = htmlDebug.Render(respDebug)
   125  
   126  	assert.Nil(t, err)
   127  	assert.DeepEqual(t, []byte("text/html; charset=utf-8"), respDebug.Header.Peek("Content-Type"))
   128  	assert.DeepEqual(t, []byte("<html><h1>Main website</h1></html>"), respDebug.Body())
   129  }