package main import ( "net/http" "testing" "git.runcible.io/learning/ratchet/internal/assert" ) // func testingLogger() *slog.Logger { // return slog.New(slog.NewTextHandler(io.Discard, nil)) // } // WITHOUT testutils_test.go helpers // func TestPingIntegration(t *testing.T) { // rs := server.NewRatchetApp(testingLogger(), nil, nil, nil) // // We then use the httptest.NewTLSServer() function to create a new test // // server, passing in the value returned by our app.routes() method as the // // handler for the server. This starts up a HTTPS server which listens on a // // randomly-chosen port of your local machine for the duration of the test. // // Notice that we defer a call to ts.Close() so that the server is shutdown // // when the test finishes. // ts := httptest.NewTLSServer(rs) // defer ts.Close() // resp, err := ts.Client().Get(ts.URL + "/ping") // if err != nil { // t.Fatal(err) // } // assert.Equal(t, resp.StatusCode, http.StatusOK) // defer resp.Body.Close() // body, err := io.ReadAll(resp.Body) // if err != nil { // t.Fatal(err) // } // body = bytes.TrimSpace(body) // assert.Equal(t, string(body), "OK") // } func TestPingIntegration(t *testing.T) { // Tests marked using t.Parallel() will be run in parallel with — and only with — other parallel tests. // By default, the maximum number of tests that will be run simultaneously is the current value of // GOMAXPROCS. You can override this by setting a specific value via the -parallel flag. t.Parallel() app := newTestApplication(t) ts := newTestServer(t, app.Routes()) defer ts.Close() code, _, body := ts.get(t, "/ping") assert.Equal(t, code, http.StatusOK) assert.Equal(t, body, "OK") } func TestSnippetView(t *testing.T) { t.Parallel() 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: 200, wantBody: "Hello golang mocking", }, { name: "Nonexistent ID", urlPath: "/snippet/view/2", wantCode: 404, }, { name: "Negative ID", urlPath: "/snippet/view/-1", wantCode: 404, }, { name: "Decimal ID", urlPath: "/snippet/view/1.23", wantCode: 404, }, { name: "string ID", urlPath: "/snippet/view/foo", wantCode: 404, }, { name: "emptry ID", urlPath: "/snippet/view/", wantCode: 404, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { code, _, body := ts.get(t, test.urlPath) assert.Equal(t, code, test.wantCode) assert.StringContains(t, body, test.wantBody) }) } }