Added tests or htmx package.
This commit is contained in:
parent
6f50552a15
commit
1dd8eb0cd7
2 changed files with 56 additions and 0 deletions
|
|
@ -22,6 +22,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Request contains data that HTMX provides during requests
|
||||||
Request struct {
|
Request struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Boosted bool
|
Boosted bool
|
||||||
|
|
@ -31,6 +32,7 @@ type (
|
||||||
Prompt string
|
Prompt string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Response contain data that the server can communicate back to HTMX
|
||||||
Response struct {
|
Response struct {
|
||||||
Push string
|
Push string
|
||||||
Redirect string
|
Redirect string
|
||||||
|
|
@ -42,6 +44,7 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetRequest extracts HTMX data from the request
|
||||||
func GetRequest(ctx echo.Context) Request {
|
func GetRequest(ctx echo.Context) Request {
|
||||||
return Request{
|
return Request{
|
||||||
Enabled: ctx.Request().Header.Get(HeaderRequest) == "true",
|
Enabled: ctx.Request().Header.Get(HeaderRequest) == "true",
|
||||||
|
|
@ -53,6 +56,7 @@ func GetRequest(ctx echo.Context) Request {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply applies data from a Response to a server response
|
||||||
func (r Response) Apply(ctx echo.Context) {
|
func (r Response) Apply(ctx echo.Context) {
|
||||||
if r.Push != "" {
|
if r.Push != "" {
|
||||||
ctx.Response().Header().Set(HeaderPush, r.Push)
|
ctx.Response().Header().Set(HeaderPush, r.Push)
|
||||||
|
|
|
||||||
52
htmx/htmx_test.go
Normal file
52
htmx/htmx_test.go
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
package htmx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"goweb/tests"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetRequest(t *testing.T) {
|
||||||
|
ctx, _ := tests.NewContext(echo.New(), "/")
|
||||||
|
ctx.Request().Header.Set(HeaderRequest, "true")
|
||||||
|
ctx.Request().Header.Set(HeaderBoosted, "true")
|
||||||
|
ctx.Request().Header.Set(HeaderTrigger, "a")
|
||||||
|
ctx.Request().Header.Set(HeaderTriggerName, "b")
|
||||||
|
ctx.Request().Header.Set(HeaderTarget, "c")
|
||||||
|
ctx.Request().Header.Set(HeaderPrompt, "d")
|
||||||
|
|
||||||
|
r := GetRequest(ctx)
|
||||||
|
assert.Equal(t, true, r.Enabled)
|
||||||
|
assert.Equal(t, true, r.Boosted)
|
||||||
|
assert.Equal(t, "a", r.Trigger)
|
||||||
|
assert.Equal(t, "b", r.TriggerName)
|
||||||
|
assert.Equal(t, "c", r.Target)
|
||||||
|
assert.Equal(t, "d", r.Prompt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResponse_Apply(t *testing.T) {
|
||||||
|
ctx, _ := tests.NewContext(echo.New(), "/")
|
||||||
|
r := Response{
|
||||||
|
Push: "a",
|
||||||
|
Redirect: "b",
|
||||||
|
Refresh: true,
|
||||||
|
Trigger: "c",
|
||||||
|
TriggerAfterSwap: "d",
|
||||||
|
TriggerAfterSettle: "e",
|
||||||
|
NoContent: true,
|
||||||
|
}
|
||||||
|
r.Apply(ctx)
|
||||||
|
|
||||||
|
assert.Equal(t, "a", ctx.Response().Header().Get(HeaderPush))
|
||||||
|
assert.Equal(t, "b", ctx.Response().Header().Get(HeaderRedirect))
|
||||||
|
assert.Equal(t, "true", ctx.Response().Header().Get(HeaderRefresh))
|
||||||
|
assert.Equal(t, "c", ctx.Response().Header().Get(HeaderTrigger))
|
||||||
|
assert.Equal(t, "d", ctx.Response().Header().Get(HeaderTriggerAfterSwap))
|
||||||
|
assert.Equal(t, "e", ctx.Response().Header().Get(HeaderTriggerAfterSettle))
|
||||||
|
assert.Equal(t, http.StatusNoContent, ctx.Response().Status)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue