about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--router_test.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/router_test.go b/router_test.go
new file mode 100644
index 0000000..5b85aa8
--- /dev/null
+++ b/router_test.go
@@ -0,0 +1,46 @@
+package mooh
+
+import (
+	"net/http"
+	"net/url"
+	"testing"
+)
+
+func testRoute(req *Request) (Response, error) {
+	resp := Response{}
+	return resp, nil
+}
+
+func TestBasic(t *testing.T) {
+	router := BuildDispatcher()
+	router.AddRoute("/blog/:year/:month/:day", "GET", testRoute)
+	router.AddRoute("/blog", "GET", testRoute)
+
+	if router.Routes[0].Path != "/blog/:year/:month/:day" {
+		t.Fatal()
+	}
+	if router.Routes[1].Path != "/blog" {
+		t.Fatal()
+	}
+}
+
+func TestMatch(t *testing.T) {
+	router := BuildDispatcher()
+	router.AddRoute("/blog/:year/:month/:day", "GET", testRoute)
+	router.AddRoute("/blog", "GET", testRoute)
+
+	pathToTests := []url.URL{
+		url.URL{Path: "blog"},
+		url.URL{Path: "/blog"},
+		url.URL{Path: "blog/2013/4/21"},
+		url.URL{Path: "/blog/2013/21/4"},
+	}
+	for _, p := range pathToTests {
+		r := http.Request{URL: &p, Method: "GET"}
+		req := Request{&r}
+		m := router.Match(req)
+		if m == nil {
+			t.Fatal()
+		}
+	}
+}