diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-28 23:13:23 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-28 23:13:23 +0800 |
| commit | 3f53ba50f2e562b5a2bc97442266b5e7acd585eb (patch) | |
| tree | ab29b0db71bb1661af0502b3b3442fb0f84a0590 /parser_test.go | |
| parent | accb1bae86a3547f16dcc448f92ec73a493dfd85 (diff) | |
feat(router, context)!: support query string also move path split logic to
parser.go
Diffstat (limited to 'parser_test.go')
| -rw-r--r-- | parser_test.go | 79 |
1 files changed, 79 insertions, 0 deletions
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() + } + } +} |
