github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/mux/router_test.gno (about)

     1  package mux
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestRouter_Render(t *testing.T) {
     9  	// Define handlers and route configuration
    10  	router := NewRouter()
    11  	router.HandleFunc("hello/{name}", func(res *ResponseWriter, req *Request) {
    12  		name := req.GetVar("name")
    13  		if name != "" {
    14  			res.Write("Hello, " + name + "!")
    15  		} else {
    16  			res.Write("Hello, world!")
    17  		}
    18  	})
    19  	router.HandleFunc("hi", func(res *ResponseWriter, req *Request) {
    20  		res.Write("Hi, earth!")
    21  	})
    22  
    23  	cases := []struct {
    24  		path           string
    25  		expectedOutput string
    26  	}{
    27  		{"hello/Alice", "Hello, Alice!"},
    28  		{"hi", "Hi, earth!"},
    29  		{"hello/Bob", "Hello, Bob!"},
    30  		// TODO: {"hello", "Hello, world!"},
    31  		// TODO: hello/, /hello, hello//Alice, hello/Alice/, hello/Alice/Bob, etc
    32  	}
    33  	for _, tt := range cases {
    34  		t.Run(tt.path, func(t *testing.T) {
    35  			output := router.Render(tt.path)
    36  			if output != tt.expectedOutput {
    37  				t.Errorf("Expected output %q, but got %q", tt.expectedOutput, output)
    38  			}
    39  		})
    40  	}
    41  }