123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- // Copyright 2018 Keelan Lightfoot. All rights reserved.
- // Copyright 2013 Julien Schmidt. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be found
- // in the LICENSE file.
-
- package pathrouter
-
- import (
- "reflect"
- "testing"
- )
-
- func TestParams(t *testing.T) {
- ps := Params{
- Param{"param1", "value1"},
- Param{"param2", "value2"},
- Param{"param3", "value3"},
- }
- for i := range ps {
- if val := ps.ByName(ps[i].Key); val != ps[i].Value {
- t.Errorf("Wrong value for %s: Got %s; Want %s", ps[i].Key, val, ps[i].Value)
- }
- }
- if val := ps.ByName("noKey"); val != "" {
- t.Errorf("Expected empty string for not found key; got: %s", val)
- }
- }
-
- func TestRouter(t *testing.T) {
- router := New()
-
- routed := false
- router.Handle("/user/:name", func(ps Params, _ interface{}) {
- routed = true
- want := Params{Param{"name", "gopher"}}
- if !reflect.DeepEqual(ps, want) {
- t.Fatalf("wrong wildcard values: want %v, got %v", want, ps)
- }
- })
-
- router.Execute("/user/gopher", nil)
-
- if !routed {
- t.Fatal("routing failed")
- }
- }
-
- func TestRouterAPI(t *testing.T) {
- var handle bool
-
- router := New()
- router.Handle("/Handle", func(ps Params, _ interface{}) {
- handle = true
- })
-
- router.Execute("/Handle", nil)
-
- if !handle {
- t.Error("routing Handle failed")
- }
- }
-
- func TestRouterRoot(t *testing.T) {
- router := New()
- recv := catchPanic(func() {
- router.Handle("noSlashRoot", nil)
- })
- if recv == nil {
- t.Fatal("registering path not beginning with '/' did not panic")
- }
- }
-
- func TestRouterNotFound(t *testing.T) {
- handlerFunc := func(_ Params, _ interface{}) {}
-
- // Test non-existant route
- router := New()
- router.Handle("/path", handlerFunc)
- err := router.Execute("/nope", nil)
- if err == nil {
- t.Errorf("NotFound handling route /nope failed")
- }
-
- // Test special case where no node for the prefix "/" exists
- router = New()
- router.Handle("/a", handlerFunc)
- err = router.Execute("/", nil)
- if err == nil {
- t.Errorf("NotFound handling route / failed")
- }
- }
-
- func TestRouterLookup(t *testing.T) {
- routed := false
- wantHandle := func(_ Params, _ interface{}) {
- routed = true
- }
- wantParams := Params{Param{"name", "gopher"}}
-
- router := New()
-
- // try empty router first
- handle, _ := router.Lookup("GET", "/nope")
- if handle != nil {
- t.Fatalf("Got handle for unregistered pattern: %v", handle)
- }
-
- // insert route and try again
- router.Handle("/user/:name", wantHandle)
-
- handle, params := router.Lookup("GET", "/user/gopher")
- if handle == nil {
- t.Fatal("Got no handle!")
- } else {
- handle(nil, nil)
- if !routed {
- t.Fatal("Routing failed!")
- }
- }
-
- if !reflect.DeepEqual(params, wantParams) {
- t.Fatalf("Wrong parameter values: want %v, got %v", wantParams, params)
- }
-
- handle, _ = router.Lookup("GET", "/user/gopher/")
- if handle != nil {
- t.Fatalf("Got handle for unregistered pattern: %v", handle)
- }
- }
|