File: pattern_test.go

package info (click to toggle)
golang-github-go-chi-chi 5.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 664 kB
  • sloc: makefile: 19
file content (56 lines) | stat: -rw-r--r-- 1,186 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
//go:build go1.23
// +build go1.23

package chi

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestPattern(t *testing.T) {
	testCases := []struct {
		name        string
		pattern     string
		method      string
		requestPath string
	}{
		{
			name:        "Basic path value",
			pattern:     "/hubs/{hubID}",
			method:      "GET",
			requestPath: "/hubs/392",
		},
		{
			name:        "Two path values",
			pattern:     "/users/{userID}/conversations/{conversationID}",
			method:      "POST",
			requestPath: "/users/Gojo/conversations/2948",
		},
		{
			name:        "Wildcard path",
			pattern:     "/users/{userID}/friends/*",
			method:      "POST",
			requestPath: "/users/Gojo/friends/all-of-them/and/more",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			r := NewRouter()

			r.Handle(tc.method+" "+tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.Write([]byte(r.Pattern))
			}))

			ts := httptest.NewServer(r)
			defer ts.Close()

			_, body := testRequest(t, ts, tc.method, tc.requestPath, nil)
			if body != tc.pattern {
				t.Fatalf("expecting %q, got %q", tc.pattern, body)
			}
		})
	}
}