Compare commits
2 Commits
c3294de4c6
...
ec5ef9a5c1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec5ef9a5c1 | ||
|
|
79d465bd7b |
@@ -236,3 +236,7 @@ func (app *application) userLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
data.Form = userLoginForm{}
|
data.Form = userLoginForm{}
|
||||||
app.render(w, http.StatusOK, "login.tmpl", data)
|
app.render(w, http.StatusOK, "login.tmpl", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ping(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Write([]byte("OK"))
|
||||||
|
}
|
||||||
|
|||||||
20
cmd/web/handlers_test.go
Normal file
20
cmd/web/handlers_test.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.local.lab/Lbenedar/snippetbox/internal/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPing(t *testing.T) {
|
||||||
|
app := newTestApplication(t)
|
||||||
|
|
||||||
|
ts := newTestServer(t, app.routes())
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
statusCode, _, body := ts.get(t, "/ping")
|
||||||
|
|
||||||
|
assert.Equal(t, statusCode, http.StatusOK)
|
||||||
|
assert.Equal(t, body, "OK")
|
||||||
|
}
|
||||||
@@ -13,8 +13,8 @@ func secureHeaders(next http.Handler) http.Handler {
|
|||||||
w.Header().Set("Content-Security-Policy",
|
w.Header().Set("Content-Security-Policy",
|
||||||
"default-src 'self'; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com")
|
"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("Referrer-Policy", "origin-when-cross-origin")
|
||||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
w.Header().Set("X-Content-Type-Options", "deny")
|
||||||
w.Header().Set("X-Frame-Options", "deny")
|
w.Header().Set("X-Frame-Options", "nosniff")
|
||||||
w.Header().Set("X-XSS-Protection", "0")
|
w.Header().Set("X-XSS-Protection", "0")
|
||||||
|
|
||||||
next.ServeHTTP(w, r)
|
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")
|
||||||
|
}
|
||||||
@@ -19,6 +19,8 @@ func (app *application) routes() http.Handler {
|
|||||||
fileServer := http.FileServer(http.FS(ui.Files))
|
fileServer := http.FileServer(http.FS(ui.Files))
|
||||||
router.Handler(http.MethodGet, "/static/*filepath", fileServer)
|
router.Handler(http.MethodGet, "/static/*filepath", fileServer)
|
||||||
|
|
||||||
|
router.HandlerFunc(http.MethodGet, "/ping", ping)
|
||||||
|
|
||||||
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
|
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
|
||||||
|
|
||||||
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
|
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
|
||||||
|
|||||||
53
cmd/web/testutils_test.go
Normal file
53
cmd/web/testutils_test.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/cookiejar"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTestApplication(t *testing.T) *application {
|
||||||
|
return &application{
|
||||||
|
errorLog: log.New(io.Discard, "", 0),
|
||||||
|
infoLog: log.New(io.Discard, "", 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type testServer struct {
|
||||||
|
*httptest.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestServer(t *testing.T, h http.Handler) *testServer {
|
||||||
|
ts := httptest.NewTLSServer(h)
|
||||||
|
jar, err := cookiejar.New(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.Client().Jar = jar
|
||||||
|
|
||||||
|
ts.Client().CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
}
|
||||||
|
return &testServer{ts}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *testServer) get(t *testing.T, urlPath string) (int, http.Header, string) {
|
||||||
|
rs, err := ts.Client().Get(ts.URL + urlPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rs.Body.Close()
|
||||||
|
body, err := io.ReadAll(rs.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
bytes.TrimSpace(body)
|
||||||
|
|
||||||
|
return rs.StatusCode, rs.Header, string(body)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package assert
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Equal[T comparable](t *testing.T, actual, expected T) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf("got: %v; want: %v", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user