File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -130,6 +130,18 @@ func (x *Context) RoutePattern() string {
130
130
return routePattern
131
131
}
132
132
133
+ // WithRouteContext returns the list of methods allowed for the current
134
+ // request, based on the current routing context.
135
+ func (x * Context ) AllowedMethods () []string {
136
+ result := make ([]string , 0 , len (x .methodsAllowed ))
137
+ for _ , method := range x .methodsAllowed {
138
+ if method := methodTypString (method ); method != "" {
139
+ result = append (result , method )
140
+ }
141
+ }
142
+ return result
143
+ }
144
+
133
145
// replaceWildcards takes a route pattern and recursively replaces all
134
146
// occurrences of "/*/" to "/".
135
147
func replaceWildcards (p string ) string {
Original file line number Diff line number Diff line change 1
1
package chi
2
2
3
- import "testing"
3
+ import (
4
+ "strings"
5
+ "testing"
6
+ )
4
7
5
8
// TestRoutePattern tests correct in-the-middle wildcard removals.
6
9
// If user organizes a router like this:
@@ -85,3 +88,26 @@ func TestRoutePattern(t *testing.T) {
85
88
t .Fatal ("unexpected route pattern for root: " + p )
86
89
}
87
90
}
91
+
92
+ func TestAllowedMethods (t * testing.T ) {
93
+ t .Run ("expected methods" , func (t * testing.T ) {
94
+ want := "GET HEAD"
95
+ rctx := & Context {
96
+ methodsAllowed : []methodTyp {mGET , mHEAD },
97
+ }
98
+ got := strings .Join (rctx .AllowedMethods (), " " )
99
+ if want != got {
100
+ t .Errorf ("Unexpected allowed methods: %s, want: %s" , got , want )
101
+ }
102
+ })
103
+ t .Run ("unexpected methods" , func (t * testing.T ) {
104
+ want := "GET HEAD"
105
+ rctx := & Context {
106
+ methodsAllowed : []methodTyp {mGET , mHEAD , 9000 },
107
+ }
108
+ got := strings .Join (rctx .AllowedMethods (), " " )
109
+ if want != got {
110
+ t .Errorf ("Unexpected allowed methods: %s, want: %s" , got , want )
111
+ }
112
+ })
113
+ }
You can’t perform that action at this time.
0 commit comments