router_test.gno

3.50 Kb ยท 118 lines
  1package mux
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/demo/uassert"
  7)
  8
  9func TestRouter_Render(t *testing.T) {
 10	cases := []struct {
 11		label          string
 12		path           string
 13		expectedOutput string
 14		setupHandler   func(t *testing.T, r *Router)
 15	}{
 16		{
 17			label:          "route with named parameter",
 18			path:           "hello/Alice",
 19			expectedOutput: "Hello, Alice!",
 20			setupHandler: func(t *testing.T, r *Router) {
 21				r.HandleFunc("hello/{name}", func(rw *ResponseWriter, req *Request) {
 22					name := req.GetVar("name")
 23					uassert.Equal(t, "Alice", name)
 24					rw.Write("Hello, " + name + "!")
 25				})
 26			},
 27		},
 28		{
 29			label:          "static route",
 30			path:           "hi",
 31			expectedOutput: "Hi, earth!",
 32			setupHandler: func(t *testing.T, r *Router) {
 33				r.HandleFunc("hi", func(rw *ResponseWriter, req *Request) {
 34					uassert.Equal(t, req.Path, "hi")
 35					rw.Write("Hi, earth!")
 36				})
 37			},
 38		},
 39		{
 40			label:          "route with named parameter and query string",
 41			path:           "hello/foo/bar?foo=bar&baz",
 42			expectedOutput: "foo bar",
 43			setupHandler: func(t *testing.T, r *Router) {
 44				r.HandleFunc("hello/{key}/{val}", func(rw *ResponseWriter, req *Request) {
 45					key := req.GetVar("key")
 46					val := req.GetVar("val")
 47					uassert.Equal(t, "foo", key)
 48					uassert.Equal(t, "bar", val)
 49					uassert.Equal(t, "hello/foo/bar?foo=bar&baz", req.RawPath)
 50					uassert.Equal(t, "hello/foo/bar", req.Path)
 51					uassert.Equal(t, "bar", req.Query.Get("foo"))
 52					uassert.Empty(t, req.Query.Get("baz"))
 53					rw.Write(key + " " + val)
 54				})
 55			},
 56		},
 57		{
 58			// TODO: finalize how router should behave with double slash in path.
 59			label:          "double slash in nested route",
 60			path:           "a/foo//",
 61			expectedOutput: "test foo",
 62			setupHandler: func(t *testing.T, r *Router) {
 63				r.HandleFunc("a/{key}", func(rw *ResponseWriter, req *Request) {
 64					// Assert not called
 65					uassert.False(t, true, "unexpected handler called")
 66				})
 67
 68				r.HandleFunc("a/{key}/{val}/", func(rw *ResponseWriter, req *Request) {
 69					key := req.GetVar("key")
 70					val := req.GetVar("val")
 71					uassert.Equal(t, key, "foo")
 72					uassert.Empty(t, val)
 73					rw.Write("test " + key)
 74				})
 75			},
 76		},
 77		{
 78			label:          "wildcard in route",
 79			path:           "hello/Alice/Bob",
 80			expectedOutput: "Matched: Alice/Bob",
 81			setupHandler: func(t *testing.T, r *Router) {
 82				r.HandleFunc("hello/*", func(rw *ResponseWriter, req *Request) {
 83					path := req.GetVar("*")
 84					uassert.Equal(t, "Alice/Bob", path)
 85					uassert.Equal(t, "hello/Alice/Bob", req.Path)
 86					rw.Write("Matched: " + path)
 87				})
 88			},
 89		},
 90		{
 91			label:          "wildcard in route with query string",
 92			path:           "hello/Alice/Bob?foo=bar",
 93			expectedOutput: "Matched: Alice/Bob",
 94			setupHandler: func(t *testing.T, r *Router) {
 95				r.HandleFunc("hello/*", func(rw *ResponseWriter, req *Request) {
 96					path := req.GetVar("*")
 97					uassert.Equal(t, "Alice/Bob", path)
 98					uassert.Equal(t, "hello/Alice/Bob?foo=bar", req.RawPath)
 99					uassert.Equal(t, "hello/Alice/Bob", req.Path)
100					uassert.Equal(t, "bar", req.Query.Get("foo"))
101					rw.Write("Matched: " + path)
102				})
103			},
104		},
105		// TODO: {"hello", "Hello, world!"},
106		// TODO: hello/, /hello, hello//Alice, hello/Alice/, hello/Alice/Bob, etc
107	}
108	for _, tt := range cases {
109		t.Run(tt.label, func(t *testing.T) {
110			router := NewRouter()
111			tt.setupHandler(t, router)
112			output := router.Render(tt.path)
113			if output != tt.expectedOutput {
114				t.Errorf("Expected output %q, but got %q", tt.expectedOutput, output)
115			}
116		})
117	}
118}