Skip to content
Snippets Groups Projects
Commit b0ac2a7d authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Fix: Content-Type of predefined 404 page

parent 5c830cca
No related branches found
No related tags found
No related merge requests found
Pipeline #
package main
 
import (
"net/http"
"fmt"
)
const predefined404 = `
<!DOCTYPE html>
<html>
Loading
Loading
@@ -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)
}
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
Loading
Loading
0.2.1
0.2.2
Loading
Loading
@@ -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
}
 
Loading
Loading
Loading
Loading
@@ -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) {
Loading
Loading
@@ -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) {
Loading
Loading
Loading
Loading
@@ -5,6 +5,9 @@ import (
"net/http"
"net/url"
"testing"
"net/http/httptest"
"github.com/stretchr/testify/require"
"mime"
)
 
func TestGroupServeHTTP(t *testing.T) {
Loading
Loading
@@ -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) {
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment