github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/app_router_test.go (about)

     1  // Copyright 2017-present Kirill Danshin and Gramework contributors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  
    10  package gramework
    11  
    12  import (
    13  	"os"
    14  	"path/filepath"
    15  	"testing"
    16  
    17  	"github.com/valyala/fasthttp"
    18  )
    19  
    20  func testProvideTempFile(t *testing.T, action func(file, dir string)) {
    21  	tmpDir := t.TempDir()
    22  	tmpFile, err := os.CreateTemp(tmpDir, "servedirtmp_")
    23  	if err != nil {
    24  		t.Error("cannot create temporary file:", err)
    25  	}
    26  
    27  	// write some text into file
    28  	text := []byte("test_file_data")
    29  	if _, err = tmpFile.Write(text); err != nil {
    30  		t.Error("failed to write to temporary file:", err)
    31  	}
    32  
    33  	if err := tmpFile.Close(); err != nil {
    34  		t.Error(err)
    35  	}
    36  
    37  	action(filepath.Base(tmpFile.Name()), tmpDir)
    38  }
    39  
    40  func TestAppServeDir(t *testing.T) {
    41  	var funcSD func(*Context)
    42  
    43  	app := New()
    44  
    45  	ctx := Context{RequestCtx: &fasthttp.RequestCtx{}}
    46  
    47  	testProvideTempFile(t, func(file, dir string) {
    48  		ctx.Request.SetRequestURI(file)
    49  		funcSD = app.ServeDir(dir)
    50  		funcSD(&ctx)
    51  	})
    52  
    53  	if ctx.Response.StatusCode() != fasthttp.StatusOK {
    54  		t.Errorf("failed on file read by directory server %d", ctx.Response.StatusCode())
    55  	}
    56  }
    57  
    58  func TestAppServeDirNoCache(t *testing.T) {
    59  	var funcSD func(*Context)
    60  	const (
    61  		hdrCacheControl = "no-cache, no-store, must-revalidate"
    62  		hdrPragma       = "no-cache"
    63  		hdrExpires      = "0"
    64  	)
    65  
    66  	app := New()
    67  
    68  	ctx := Context{RequestCtx: &fasthttp.RequestCtx{}}
    69  
    70  	testProvideTempFile(t, func(file, dir string) {
    71  		ctx.Request.SetRequestURI(file)
    72  		funcSD = app.ServeDirNoCache(dir)
    73  		funcSD(&ctx)
    74  	})
    75  
    76  	if ctx.Response.StatusCode() != fasthttp.StatusOK {
    77  		t.Errorf("failed on directory serve (no cache) %d", ctx.Response.StatusCode())
    78  	}
    79  
    80  	hdrList := []struct {
    81  		name string
    82  		exp  string
    83  	}{
    84  		{"Cache-Control", hdrCacheControl},
    85  		{"Pragma", hdrPragma},
    86  		{"Expires", hdrExpires},
    87  	}
    88  	for _, hdr := range hdrList {
    89  		if act := string(ctx.Response.Header.Peek(hdr.name)); act != hdr.exp {
    90  			t.Errorf("wrong header '%s' check: %s, but %s expected", hdr.name, act, hdr.exp)
    91  		}
    92  	}
    93  }