Core functionality for new aiterp.net servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
872 B

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")
}
}