github.com/motyar/up@v0.2.10/http/headers/headers_test.go (about)

     1  package headers
     2  
     3  import (
     4  	"net/http/httptest"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/tj/assert"
     9  	"github.com/apex/up"
    10  
    11  	"github.com/apex/up/http/static"
    12  	"github.com/apex/up/internal/header"
    13  )
    14  
    15  func TestHeaders(t *testing.T) {
    16  	os.Chdir("testdata")
    17  	defer os.Chdir("..")
    18  
    19  	c := &up.Config{
    20  		Headers: header.Rules{
    21  			"/*.css": {
    22  				"Cache-Control": "public, max-age=999999",
    23  			},
    24  		},
    25  	}
    26  
    27  	h, err := New(c, static.New(c))
    28  	assert.NoError(t, err, "init")
    29  
    30  	t.Run("mismatch", func(t *testing.T) {
    31  		res := httptest.NewRecorder()
    32  		req := httptest.NewRequest("GET", "/", nil)
    33  
    34  		h.ServeHTTP(res, req)
    35  
    36  		assert.Equal(t, 200, res.Code)
    37  		assert.Equal(t, "", res.Header().Get("Cache-Control"))
    38  		assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type"))
    39  		assert.Equal(t, "Index HTML\n", res.Body.String())
    40  	})
    41  
    42  	t.Run("matched exact", func(t *testing.T) {
    43  		res := httptest.NewRecorder()
    44  		req := httptest.NewRequest("GET", "/style.css", nil)
    45  
    46  		h.ServeHTTP(res, req)
    47  
    48  		assert.Equal(t, 200, res.Code)
    49  		assert.Equal(t, "public, max-age=999999", res.Header().Get("Cache-Control"))
    50  		assert.Equal(t, "css", res.Header().Get("X-Type"))
    51  		assert.Equal(t, "text/css; charset=utf-8", res.Header().Get("Content-Type"))
    52  		assert.Equal(t, "body { color: red }\n", res.Body.String())
    53  	})
    54  }