Skip to content

Commit

Permalink
add more errorpages test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jan 28, 2018
1 parent 470f50c commit 6d940aa
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 16 deletions.
123 changes: 107 additions & 16 deletions http/errorpages/errorpages_test.go
Expand Up @@ -2,14 +2,16 @@ package errorpages


import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"testing" "testing"


"github.com/tj/assert"

"github.com/apex/up" "github.com/apex/up"
"github.com/apex/up/config" "github.com/apex/up/config"
"github.com/tj/assert"
) )


var server = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var server = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -24,6 +26,13 @@ var server = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return return
} }


if r.URL.Path == "/400/json" {
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{ "error": "bad_request" }`)
return
}

if r.URL.Path == "/500" { if r.URL.Path == "/500" {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return return
Expand All @@ -36,9 +45,9 @@ var server = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "World") fmt.Fprintf(w, "World")
}) })


func TestErrors_defaults(t *testing.T) { func TestErrors_templates(t *testing.T) {
os.Chdir("testdata") os.Chdir("testdata/templates")
defer os.Chdir("..") defer os.Chdir("../..")


c := &up.Config{Name: "app"} c := &up.Config{Name: "app"}
assert.NoError(t, c.Default(), "default") assert.NoError(t, c.Default(), "default")
Expand All @@ -51,7 +60,7 @@ func TestErrors_dir(t *testing.T) {
c := &up.Config{ c := &up.Config{
Name: "app", Name: "app",
ErrorPages: config.ErrorPages{ ErrorPages: config.ErrorPages{
Dir: "testdata", Dir: "testdata/templates",
}, },
} }


Expand All @@ -61,22 +70,72 @@ func TestErrors_dir(t *testing.T) {
test(t, c) test(t, c)
} }


func test(t *testing.T, c *up.Config) { func TestErrors_defaults(t *testing.T) {
os.Chdir("testdata/defaults")
defer os.Chdir("../..")

c := &up.Config{Name: "app"}
assert.NoError(t, c.Default(), "default")
assert.NoError(t, c.Validate(), "validate")

h, err := New(c, server)
assert.NoError(t, err, "init")

t.Run("200", nonError(h))
t.Run("accepts text/html", acceptsHTML(h))
t.Run("accepts text/*", acceptsText(h))
t.Run("does not accept html", doesNotAcceptHTML(h))
}

func TestErrors_disabled(t *testing.T) {
c := &up.Config{
Name: "app",
ErrorPages: config.ErrorPages{
Disable: true,
},
}

assert.NoError(t, c.Default(), "default")
assert.NoError(t, c.Validate(), "validate")

h, err := New(c, server) h, err := New(c, server)
assert.NoError(t, err, "init") assert.NoError(t, err, "init")


t.Run("200", func(t *testing.T) { t.Run("200", nonError(h))

t.Run("error", func(t *testing.T) {
res := httptest.NewRecorder() res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil) req := httptest.NewRequest("GET", "/404", nil)


h.ServeHTTP(res, req) h.ServeHTTP(res, req)


assert.Equal(t, 200, res.Code) assert.Equal(t, 404, res.Code)
assert.Equal(t, "bar", res.Header().Get("X-Foo")) assert.Equal(t, "text/plain; charset=utf-8", res.Header().Get("Content-Type"))
assert.Equal(t, "text/plain", res.Header().Get("Content-Type")) assert.Equal(t, "Not Found\n", res.Body.String())
assert.Equal(t, "Hello World", res.Body.String())
}) })


t.Run("json error", func(t *testing.T) {
res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/400/json", nil)
req.Header.Set("Accept", "application/json")

h.ServeHTTP(res, req)

assert.Equal(t, 400, res.Code)
assert.Equal(t, "application/json", res.Header().Get("Content-Type"))
assert.Equal(t, `{ "error": "bad_request" }`, res.Body.String())
})
}

func test(t *testing.T, c *up.Config) {
h, err := New(c, server)
assert.NoError(t, err, "init")

t.Run("200", nonError(h))
t.Run("accepts text/html", acceptsHTML(h))
t.Run("accepts text/*", acceptsText(h))
t.Run("does not accept html", doesNotAcceptHTML(h))

t.Run("exact", func(t *testing.T) { t.Run("exact", func(t *testing.T) {
res := httptest.NewRecorder() res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/404", nil) req := httptest.NewRequest("GET", "/404", nil)
Expand All @@ -101,8 +160,24 @@ func test(t *testing.T, c *up.Config) {
assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type")) assert.Equal(t, "text/html; charset=utf-8", res.Header().Get("Content-Type"))
assert.Equal(t, "500 – Internal Server Error\n", res.Body.String()) assert.Equal(t, "500 – Internal Server Error\n", res.Body.String())
}) })
}


t.Run("default text/html", func(t *testing.T) { func nonError(h http.Handler) func(t *testing.T) {
return func(t *testing.T) {
res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)

h.ServeHTTP(res, req)

assert.Equal(t, 200, res.Code)
assert.Equal(t, "bar", res.Header().Get("X-Foo"))
assert.Equal(t, "text/plain", res.Header().Get("Content-Type"))
assert.Equal(t, "Hello World", res.Body.String())
}
}

func acceptsHTML(h http.Handler) func(t *testing.T) {
return func(t *testing.T) {
res := httptest.NewRecorder() res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/400", nil) req := httptest.NewRequest("GET", "/400", nil)
req.Header.Set("Accept", "text/html") req.Header.Set("Accept", "text/html")
Expand All @@ -116,9 +191,11 @@ func test(t *testing.T, c *up.Config) {
assert.Contains(t, res.Body.String(), "<title>Bad Request – 400</title>", "title") assert.Contains(t, res.Body.String(), "<title>Bad Request – 400</title>", "title")
assert.Contains(t, res.Body.String(), `<span class="status">Bad Request</span>`, "status text") assert.Contains(t, res.Body.String(), `<span class="status">Bad Request</span>`, "status text")
assert.Contains(t, res.Body.String(), `<span class="code">400</span>`, "status code") assert.Contains(t, res.Body.String(), `<span class="code">400</span>`, "status code")
}) }
}


t.Run("default text/*", func(t *testing.T) { func acceptsText(h http.Handler) func(t *testing.T) {
return func(t *testing.T) {
res := httptest.NewRecorder() res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/400", nil) req := httptest.NewRequest("GET", "/400", nil)
req.Header.Set("Accept", "text/*") req.Header.Set("Accept", "text/*")
Expand All @@ -132,5 +209,19 @@ func test(t *testing.T, c *up.Config) {
assert.Contains(t, res.Body.String(), "<title>Bad Request – 400</title>", "title") assert.Contains(t, res.Body.String(), "<title>Bad Request – 400</title>", "title")
assert.Contains(t, res.Body.String(), `<span class="status">Bad Request</span>`, "status text") assert.Contains(t, res.Body.String(), `<span class="status">Bad Request</span>`, "status text")
assert.Contains(t, res.Body.String(), `<span class="code">400</span>`, "status code") assert.Contains(t, res.Body.String(), `<span class="code">400</span>`, "status code")
}) }
}

func doesNotAcceptHTML(h http.Handler) func(t *testing.T) {
return func(t *testing.T) {
res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/400/json", nil)
req.Header.Set("Accept", "application/json")

h.ServeHTTP(res, req)

assert.Equal(t, 400, res.Code)
assert.Equal(t, "application/json", res.Header().Get("Content-Type"))
assert.Equal(t, `{ "error": "bad_request" }`, res.Body.String())
}
} }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions http/errorpages/testdata/templates/index.html
@@ -0,0 +1 @@
Index HTML
3 changes: 3 additions & 0 deletions http/errorpages/testdata/templates/up.json
@@ -0,0 +1,3 @@
{
"name": "app"
}

0 comments on commit 6d940aa

Please sign in to comment.