File: path_processor_test.go

package info (click to toggle)
golang-github-emicklei-go-restful 3.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 792 kB
  • sloc: makefile: 9; sh: 6
file content (113 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package restful

import (
	"testing"
)

func TestMatchesPath_OneParam(t *testing.T) {
	params := doExtractParams("/from/{source}", 2, "/from/here", t)
	if params["source"] != "here" {
		t.Errorf("parameter mismatch here")
	}
}

func TestMatchesPath_Slash(t *testing.T) {
	params := doExtractParams("/", 0, "/", t)
	if len(params) != 0 {
		t.Errorf("expected empty parameters")
	}
}

func TestMatchesPath_SlashNonVar(t *testing.T) {
	params := doExtractParams("/any", 1, "/any", t)
	if len(params) != 0 {
		t.Errorf("expected empty parameters")
	}
}

func TestMatchesPath_TwoVars(t *testing.T) {
	params := doExtractParams("/from/{source}/to/{destination}", 4, "/from/AMS/to/NY", t)
	if params["source"] != "AMS" {
		t.Errorf("parameter mismatch AMS")
	}
}

func TestMatchesPath_VarOnFront(t *testing.T) {
	params := doExtractParams("{what}/from/{source}/", 4, "who/from/SOS/", t) // slash is not removed
	if params["source"] != "SOS" {
		t.Errorf("parameter mismatch SOS")
	}
}

func TestExtractParameters_EmptyValue(t *testing.T) {
	params := doExtractParams("/fixed/{var}", 2, "/fixed/", t)
	if params["var"] != "" {
		t.Errorf("parameter mismatch var")
	}
}

func TestExtractParameters_Dot(t *testing.T) {
	params := doExtractParams("/fixed/{var}.foo", 2, "/fixed/barrr.foo", t)
	if params["var"] != "barrr" {
		t.Errorf("parameter mismatch var")
	}
}

func TestExtractParameters_Prefix(t *testing.T) {
	params := doExtractParams("/fixed/foo_{var}", 2, "/fixed/foo_barrr", t)
	if params["var"] != "barrr" {
		t.Errorf("parameter mismatch var")
	}
}

func TestExtractParameters_Suffix(t *testing.T) {
	params := doExtractParams("/fixed/{var}_foo", 2, "/fixed/barrr_foo", t)
	if params["var"] != "barrr" {
		t.Errorf("parameter mismatch var")
	}
}

func TestExtractParameters_Mixed(t *testing.T) {
	params := doExtractParams("/fixed/foo_{var}_bar", 2, "/fixed/foo_barrr_bar", t)
	if params["var"] != "barrr" {
		t.Errorf("parameter mismatch var")
	}
}

func TestExtractParameters_RegexAndCustomVerb(t *testing.T) {
	testCase := []struct {
		route     string
		size      int
		path      string
		checkList map[string]string
	}{
		{"/projects/{projectId}/users/{id:^prefix-}:custom", 4, "/projects/110/users/prefix-userId:custom", map[string]string{
			"projectId": "110",
			"id":        "prefix-userId"}},
		{"/projects/{projectId}/users/{id:*}", 4, "/projects/110/users/prefix-userId:custom", map[string]string{
			"projectId": "110",
			"id":        "prefix-userId:custom"}},
	}

	for idx, v := range testCase {
		params := doExtractParams(v.route, v.size, v.path, t)
		for k, val := range v.checkList {
			if params[k] != val {
				t.Errorf("[%v] params: %v mismatch, expected: %v, actual: %v", idx, k, v.checkList[k], params[k])
			}
		}
	}
}

func doExtractParams(routePath string, size int, urlPath string, t *testing.T) map[string]string {
	r := Route{Path: routePath}
	r.postBuild()
	if len(r.pathParts) != size {
		for i, each := range r.pathParts {
			t.Logf("%d:%q", i, each)
		}
		t.Fatalf("len not %v, but %v", size, len(r.pathParts))
	}
	pathProcessor := defaultPathProcessor{}
	return pathProcessor.ExtractParameters(&r, nil, urlPath)
}