|
5 | 5 | "fmt"
|
6 | 6 | "net/http"
|
7 | 7 | "net/http/httptest"
|
| 8 | + "sort" |
| 9 | + "strings" |
8 | 10 | "testing"
|
9 | 11 | )
|
10 | 12 |
|
@@ -634,6 +636,81 @@ func TestRouterServeHTTP(t *testing.T) {
|
634 | 636 | r.ServeHTTP(w, req)
|
635 | 637 | }
|
636 | 638 |
|
| 639 | +func TestPaths(t *testing.T) { |
| 640 | + e := New() |
| 641 | + |
| 642 | + e.Get("/foo", func(res http.ResponseWriter, req *http.Request) {}) |
| 643 | + e.Put("/foo", func(res http.ResponseWriter, req *http.Request) {}) |
| 644 | + e.Get("/foo/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 645 | + e.Get("/bar/baz/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 646 | + e.Get("/:controller/:action/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 647 | + |
| 648 | + g := e.Group("/api/v1") |
| 649 | + g.Get("/users/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 650 | + g.Get("/users/:id/edit", func(res http.ResponseWriter, req *http.Request) {}) |
| 651 | + g.Post("/users/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 652 | + |
| 653 | + trees := e.Router.trees |
| 654 | + |
| 655 | + exm := map[string][]string{ |
| 656 | + "GET": []string{"/:controller/:action/:id", "/api/v1/users/:id", "/api/v1/users/:id/edit", "/bar/baz/:id", "/foo", "/foo/:id"}, |
| 657 | + "PUT": []string{"/foo"}, |
| 658 | + "POST": []string{"/api/v1/users/:id"}, |
| 659 | + } |
| 660 | + |
| 661 | + for verb, exp := range exm { |
| 662 | + paths := trees[verb].Paths() |
| 663 | + sort.Strings(paths) |
| 664 | + |
| 665 | + for _, e := range exp { |
| 666 | + found := false |
| 667 | + for _, p := range paths { |
| 668 | + if p == e { |
| 669 | + found = true |
| 670 | + break |
| 671 | + } |
| 672 | + } |
| 673 | + if !found { |
| 674 | + t.Errorf("expected %q to contain %s", paths, e) |
| 675 | + } |
| 676 | + } |
| 677 | + } |
| 678 | + |
| 679 | +} |
| 680 | + |
| 681 | +func TestPrint(t *testing.T) { |
| 682 | + e := New() |
| 683 | + |
| 684 | + e.Get("/foo", func(res http.ResponseWriter, req *http.Request) {}) |
| 685 | + e.Put("/foo", func(res http.ResponseWriter, req *http.Request) {}) |
| 686 | + e.Get("/foo/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 687 | + e.Get("/bar/baz/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 688 | + e.Get("/:controller/:action/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 689 | + |
| 690 | + g := e.Group("/api/v1") |
| 691 | + g.Get("/users/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 692 | + g.Get("/users/:id/edit", func(res http.ResponseWriter, req *http.Request) {}) |
| 693 | + g.Post("/users/:id", func(res http.ResponseWriter, req *http.Request) {}) |
| 694 | + |
| 695 | + var b bytes.Buffer |
| 696 | + e.Router.Print(&b) |
| 697 | + get := `GET /foo |
| 698 | + /foo/:id |
| 699 | + /bar/baz/:id |
| 700 | + /:controller/:action/:id |
| 701 | + /api/v1/users/:id |
| 702 | + /api/v1/users/:id/edit` |
| 703 | + post := `POST /api/v1/users/:id` |
| 704 | + put := `PUT /foo` |
| 705 | + |
| 706 | + res := b.String() |
| 707 | + for _, exp := range []string{get, post, put} { |
| 708 | + if !strings.Contains(res, exp) { |
| 709 | + t.Errorf("expected %q to contain %q", res, exp) |
| 710 | + } |
| 711 | + } |
| 712 | +} |
| 713 | + |
637 | 714 | func (n *node) printTree(pfx string, tail bool) {
|
638 | 715 | p := prefix(tail, pfx, "└── ", "├── ")
|
639 | 716 | fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.typ, n.parent, n.handler)
|
|
0 commit comments