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

     1  package mux
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestRequest_GetVar(t *testing.T) {
     9  	cases := []struct {
    10  		handlerPath    string
    11  		reqPath        string
    12  		getVarKey      string
    13  		expectedOutput string
    14  	}{
    15  		{"users/{id}", "users/123", "id", "123"},
    16  		{"users/123", "users/123", "id", ""},
    17  		{"users/{id}", "users/123", "nonexistent", ""},
    18  		{"a/{b}/c/{d}", "a/42/c/1337", "b", "42"},
    19  		{"a/{b}/c/{d}", "a/42/c/1337", "d", "1337"},
    20  		{"{a}", "foo", "a", "foo"},
    21  		// TODO: wildcards: a/*/c
    22  		// TODO: multiple patterns per slashes: a/{b}-{c}/d
    23  	}
    24  
    25  	for _, tt := range cases {
    26  		name := fmt.Sprintf("%s-%s", tt.handlerPath, tt.reqPath)
    27  		t.Run(name, func(t *testing.T) {
    28  			req := &Request{
    29  				HandlerPath: tt.handlerPath,
    30  				Path:        tt.reqPath,
    31  			}
    32  
    33  			output := req.GetVar(tt.getVarKey)
    34  			if output != tt.expectedOutput {
    35  				t.Errorf("Expected '%q, but got %q", tt.expectedOutput, output)
    36  			}
    37  		})
    38  	}
    39  }