package main import ( "net/http" "net/url" "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") } 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) } }) } } func TestUserSignup(t *testing.T) { app := newTestApplication(t) ts := newTestServer(t, app.routes()) defer ts.Close() _, _, body := ts.get(t, "/user/signup") validCsrfToken := extractCSRFToken(t, body) t.Logf("CsrfToken: %s", validCsrfToken) const ( validName = "Bob" validPassword = "validPa$$word" validEmail = "bob@example.com" formTag = "