ch14.5
This commit is contained in:
@@ -18,3 +18,61 @@ func TestPing(t *testing.T) {
|
||||
assert.Equal(t, statusCode, http.StatusOK)
|
||||
assert.Equal(t, body, "OK")
|
||||
}
|
||||
|
||||
func TestSnippetView(t *testing.T) {
|
||||
app := newTestApplication(t)
|
||||
|
||||
ts := newTestServer(t, app.routes())
|
||||
defer ts.Close()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
urlPath string
|
||||
wantCode int
|
||||
wantBody string
|
||||
}{
|
||||
{
|
||||
name: "Valid ID",
|
||||
urlPath: "/snippet/view/1",
|
||||
wantCode: http.StatusOK,
|
||||
wantBody: "An old silent pond...",
|
||||
},
|
||||
{
|
||||
name: "Non-existend ID",
|
||||
urlPath: "/snippet/view/2",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "Negative ID",
|
||||
urlPath: "/snippet/view/-1",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "Decimal ID",
|
||||
urlPath: "/snippet/view/1.23",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "String ID",
|
||||
urlPath: "/snippet/view/foo",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
{
|
||||
name: "Empty ID",
|
||||
urlPath: "/snippet/view/",
|
||||
wantCode: http.StatusNotFound,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
code, _, body := ts.get(t, tt.urlPath)
|
||||
|
||||
assert.Equal(t, code, tt.wantCode)
|
||||
|
||||
if tt.wantBody != "" {
|
||||
assert.StringContains(t, body, tt.wantBody)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ import (
|
||||
type application struct {
|
||||
errorLog *log.Logger
|
||||
infoLog *log.Logger
|
||||
snippets *models.SnippetModel
|
||||
users *models.UserModel
|
||||
snippets models.SnippetModelInterface
|
||||
users models.UserModelInterface
|
||||
templateCache map[string]*template.Template
|
||||
formDecoder *form.Decoder
|
||||
sessionManager *scs.SessionManager
|
||||
|
||||
@@ -8,12 +8,33 @@ import (
|
||||
"net/http/cookiejar"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.local.lab/Lbenedar/snippetbox/internal/models/mocks"
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/go-playground/form/v4"
|
||||
)
|
||||
|
||||
func newTestApplication(t *testing.T) *application {
|
||||
templateCache, err := newTemplateCache()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
formDecoder := form.NewDecoder()
|
||||
|
||||
sessionManager := scs.New()
|
||||
sessionManager.Lifetime = 12 * time.Hour
|
||||
sessionManager.Cookie.Secure = true
|
||||
|
||||
return &application{
|
||||
errorLog: log.New(io.Discard, "", 0),
|
||||
infoLog: log.New(io.Discard, "", 0),
|
||||
errorLog: log.New(io.Discard, "", 0),
|
||||
infoLog: log.New(io.Discard, "", 0),
|
||||
snippets: &mocks.SnippetModel{},
|
||||
users: &mocks.UserModel{},
|
||||
templateCache: templateCache,
|
||||
formDecoder: formDecoder,
|
||||
sessionManager: sessionManager,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user