github.com/orteth01/up@v0.2.0/internal/errorpage/errorpage_test.go (about)

     1  package errorpage
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/tj/assert"
     8  )
     9  
    10  // load pages from dir.
    11  func load(t testing.TB, dir string) Pages {
    12  	dir = filepath.Join("testdata", dir)
    13  	pages, err := Load(dir)
    14  	assert.NoError(t, err, "load")
    15  	return pages
    16  }
    17  
    18  func TestPages_precedence(t *testing.T) {
    19  	pages := load(t, ".")
    20  
    21  	t.Run("code 500 match exact", func(t *testing.T) {
    22  		p := pages.Match(500)
    23  		assert.NotNil(t, p, "no match")
    24  
    25  		html, err := p.Render(nil)
    26  		assert.NoError(t, err)
    27  
    28  		assert.Equal(t, "500 page.\n", html)
    29  	})
    30  
    31  	t.Run("code 404 match exact", func(t *testing.T) {
    32  		p := pages.Match(404)
    33  		assert.NotNil(t, p, "no match")
    34  
    35  		html, err := p.Render(nil)
    36  		assert.NoError(t, err)
    37  
    38  		assert.Equal(t, "404 page.\n", html)
    39  	})
    40  
    41  	t.Run("code 200 match exact", func(t *testing.T) {
    42  		p := pages.Match(200)
    43  		assert.NotNil(t, p, "no match")
    44  
    45  		html, err := p.Render(nil)
    46  		assert.NoError(t, err)
    47  
    48  		assert.Equal(t, "200 page.\n", html)
    49  	})
    50  
    51  	t.Run("code 403 match range", func(t *testing.T) {
    52  		p := pages.Match(403)
    53  		assert.NotNil(t, p, "no match")
    54  
    55  		html, err := p.Render(nil)
    56  		assert.NoError(t, err)
    57  
    58  		assert.Equal(t, "4xx page.\n", html)
    59  	})
    60  
    61  	t.Run("502 match global", func(t *testing.T) {
    62  		p := pages.Match(502)
    63  		assert.NotNil(t, p, "no match")
    64  
    65  		data := struct {
    66  			StatusText string
    67  			StatusCode int
    68  		}{"Bad Gateway", 502}
    69  
    70  		html, err := p.Render(data)
    71  		assert.NoError(t, err)
    72  
    73  		assert.Equal(t, "Bad Gateway - 502.\n", html)
    74  	})
    75  }