package wrouter import ( "io/ioutil" "net/http" "net/http/httptest" "testing" "git.aiterp.net/gisle/wrouter/response" "git.aiterp.net/gisle/wrouter/auth" ) func TestFunction(t *testing.T) { router := &Router{} router.Function("/test", func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool { response.Text(w, 200, path+" called") return true }) server := httptest.NewServer(router) resp, err := http.Get(server.URL + "/test/24g42g24f24f24f24f") if err != nil { t.Error("Request:", err) t.Fail() } if resp.StatusCode != 200 { t.Error("Expected 200, got", resp.Status) t.Fail() } if resp.ContentLength == 0 { t.Error("No content returned from server") t.Fail() } data, _ := ioutil.ReadAll(resp.Body) if string(data) != "/test called" { t.Errorf(`Body: "%s" != "%s"`, string(data), "/test called") } }