A simple way to handle form submissions from static websites.
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.

117 lines
2.6 KiB

4 years ago
  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/url"
  6. "os"
  7. "reflect"
  8. "testing"
  9. )
  10. func Test_sanitizeForm(t *testing.T) {
  11. t.Run("Sanitize form", func(t *testing.T) {
  12. result := sanitizeForm(url.Values{"<b>Test</b>": {"<a href=\"https://example.com\">Test</a>"}})
  13. want := FormValues{"Test": {"Test"}}
  14. if !reflect.DeepEqual(result, want) {
  15. t.Error()
  16. }
  17. })
  18. }
  19. func TestFormHandler(t *testing.T) {
  20. t.Run("GET request to FormHandler", func(t *testing.T) {
  21. req := httptest.NewRequest("GET", "http://example.com/", nil)
  22. w := httptest.NewRecorder()
  23. FormHandler(w, req)
  24. resp := w.Result()
  25. if resp.StatusCode != http.StatusOK {
  26. t.Error()
  27. }
  28. })
  29. t.Run("POST request to FormHandler", func(t *testing.T) {
  30. req := httptest.NewRequest("POST", "http://example.com/", nil)
  31. w := httptest.NewRecorder()
  32. FormHandler(w, req)
  33. resp := w.Result()
  34. if resp.StatusCode != http.StatusCreated {
  35. t.Error()
  36. }
  37. })
  38. t.Run("Wrong method request to FormHandler", func(t *testing.T) {
  39. req := httptest.NewRequest("DELETE", "http://example.com/", nil)
  40. w := httptest.NewRecorder()
  41. FormHandler(w, req)
  42. resp := w.Result()
  43. if resp.StatusCode != http.StatusMethodNotAllowed {
  44. t.Error()
  45. }
  46. })
  47. }
  48. func Test_isBot(t *testing.T) {
  49. t.Run("No bot", func(t *testing.T) {
  50. os.Clearenv()
  51. result := isBot(FormValues{"_t_email": {""}})
  52. if !reflect.DeepEqual(result, false) {
  53. t.Error()
  54. }
  55. })
  56. t.Run("No honeypot", func(t *testing.T) {
  57. os.Clearenv()
  58. result := isBot(FormValues{})
  59. if !reflect.DeepEqual(result, false) {
  60. t.Error()
  61. }
  62. })
  63. t.Run("Bot", func(t *testing.T) {
  64. os.Clearenv()
  65. result := isBot(FormValues{"_t_email": {"Test", ""}})
  66. if !reflect.DeepEqual(result, true) {
  67. t.Error()
  68. }
  69. })
  70. }
  71. func Test_sendResponse(t *testing.T) {
  72. t.Run("No redirect", func(t *testing.T) {
  73. values := FormValues{}
  74. w := httptest.NewRecorder()
  75. sendResponse(values, w)
  76. if w.Code != http.StatusCreated {
  77. t.Error()
  78. }
  79. })
  80. t.Run("No redirect 2", func(t *testing.T) {
  81. values := FormValues{
  82. "_redirectTo": {""},
  83. }
  84. w := httptest.NewRecorder()
  85. sendResponse(values, w)
  86. if w.Code != http.StatusCreated {
  87. t.Error()
  88. }
  89. })
  90. t.Run("No redirect 3", func(t *testing.T) {
  91. values := FormValues{
  92. "_redirectTo": {"abc", "def"},
  93. }
  94. w := httptest.NewRecorder()
  95. sendResponse(values, w)
  96. if w.Code != http.StatusCreated {
  97. t.Error()
  98. }
  99. })
  100. t.Run("Redirect", func(t *testing.T) {
  101. values := FormValues{
  102. "_redirectTo": {"https://example.com"},
  103. }
  104. w := httptest.NewRecorder()
  105. sendResponse(values, w)
  106. if w.Code != http.StatusSeeOther {
  107. t.Error()
  108. }
  109. if w.Header().Get("Location") != "https://example.com" {
  110. t.Error()
  111. }
  112. })
  113. }