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

  1. package wrouter
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "git.aiterp.net/gisle/wrouter/response"
  8. "git.aiterp.net/gisle/wrouter/auth"
  9. )
  10. func TestFunction(t *testing.T) {
  11. router := &Router{}
  12. router.Function("/test", func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
  13. response.Text(w, 200, path+" called")
  14. return true
  15. })
  16. server := httptest.NewServer(router)
  17. resp, err := http.Get(server.URL + "/test/24g42g24f24f24f24f")
  18. if err != nil {
  19. t.Error("Request:", err)
  20. t.Fail()
  21. }
  22. if resp.StatusCode != 200 {
  23. t.Error("Expected 200, got", resp.Status)
  24. t.Fail()
  25. }
  26. if resp.ContentLength == 0 {
  27. t.Error("No content returned from server")
  28. t.Fail()
  29. }
  30. data, _ := ioutil.ReadAll(resp.Body)
  31. if string(data) != "/test called" {
  32. t.Errorf(`Body: "%s" != "%s"`, string(data), "/test called")
  33. }
  34. }