(t *testing.T)
| 8 | ) |
| 9 | |
| 10 | func TestPathValue(t *testing.T) { |
| 11 | testCases := []struct { |
| 12 | name string |
| 13 | pattern string |
| 14 | method string |
| 15 | requestPath string |
| 16 | expectedBody string |
| 17 | pathKeys []string |
| 18 | }{ |
| 19 | { |
| 20 | name: "Basic path value", |
| 21 | pattern: "/hubs/{hubID}", |
| 22 | method: "GET", |
| 23 | pathKeys: []string{"hubID"}, |
| 24 | requestPath: "/hubs/392", |
| 25 | expectedBody: "392", |
| 26 | }, |
| 27 | { |
| 28 | name: "Two path values", |
| 29 | pattern: "/users/{userID}/conversations/{conversationID}", |
| 30 | method: "POST", |
| 31 | pathKeys: []string{"userID", "conversationID"}, |
| 32 | requestPath: "/users/Gojo/conversations/2948", |
| 33 | expectedBody: "Gojo 2948", |
| 34 | }, |
| 35 | { |
| 36 | name: "Wildcard path", |
| 37 | pattern: "/users/{userID}/friends/*", |
| 38 | method: "POST", |
| 39 | pathKeys: []string{"userID", "*"}, |
| 40 | requestPath: "/users/Gojo/friends/all-of-them/and/more", |
| 41 | expectedBody: "Gojo all-of-them/and/more", |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | for _, tc := range testCases { |
| 46 | t.Run(tc.name, func(t *testing.T) { |
| 47 | r := NewRouter() |
| 48 | |
| 49 | r.Handle(tc.method+" "+tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 50 | pathValues := []string{} |
| 51 | for _, pathKey := range tc.pathKeys { |
| 52 | pathValue := r.PathValue(pathKey) |
| 53 | if pathValue == "" { |
| 54 | pathValue = "NOT_FOUND:" + pathKey |
| 55 | } |
| 56 | |
| 57 | pathValues = append(pathValues, pathValue) |
| 58 | } |
| 59 | |
| 60 | body := strings.Join(pathValues, " ") |
| 61 | |
| 62 | w.Write([]byte(body)) |
| 63 | })) |
| 64 | |
| 65 | ts := httptest.NewServer(r) |
| 66 | defer ts.Close() |
| 67 |
nothing calls this directly
no test coverage detected