From dda008c1f0b37219016e0814042acc6f6740aab7 Mon Sep 17 00:00:00 2001 From: Lz Date: Wed, 25 Dec 2024 10:56:12 +0800 Subject: [PATCH] fix(tests): added unit tests for RoutingOption --- routing_option_test.go | 52 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/routing_option_test.go b/routing_option_test.go index ae0cd2e..ca8d78d 100644 --- a/routing_option_test.go +++ b/routing_option_test.go @@ -23,6 +23,20 @@ func TestRoutingOption(t *testing.T) { } }) + app.Get("/", 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)) + app.Get("/admin", func(c *Context) error { data := map[string]any{ @@ -34,14 +48,13 @@ func TestRoutingOption(t *testing.T) { } return c.View(data) - }, WithMetadata("s1", "v1"), - WithMetadata("i1", 1), + }, WithMetadata("i1", ""), WithNavigation("admin", "ha-dash", "admin:view")) app.Start() defer app.Close() - req, err := http.NewRequest("GET", srv.URL+"/admin", nil) + req, err := http.NewRequest("GET", srv.URL+"/", nil) require.NoError(t, err) resp, err := client.Do(req) require.NoError(t, err) @@ -50,14 +63,33 @@ func TestRoutingOption(t *testing.T) { require.NoError(t, err) resp.Body.Close() - data := make(map[string]any) - err = json.Unmarshal(buf, &data) + d1 := make(map[string]any) + err = json.Unmarshal(buf, &d1) + require.NoError(t, err) + + require.Equal(t, "v1", d1["s1"]) + require.EqualValues(t, 1, d1["i1"]) + require.Nil(t, d1["name"]) + require.Nil(t, d1["icon"]) + require.Nil(t, d1["access"]) + + 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() + + d2 := make(map[string]any) + err = json.Unmarshal(buf, &d2) 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"]) + require.Equal(t, "", d2["s1"]) + require.EqualValues(t, 0, d2["i1"]) + require.Equal(t, "admin", d2["name"]) + require.Equal(t, "ha-dash", d2["icon"]) + require.Equal(t, "admin:view", d2["access"]) }