Skip to content

Commit

Permalink
fix(tests): added unit tests for RoutingOption
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Dec 25, 2024
1 parent de668a4 commit e59aeee
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
8 changes: 5 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (app *App) handleFunc(pattern string, hf HandleFunc, opts []RoutingOption,
ctx := &Context{
req: req,
rw: w,
routing: *r,
Routing: *r,
app: app,
}

Expand Down Expand Up @@ -262,7 +262,7 @@ func (app *App) HandlePage(pattern string, viewName string, v Viewer) {
ctx := &Context{
req: req,
rw: w,
routing: *r,
Routing: *r,
app: app,
}

Expand Down Expand Up @@ -313,11 +313,13 @@ func (app *App) HandleFile(name string, v *FileViewer) {

app.routes[pat] = r

r.Viewers[v.MimeType()] = v

app.mux.HandleFunc(pat, func(w http.ResponseWriter, req *http.Request) {
ctx := &Context{
req: req,
rw: w,
routing: *r,
Routing: *r,
app: app,
}

Expand Down
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// the HTTP request, the HTTP response writer, and a Viewer instance for rendering.
// It is used to manage and pass request-scoped data throughout the request lifecycle.
type Context struct {
routing Routing
Routing Routing
app *App
rw http.ResponseWriter
req *http.Request
Expand Down Expand Up @@ -79,15 +79,15 @@ func (c *Context) View(items ...any) error {

} else {
for _, accept := range c.Accept() {
v, ok = c.routing.Viewers[accept]
v, ok = c.Routing.Viewers[accept]
if ok {
break
}
}
}

if !ok {
v = c.routing.Options.viewer
v = c.Routing.Options.viewer
}

if !c.writtenStatus {
Expand Down
20 changes: 17 additions & 3 deletions routing_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,34 @@ func (ro *RoutingOptions) Get(name string) any {
return ro.metadata[name]
}

func (ro *RoutingOptions) String(name string) string {
v, ok := ro.metadata[name]
func (ro *RoutingOptions) GetString(name string) string {
it, ok := ro.metadata[name]
if !ok {
return ""
}

s, ok := v.(string)
s, ok := it.(string)
if !ok {
return ""
}

return s
}

func (ro *RoutingOptions) GetInt(name string) int {
it, ok := ro.metadata[name]
if !ok {
return 0
}

v, ok := it.(int)
if !ok {
return 0
}

return v
}

type RoutingOption func(*RoutingOptions)

const (
Expand Down
63 changes: 63 additions & 0 deletions routing_option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package htmx

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

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

func TestRoutingOption(t *testing.T) {
mux := http.NewServeMux()
srv := httptest.NewServer(mux)
defer srv.Close()

app := New(WithMux(mux))

app.Use(func(next HandleFunc) HandleFunc {
return func(c *Context) error {
// check access with metadata
return next(c)
}
})

app.Get("/admin", func(c *Context) error {

data := map[string]any{
"s1": c.Routing.Options.GetString("s1"),
"i1": c.Routing.Options.GetInt("i1"),
"name": c.Routing.Options.Get(NavigationName),
"icon": c.Routing.Options.Get(NavigationIcon),
"access": c.Routing.Options.Get(NavigationAccess),
}

return c.View(data)
}, WithMetadata("s1", "v1"),
WithMetadata("i1", 1),
WithNavigation("admin", "ha-dash", "admin:view"))

app.Start()
defer app.Close()

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

buf, err := io.ReadAll(resp.Body)
require.NoError(t, err)
resp.Body.Close()

data := make(map[string]any)
err = json.Unmarshal(buf, &data)
require.NoError(t, err)

require.Equal(t, "v1", data["s1"])
require.EqualValues(t, 1, data["i1"])
require.Equal(t, "admin", data["name"])
require.Equal(t, "ha-dash", data["icon"])
require.Equal(t, "admin:view", data["access"])

}

0 comments on commit e59aeee

Please sign in to comment.