Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

request_test.gno

1.38 Kb · 48 lines
 1package mux
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7	"gno.land/p/nt/ufmt/v0"
 8)
 9
10func TestRequest_GetVar(t *testing.T) {
11	cases := []struct {
12		handlerPath    string
13		reqPath        string
14		getVarKey      string
15		expectedOutput string
16	}{
17
18		{"users/{id}", "users/123", "id", "123"},
19		{"users/123", "users/123", "id", ""},
20		{"users/{id}", "users/123", "nonexistent", ""},
21		{"users/{userId}/posts/{postId}", "users/123/posts/456", "userId", "123"},
22		{"users/{userId}/posts/{postId}", "users/123/posts/456", "postId", "456"},
23
24		// Wildcards
25		{"*", "users/123", "*", "users/123"},
26		{"*", "users/123/posts/456", "*", "users/123/posts/456"},
27		{"*", "users/123/posts/456/comments/789", "*", "users/123/posts/456/comments/789"},
28		{"users/*", "users/john/posts", "*", "john/posts"},
29		{"users/*/comments", "users/jane/comments", "*", "jane/comments"},
30		{"api/*/posts/*", "api/v1/posts/123", "*", "v1/posts/123"},
31
32		// wildcards and parameters
33		{"api/{version}/*", "api/v1/user/settings", "version", "v1"},
34	}
35	for _, tt := range cases {
36		name := ufmt.Sprintf("%s-%s", tt.handlerPath, tt.reqPath)
37		t.Run(name, func(t *testing.T) {
38			req := &Request{
39				HandlerPath: tt.handlerPath,
40				Path:        tt.reqPath,
41			}
42			output := req.GetVar(tt.getVarKey)
43			uassert.Equal(t, tt.expectedOutput, output,
44				"handler: %q, path: %q, key: %q",
45				tt.handlerPath, tt.reqPath, tt.getVarKey)
46		})
47	}
48}