summaryrefslogtreecommitdiff
path: root/parser_test.go
blob: cf000170cd1871a1989645f7ed9c9e362a1bcc86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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()
		}
	}
}