From 3f53ba50f2e562b5a2bc97442266b5e7acd585eb Mon Sep 17 00:00:00 2001 From: Eltrac Date: Fri, 28 Nov 2025 23:13:23 +0800 Subject: feat(router, context)!: support query string also move path split logic to parser.go --- parser_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 parser_test.go (limited to 'parser_test.go') diff --git a/parser_test.go b/parser_test.go new file mode 100644 index 0000000..c63f758 --- /dev/null +++ b/parser_test.go @@ -0,0 +1,79 @@ +package growel + +import ( + "fmt" + "reflect" + "testing" +) + +func TestRouterSplitPath(t *testing.T) { + tests := []struct { + input string + want []string + }{ + {"/", []string{""}}, + {"/hello", []string{"hello"}}, + {"/hello/world", []string{"hello", "world"}}, + {"//hello//world//", []string{"hello", "", "world"}}, + {"/hello?world=true", []string{"hello?world=true"}}, + } + + for _, tt := range tests { + got := splitPath(tt.input) + if len(got) != len(tt.want) { + t.Fatalf("splitPath(%q) length mismatch: got %v want %v", tt.input, got, tt.want) + } + for i := range got { + if got[i] != tt.want[i] { + t.Fatalf("splitPath(%q) mismatch: got %v want %v", tt.input, got, tt.want) + } + } + } +} + +func TestExtractQueryStr(t *testing.T) { + tests := []struct { + input string + want1 string + want2 string + }{ + {"", "", ""}, + {"hello", "hello", ""}, + {"user/23/", "user/23/", ""}, + {"api?str=123&yrs=23", "api", "str=123&yrs=23"}, + {"w?st?r", "w", "st"}, + {"w?str", "w", "str"}, + } + + for _, tt := range tests { + got1, got2 := extractQueryStr(tt.input) + if got1 != tt.want1 && got2 != tt.want2 { + fmt.Printf("want1: %s, got1: %s\n", tt.want1, got1) + fmt.Printf("want2: %s, got2: %s\n", tt.want2, got2) + t.Fail() + } + } +} + +func TestParseQueryStr(t *testing.T) { + tests := []struct { + input string + want map[string]string + }{ + {"str=123&yrs=23", map[string]string{ + "str": "123", + "yrs": "23", + }}, + {"str=abc", map[string]string{ + "str": "abc", + }}, + } + + for _, tt := range tests { + got := parseQueryStr(tt.input) + if !reflect.DeepEqual(got, tt.want) { + fmt.Printf("want: %s, got: %s\n", tt.want, got) + t.Fail() + } + } +} -- cgit v1.2.3