mirror of https://github.com/jlelse/GoBlog
Simple blogging system written in Go
https://goblog.app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.7 KiB
116 lines
2.7 KiB
package main |
|
|
|
import ( |
|
"io" |
|
"net/http" |
|
"net/http/httptest" |
|
"path/filepath" |
|
"testing" |
|
|
|
"github.com/stretchr/testify/assert" |
|
"go.goblog.app/app/pkgs/contenttype" |
|
) |
|
|
|
func Test_errors(t *testing.T) { |
|
app := &goBlog{ |
|
cfg: &config{ |
|
Db: &configDb{ |
|
File: filepath.Join(t.TempDir(), "test.db"), |
|
}, |
|
Server: &configServer{ |
|
PublicAddress: "https://example.com", |
|
}, |
|
Blogs: map[string]*configBlog{ |
|
"en": { |
|
Lang: "en", |
|
}, |
|
}, |
|
DefaultBlog: "en", |
|
User: &configUser{}, |
|
}, |
|
} |
|
|
|
_ = app.initDatabase(false) |
|
defer app.db.close() |
|
app.initComponents(false) |
|
|
|
t.Run("Test 404, no HTML", func(t *testing.T) { |
|
h := http.HandlerFunc(app.serve404) |
|
|
|
req := httptest.NewRequest(http.MethodGet, "/abc", nil) |
|
req.Header.Set("Accept", contenttype.JSON) |
|
|
|
rec := httptest.NewRecorder() |
|
|
|
h(rec, req) |
|
|
|
res := rec.Result() |
|
resBody, _ := io.ReadAll(res.Body) |
|
_ = res.Body.Close() |
|
resString := string(resBody) |
|
|
|
assert.Equal(t, http.StatusNotFound, res.StatusCode) |
|
assert.Contains(t, resString, "not found") |
|
assert.Contains(t, res.Header.Get("Content-Type"), "text/plain") |
|
}) |
|
|
|
t.Run("Test 404, HTML", func(t *testing.T) { |
|
h := http.HandlerFunc(app.serve404) |
|
|
|
req := httptest.NewRequest(http.MethodGet, "/abc", nil) |
|
req.Header.Set("Accept", contenttype.HTML) |
|
|
|
rec := httptest.NewRecorder() |
|
|
|
h(rec, req) |
|
|
|
res := rec.Result() |
|
resBody, _ := io.ReadAll(res.Body) |
|
_ = res.Body.Close() |
|
resString := string(resBody) |
|
|
|
assert.Equal(t, http.StatusNotFound, res.StatusCode) |
|
assert.Contains(t, resString, "not found") |
|
assert.Contains(t, res.Header.Get("Content-Type"), contenttype.HTML) |
|
}) |
|
|
|
t.Run("Test Method Not Allowed, no HTML", func(t *testing.T) { |
|
h := http.HandlerFunc(app.serveNotAllowed) |
|
|
|
req := httptest.NewRequest(http.MethodGet, "/abc", nil) |
|
req.Header.Set("Accept", contenttype.JSON) |
|
|
|
rec := httptest.NewRecorder() |
|
|
|
h(rec, req) |
|
|
|
res := rec.Result() |
|
resBody, _ := io.ReadAll(res.Body) |
|
_ = res.Body.Close() |
|
resString := string(resBody) |
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, res.StatusCode) |
|
assert.Contains(t, resString, "Method Not Allowed") |
|
assert.Contains(t, res.Header.Get("Content-Type"), "text/plain") |
|
}) |
|
|
|
t.Run("Test Method Not Allowed", func(t *testing.T) { |
|
h := http.HandlerFunc(app.serveNotAllowed) |
|
|
|
req := httptest.NewRequest(http.MethodGet, "/abc", nil) |
|
req.Header.Set("Accept", contenttype.HTML) |
|
|
|
rec := httptest.NewRecorder() |
|
|
|
h(rec, req) |
|
|
|
res := rec.Result() |
|
resBody, _ := io.ReadAll(res.Body) |
|
_ = res.Body.Close() |
|
resString := string(resBody) |
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, res.StatusCode) |
|
assert.Contains(t, resString, "Method Not Allowed") |
|
assert.Contains(t, res.Header.Get("Content-Type"), contenttype.HTML) |
|
}) |
|
}
|
|
|