GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — chi/context_test.go at master · go-chi/chi

Expand file tree

/

context_test.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
155 lines (138 loc) · 4.43 KB
/

context_test.go

Copy path

File metadata and controls

155 lines (138 loc) · 4.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package chi
import (
"context"
"testing"
)
// TestRoutePattern tests correct in-the-middle wildcard removals.
// If user organizes a router like this:
//
// (router.go)
//
// r.Route("/v1", func(r chi.Router) {
// r.Mount("/resources", resourcesController{}.Router())
// }
//
// (resources_controller.go)
//
// r.Route("/", func(r chi.Router) {
// r.Get("/{resource_id}", getResource())
// // other routes...
// }
//
// This test checks how the route pattern is calculated
// "/v1/resources/{resource_id}" (right)
// "/v1/resources/*/{resource_id}" (wrong)
func TestRoutePattern(t *testing.T) {
routePatterns := []string{
"/v1/*",
"/resources/*",
"/{resource_id}",
}
x := &Context{
RoutePatterns: routePatterns,
}
if p := x.RoutePattern(); p != "/v1/resources/{resource_id}" {
t.Fatal("unexpected route pattern: " + p)
}
x.RoutePatterns = []string{
"/v1/*",
"/resources/*",
// Additional wildcard, depending on the router structure of the user
"/*",
"/{resource_id}",
}
// Correctly removes in-the-middle wildcards instead of "/v1/resources/*/{resource_id}"
if p := x.RoutePattern(); p != "/v1/resources/{resource_id}" {
t.Fatal("unexpected route pattern: " + p)
}
x.RoutePatterns = []string{
"/v1/*",
"/resources/*",
// Even with many wildcards
"/*",
"/*",
"/*",
"/{resource_id}/*", // Keeping trailing wildcard
}
// Correctly removes in-the-middle wildcards instead of "/v1/resources/*/*/{resource_id}/*"
if p := x.RoutePattern(); p != "/v1/resources/{resource_id}/*" {
t.Fatal("unexpected route pattern: " + p)
}
x.RoutePatterns = []string{
"/v1/*",
"/resources/*",
// And respects asterisks as part of the paths
"/*special_path/*",
"/with_asterisks*/*",
"/{resource_id}",
}
// Correctly removes in-the-middle wildcards instead of "/v1/resourcesspecial_path/with_asterisks{resource_id}"
if p := x.RoutePattern(); p != "/v1/resources/*special_path/with_asterisks*/{resource_id}" {
t.Fatal("unexpected route pattern: " + p)
}
// Testing for the root route pattern
x.RoutePatterns = []string{"/"}
// It should just return "/" as the pattern
if p := x.RoutePattern(); p != "/" {
t.Fatal("unexpected route pattern for root: " + p)
}
// Testing empty route pattern for nil context
var nilContext *Context
if p := nilContext.RoutePattern(); p != "" {
t.Fatalf("unexpected non-empty route pattern for nil context: %q", p)
}
}
// TestReplaceWildcardsConsecutive ensures multiple consecutive wildcards are
// collapsed into a single slash.
func TestReplaceWildcardsConsecutive(t *testing.T) {
if p := replaceWildcards("/foo/*/*/*/bar"); p != "/foo/bar" {
t.Fatalf("unexpected wildcard replacement: %s", p)
}
if p := replaceWildcards("/foo/*/*/*/bar/*"); p != "/foo/bar/*" {
t.Fatalf("unexpected trailing wildcard behavior: %s", p)
}
}
func TestContext_Clone(t *testing.T) {
orig := &Context{
parentCtx: context.Background(),
RoutePatterns: []string{"/v1", "/resources/{id}"},
methodsAllowed: []methodTyp{mHEAD, mGET},
URLParams: RouteParams{
Keys: []string{"foo"},
Values: []string{"bar"},
},
routeParams: RouteParams{
Keys: []string{"id"},
Values: []string{"123"},
},
}
clone := orig.Clone()
orig.Reset()
orig.URLParams.Keys = append(orig.URLParams.Keys, "bar")
orig.URLParams.Values = append(orig.URLParams.Values, "baz")
orig.routeParams.Keys = append(orig.routeParams.Keys, "name")
orig.routeParams.Values = append(orig.routeParams.Values, "foxmulder")
orig.RoutePatterns = append(orig.RoutePatterns, "/mutated")
orig.methodsAllowed = append(orig.methodsAllowed, mPOST)
if got := clone.URLParams.Keys[0]; got != "foo" {
t.Fatalf("clone URLParams.Keys was corrupted, want %q got %q", "foo", got)
}
if got := clone.URLParams.Values[0]; got != "bar" {
t.Fatalf("clone URLParams.Values was corrupted, want %q got %q", "bar", got)
}
if got := clone.routeParams.Keys[0]; got != "id" {
t.Fatalf("clone routeParams.Keys was corrupted, want %q got %q", "id", got)
}
if got := clone.routeParams.Values[0]; got != "123" {
t.Fatalf("clone routeParams.Values was corrupted, want %q got %q", "123", got)
}
if got := clone.RoutePatterns[0]; got != "/v1" {
t.Fatalf("clone RoutePatterns[0] was corrupted, want %q got %q", "/v1", got)
}
if got := clone.methodsAllowed[0]; got != mHEAD {
t.Fatalf("clone methodsAllowed[0] was corrupted, want %d got %d", mHEAD, got)
}
if clone.parentCtx != nil {
t.Fatalf("clone parentCtx should be detached, got %v", clone.parentCtx)
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.