1
0

flash_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "encoding/base64"
  17. "fmt"
  18. "net/http"
  19. "net/http/httptest"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/stretchr/testify/require"
  23. )
  24. func TestFlashMessages(t *testing.T) {
  25. rr := httptest.NewRecorder()
  26. req, err := http.NewRequest(http.MethodGet, "/url", nil)
  27. require.NoError(t, err)
  28. message := "test message"
  29. setFlashMessage(rr, req, message)
  30. req.Header.Set("Cookie", fmt.Sprintf("%v=%v", flashCookieName, base64.URLEncoding.EncodeToString([]byte(message))))
  31. msg := getFlashMessage(rr, req)
  32. assert.Equal(t, message, msg)
  33. req.Header.Set("Cookie", fmt.Sprintf("%v=%v", flashCookieName, "a"))
  34. msg = getFlashMessage(rr, req)
  35. assert.Empty(t, msg)
  36. }