File: router_middleware_test.go

package info (click to toggle)
golang-github-zenazn-goji 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 464 kB
  • sloc: makefile: 3
file content (35 lines) | stat: -rw-r--r-- 713 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package web

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestRouterMiddleware(t *testing.T) {
	t.Parallel()

	m := New()
	ch := make(chan string, 1)
	m.Get("/a", chHandler(ch, "a"))
	m.Get("/b", chHandler(ch, "b"))
	m.Use(m.Router)
	m.Use(func(c *C, h http.Handler) http.Handler {
		fn := func(w http.ResponseWriter, r *http.Request) {
			m := GetMatch(*c)
			if rp := m.RawPattern(); rp != "/a" {
				t.Fatalf("RawPattern was not /a: %v", rp)
			}
			r.URL.Path = "/b"
			h.ServeHTTP(w, r)
		}
		return http.HandlerFunc(fn)
	})

	r, _ := http.NewRequest("GET", "/a", nil)
	w := httptest.NewRecorder()
	m.ServeHTTP(w, r)
	if v := <-ch; v != "a" {
		t.Errorf("Routing was not frozen! %s", v)
	}
}