File: mux_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 (45 lines) | stat: -rw-r--r-- 973 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
36
37
38
39
40
41
42
43
44
45
package web

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

// Sanity check types
var _ http.Handler = &Mux{}
var _ Handler = &Mux{}

// There's... really not a lot to do here.

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

	m := New()
	ch := make(chan string, 1)

	m.Get("/hello/:name", func(c C, w http.ResponseWriter, r *http.Request) {
		greeting := "Hello "
		if c.Env != nil {
			if g, ok := c.Env["greeting"]; ok {
				greeting = g.(string)
			}
		}
		ch <- greeting + c.URLParams["name"]
	})

	r, _ := http.NewRequest("GET", "/hello/carl", nil)
	m.ServeHTTP(httptest.NewRecorder(), r)
	out := <-ch
	if out != "Hello carl" {
		t.Errorf(`Unexpected response %q, expected "Hello carl"`, out)
	}

	r, _ = http.NewRequest("GET", "/hello/bob", nil)
	env := map[interface{}]interface{}{"greeting": "Yo "}
	m.ServeHTTPC(C{Env: env}, httptest.NewRecorder(), r)
	out = <-ch
	if out != "Yo bob" {
		t.Errorf(`Unexpected response %q, expected "Yo bob"`, out)
	}
}