ch14.2
This commit is contained in:
@@ -236,3 +236,7 @@ func (app *application) userLogin(w http.ResponseWriter, r *http.Request) {
|
||||
data.Form = userLoginForm{}
|
||||
app.render(w, http.StatusOK, "login.tmpl", data)
|
||||
}
|
||||
|
||||
func ping(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("OK"))
|
||||
}
|
||||
|
||||
33
cmd/web/handlers_test.go
Normal file
33
cmd/web/handlers_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitea.local.lab/Lbenedar/snippetbox/internal/assert"
|
||||
)
|
||||
|
||||
func TestPing(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ping(rr, r)
|
||||
|
||||
rs := rr.Result()
|
||||
|
||||
assert.Equal(t, rs.StatusCode, http.StatusOK)
|
||||
defer rs.Body.Close()
|
||||
body, err := io.ReadAll(rs.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bytes.TrimSpace(body)
|
||||
assert.Equal(t, string(body), "OK")
|
||||
}
|
||||
@@ -13,8 +13,8 @@ func secureHeaders(next http.Handler) http.Handler {
|
||||
w.Header().Set("Content-Security-Policy",
|
||||
"default-src 'self'; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com")
|
||||
w.Header().Set("Referrer-Policy", "origin-when-cross-origin")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("X-Frame-Options", "deny")
|
||||
w.Header().Set("X-Content-Type-Options", "deny")
|
||||
w.Header().Set("X-Frame-Options", "nosniff")
|
||||
w.Header().Set("X-XSS-Protection", "0")
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
46
cmd/web/middleware_test.go
Normal file
46
cmd/web/middleware_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitea.local.lab/Lbenedar/snippetbox/internal/assert"
|
||||
)
|
||||
|
||||
func TestSecureHeaders(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("OK"))
|
||||
})
|
||||
secureHeaders(next).ServeHTTP(rr, r)
|
||||
|
||||
rs := rr.Result()
|
||||
|
||||
expectedValue := "default-src 'self'; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com"
|
||||
assert.Equal(t, rs.Header.Get("Content-Security-Policy"), expectedValue)
|
||||
|
||||
expectedValue = "nosniff"
|
||||
assert.Equal(t, rs.Header.Get("X-Frame-Options"), expectedValue)
|
||||
|
||||
expectedValue = "0"
|
||||
assert.Equal(t, rs.Header.Get("X-XSS-Protection"), expectedValue)
|
||||
|
||||
assert.Equal(t, rs.StatusCode, http.StatusOK)
|
||||
|
||||
defer rs.Body.Close()
|
||||
body, err := io.ReadAll(rs.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bytes.TrimSpace(body)
|
||||
|
||||
assert.Equal(t, string(body), "OK")
|
||||
}
|
||||
Reference in New Issue
Block a user