|
1 | 1 | package chi
|
2 | 2 |
|
3 |
| -import "testing" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
4 | 8 |
|
5 | 9 | // TestRoutePattern tests correct in-the-middle wildcard removals.
|
6 | 10 | // If user organizes a router like this:
|
@@ -85,3 +89,32 @@ func TestRoutePattern(t *testing.T) {
|
85 | 89 | t.Fatal("unexpected route pattern for root: " + p)
|
86 | 90 | }
|
87 | 91 | }
|
| 92 | + |
| 93 | +func TestAllowedMethods(t *testing.T) { |
| 94 | + t.Run("no chi context", func(t *testing.T) { |
| 95 | + got := AllowedMethods(context.Background()) |
| 96 | + if got != nil { |
| 97 | + t.Errorf("Unexpected allowed methods: %v", got) |
| 98 | + } |
| 99 | + }) |
| 100 | + t.Run("expected methods", func(t *testing.T) { |
| 101 | + want := "GET HEAD" |
| 102 | + ctx := context.WithValue(context.Background(), RouteCtxKey, &Context{ |
| 103 | + methodsAllowed: []methodTyp{mGET, mHEAD}, |
| 104 | + }) |
| 105 | + got := strings.Join(AllowedMethods(ctx), " ") |
| 106 | + if want != got { |
| 107 | + t.Errorf("Unexpected allowed methods: %s, want: %s", got, want) |
| 108 | + } |
| 109 | + }) |
| 110 | + t.Run("unexpected methods", func(t *testing.T) { |
| 111 | + want := "GET HEAD" |
| 112 | + ctx := context.WithValue(context.Background(), RouteCtxKey, &Context{ |
| 113 | + methodsAllowed: []methodTyp{mGET, mHEAD, 9000}, |
| 114 | + }) |
| 115 | + got := strings.Join(AllowedMethods(ctx), " ") |
| 116 | + if want != got { |
| 117 | + t.Errorf("Unexpected allowed methods: %s, want: %s", got, want) |
| 118 | + } |
| 119 | + }) |
| 120 | +} |
0 commit comments