Skip to content

Commit

Permalink
fix(htmx): added TestHtmxWriteHeader (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi authored Jan 4, 2025
1 parent 3b5446c commit 56774b8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
7 changes: 5 additions & 2 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
### Changes
### Changed
-

### Fixes
### Fixed
-

### Added
-

### Tests
Tasks to complete before merging PR:
- [ ] Ensure unit tests are passing. If not run `make unit-test` to check for any regressions :clipboard:
Expand Down
70 changes: 70 additions & 0 deletions ext/htmx/htmx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package htmx

import (
"net/http"
"net/http/httptest"
"strconv"
"testing"

"github.com/stretchr/testify/require"
"github.com/yaitoo/xun"
)

func TestHtmxWriteHeader(t *testing.T) {

mux := http.NewServeMux()
srv := httptest.NewServer(mux)
defer srv.Close()

app := xun.New(xun.WithMux(mux))

app.Get("/string", func(c *xun.Context) error {
WriteHeader(c, HxTrigger, "string")
return c.View()
})

app.Get("/int", func(c *xun.Context) error {
WriteHeader(c, HxTrigger, 100)
return c.View()
})

app.Get("/header", func(c *xun.Context) error {
WriteHeader(c, HxTrigger, HxHeader[string]{"name": "message"})
return c.View()
})

client := &http.Client{}
var value string

req, err := http.NewRequest("GET", srv.URL+"/string", nil)
require.NoError(t, err)

resp, err := client.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, "string", resp.Header.Get(HxTrigger))

req, err = http.NewRequest("GET", srv.URL+"/int", nil)
require.NoError(t, err)

resp, err = client.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
value = resp.Header.Get(HxTrigger)
i, err := strconv.Atoi(value)
require.NoError(t, err)
require.Equal(t, 100, i)

var header HxHeader[string]
req, err = http.NewRequest("GET", srv.URL+"/header", nil)
require.NoError(t, err)

resp, err = client.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
value = resp.Header.Get(HxTrigger)
err = json.Unmarshal([]byte(value), &header)
require.NoError(t, err)
require.Equal(t, "message", header["name"])

}

0 comments on commit 56774b8

Please sign in to comment.