Skip to content

Commit f121b95

Browse files
committed
Merge branch 'development' of github.com:gofr-dev/gofr into release/v1.35.0
2 parents 30f8430 + 8458cb7 commit f121b95

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

docs/advanced-guide/overriding-default/page.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Response example:
7272
```
7373

7474
## Rendering Templates
75-
GoFr allows rendering HTML templates in handlers using the response.Template type.
75+
GoFr allows rendering HTML/HTMX templates in handlers using the response.Template type.
7676

7777
### Example
7878
```go

examples/using-html-template/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type TodoPageData struct {
2222
Todos []Todo
2323
}
2424

25-
func listHandler(c *gofr.Context) (any, error) {
25+
func listHandler(*gofr.Context) (any, error) {
2626
// Get data from somewhere
2727
data := TodoPageData{
2828
PageTitle: "My TODO list",
@@ -33,5 +33,5 @@ func listHandler(c *gofr.Context) (any, error) {
3333
},
3434
}
3535

36-
return response.Template{data, "todo.html"}, nil
36+
return response.Template{Data: data, Name: "todo.html"}, nil
3737
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)