diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-04-05 19:04:21 +0300 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-04-05 19:04:21 +0300 |
commit | b0ac2a7d3f7c48702120de4910cde34b7aee7bdd (patch) | |
tree | a8aaad3f80b5dcf51f2d3e8905d5da1172784c07 | |
parent | 5c830cca6dee4ad6f0025dda14fa345bb677e38c (diff) |
Fix: Content-Type of predefined 404 pagev0.2.2
-rw-r--r-- | 404.go | 12 | ||||
-rw-r--r-- | CHANGELOG | 3 | ||||
-rw-r--r-- | VERSION | 2 | ||||
-rw-r--r-- | app.go | 2 | ||||
-rw-r--r-- | domain.go | 4 | ||||
-rw-r--r-- | domain_test.go | 21 |
6 files changed, 32 insertions, 12 deletions
@@ -1,5 +1,10 @@ package main +import ( + "net/http" + "fmt" +) + const predefined404 = ` <!DOCTYPE html> <html> @@ -56,3 +61,10 @@ const predefined404 = ` </body> </html> ` + +func serve404(w http.ResponseWriter) { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.WriteHeader(http.StatusNotFound) + fmt.Fprintln(w, predefined404) +} @@ -1,3 +1,6 @@ +v 0.2.2 +- Fix predefined 404 page content-type + v 0.2.1 - Serve nice GitLab branded 404 page - Present user's error page for 404: put the 404.html in root of your pages @@ -1 +1 @@ -0.2.1 +0.2.2 @@ -56,7 +56,7 @@ func (a *theApp) serveContent(ww http.ResponseWriter, r *http.Request, https boo domain := a.domain(r.Host) if domain == nil { - http.NotFound(&w, r) + serve404(&w) return } @@ -169,7 +169,7 @@ func (d *domain) serveFromGroup(w http.ResponseWriter, r *http.Request) { } // Serve generic not found - http.Error(w, predefined404, http.StatusNotFound) + serve404(w) } func (d *domain) serveFromConfig(w http.ResponseWriter, r *http.Request) { @@ -184,7 +184,7 @@ func (d *domain) serveFromConfig(w http.ResponseWriter, r *http.Request) { } // Serve generic not found - http.Error(w, predefined404, http.StatusNotFound) + serve404(w) } func (d *domain) ensureCertificate() (*tls.Certificate, error) { diff --git a/domain_test.go b/domain_test.go index d1dca06e..56e032a4 100644 --- a/domain_test.go +++ b/domain_test.go @@ -5,6 +5,9 @@ import ( "net/http" "net/url" "testing" + "net/http/httptest" + "github.com/stretchr/testify/require" + "mime" ) func TestGroupServeHTTP(t *testing.T) { @@ -48,14 +51,16 @@ func TestDomainServeHTTP(t *testing.T) { assert.HTTPError(t, testDomain.ServeHTTP, "GET", "/not-existing-file", nil) } -func testHTTP404(t *testing.T, handler http.HandlerFunc, mode, url string, values url.Values, str interface{}) bool { - if !assert.HTTPError(t, handler, mode, url, values) { - return false - } - if !assert.HTTPBodyContains(t, handler, mode, url, values, str) { - return false - } - return true +func testHTTP404(t *testing.T, handler http.HandlerFunc, mode, url string, values url.Values, str interface{}) { + w := httptest.NewRecorder() + req, err := http.NewRequest(mode, url+"?"+values.Encode(), nil) + require.NoError(t, err) + handler(w, req) + + contentType, _, _ := mime.ParseMediaType(w.Header().Get("Content-Type")) + assert.Equal(t, http.StatusNotFound, w.Code, "HTTP status") + assert.Equal(t, "text/html", contentType, "Content-Type") + assert.Contains(t, w.Body.String(), str) } func TestGroup404ServeHTTP(t *testing.T) { |