|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "gofr.dev/pkg/gofr/testutil" |
| 15 | +) |
| 16 | + |
| 17 | +func TestListHandler(t *testing.T) { |
| 18 | + configs := testutil.NewServerConfigs(t) |
| 19 | + c := &http.Client{} |
| 20 | + |
| 21 | + go main() |
| 22 | + time.Sleep(100 * time.Millisecond) |
| 23 | + |
| 24 | + // Make a GET request to the /list endpoint |
| 25 | + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, |
| 26 | + configs.HTTPHost+"/list", http.NoBody) |
| 27 | + resp, err := c.Do(req) |
| 28 | + |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + defer resp.Body.Close() |
| 32 | + |
| 33 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 34 | + assert.Equal(t, "text/html", resp.Header.Get("Content-Type")) |
| 35 | + |
| 36 | + // Read response body |
| 37 | + body, err := io.ReadAll(resp.Body) |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + bodyStr := strings.Join(strings.Fields(string(body)), " ") |
| 41 | + |
| 42 | + // Validate key HTML elements using strings.Contains |
| 43 | + assert.Contains(t, bodyStr, "<h2>My TODO list</h2>", "Header text missing") |
| 44 | + |
| 45 | + expectedItems := "<li>Expand on Gofr documentation </li> <li class=\"done\">" + |
| 46 | + "Add more examples</li> <li>Write some articles</li>" |
| 47 | + |
| 48 | + assert.Contains(t, bodyStr, expectedItems, "Missing TODO items") |
| 49 | + |
| 50 | + // Validate stylesheet link |
| 51 | + assert.Contains(t, bodyStr, `<link rel="stylesheet" href="style.css">`, "Stylesheet link missing") |
| 52 | +} |
0 commit comments