diff --git a/router.go b/router.go index 176ec56..d9c3a9a 100644 --- a/router.go +++ b/router.go @@ -77,13 +77,9 @@ is matched. The handler function should take an http.ResponseWriter and an *http as its parameters. */ func (r *Router) Handle(method string, path string, handler http.Handler) { - // Add route to router - route := &Route{ - path: regexp.MustCompile("^" + path + "$"), - method: method, - handler: handler, - } - r.routes = append(r.routes, *route) + r.HandlerFunc(method, path, func(w http.ResponseWriter, req *http.Request) { + handler.ServeHTTP(w, req) + }) } /* diff --git a/router_test.go b/router_test.go index a10899e..354fe03 100644 --- a/router_test.go +++ b/router_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "regexp" "strings" "testing" @@ -85,8 +86,9 @@ func TestRouter_Handle(t *testing.T) { t.Errorf("unexpected method for route %d: expected=%s, actual=%s", i, tc.method, route.method) } - if route.path.String() != "^"+tc.path+"$" { - t.Errorf("unexpected path for route %d: expected=%s, actual=%s", i, "^"+tc.path+"$", route.path.String()) + expectedPathPattern := "^" + regexp.MustCompile(`:([\w-]+)`).ReplaceAllString(tc.path, `([-\w.]+)`) + "$" + if route.path.String() != expectedPathPattern { + t.Errorf("unexpected path for route %d: expected=%s, actual=%s", i, expectedPathPattern, route.path.String()) } if route.handler == nil { @@ -95,6 +97,35 @@ func TestRouter_Handle(t *testing.T) { } } +func TestRouter_Handle_ServeHTTP(t *testing.T) { + router := NewRouter() + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + id := router.Params(r)["id"] + w.WriteHeader(http.StatusOK) + _, err := w.Write([]byte("User ID: " + id)) + if err != nil { + t.Fatalf("Error writing response: %v", err) + } + }) + + router.Handle("GET", "/users/:id", handler) + + req := httptest.NewRequest("GET", "/users/123", nil) + w := httptest.NewRecorder() + + router.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected status code 200, got %d", w.Code) + } + + expectedBody := "User ID: 123" + if w.Body.String() != expectedBody { + t.Errorf("Expected response body %q, got %q", expectedBody, w.Body.String()) + } +} + func TestParams(t *testing.T) { testCases := []struct { path string