flash_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2019 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. "encoding/json"
  18. "fmt"
  19. "net/http"
  20. "net/http/httptest"
  21. "testing"
  22. "github.com/stretchr/testify/assert"
  23. "github.com/stretchr/testify/require"
  24. "github.com/drakkan/sftpgo/v2/internal/util"
  25. )
  26. func TestFlashMessages(t *testing.T) {
  27. rr := httptest.NewRecorder()
  28. req, err := http.NewRequest(http.MethodGet, "/url", nil)
  29. require.NoError(t, err)
  30. message := flashMessage{
  31. ErrorString: "error",
  32. I18nMessage: util.I18nChangePwdTitle,
  33. }
  34. setFlashMessage(rr, req, message)
  35. value, err := json.Marshal(message)
  36. assert.NoError(t, err)
  37. req.Header.Set("Cookie", fmt.Sprintf("%v=%v", flashCookieName, base64.URLEncoding.EncodeToString(value)))
  38. msg := getFlashMessage(rr, req)
  39. assert.Equal(t, message, msg)
  40. assert.Equal(t, util.I18nChangePwdTitle, msg.getI18nError().Message)
  41. req.Header.Set("Cookie", fmt.Sprintf("%v=%v", flashCookieName, "a"))
  42. msg = getFlashMessage(rr, req)
  43. assert.Empty(t, msg)
  44. req.Header.Set("Cookie", fmt.Sprintf("%v=%v", flashCookieName, "YQ=="))
  45. msg = getFlashMessage(rr, req)
  46. assert.Empty(t, msg)
  47. }