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.

66 lines
1.3 KiB

  1. package wrouter
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. )
  8. func TestStatic(t *testing.T) {
  9. router := &Router{}
  10. router.Static("/data", "./")
  11. server := httptest.NewServer(router)
  12. t.Run("Download", func(t *testing.T) {
  13. resp, err := http.Get(server.URL + "/data/README.md")
  14. if err != nil {
  15. t.Error("Request:", err)
  16. t.Fail()
  17. }
  18. if resp.StatusCode != 200 {
  19. t.Error("Expected 200, got", resp.Status)
  20. t.Fail()
  21. }
  22. if resp.ContentLength == 0 {
  23. t.Error("No content returned from server")
  24. t.Fail()
  25. }
  26. if !strings.Contains(resp.Header.Get("Content-Type"), "text/plain") {
  27. t.Errorf("Content-Type %s != %s", resp.Header.Get("Content-Type"), "text/plain")
  28. t.Fail()
  29. }
  30. })
  31. t.Run("Download_Fail", func(t *testing.T) {
  32. resp, err := http.Get(server.URL + "/data/f42klfk2kf2kfk.md")
  33. if err != nil {
  34. t.Error("Request:", err)
  35. t.Fail()
  36. }
  37. if resp.StatusCode != 404 {
  38. t.Error("Expected 404, got", resp.Status)
  39. t.Fail()
  40. }
  41. })
  42. t.Run("Download_Fail2", func(t *testing.T) {
  43. resp, err := http.Get(server.URL + "/data/../../../../../../../etc/passwd")
  44. if err != nil {
  45. t.Error("Request:", err)
  46. t.Fail()
  47. }
  48. if resp.StatusCode != 403 {
  49. t.Error("Expected 403, got", resp.Status)
  50. t.Fail()
  51. }
  52. })
  53. }