Testing Cookies

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/kataras/iris/v12/httptest"
  6. )
  7. func TestCookiesBasic(t *testing.T) {
  8. app := newApp()
  9. e := httptest.New(t, app, httptest.URL("http://example.com"))
  10. cookieName, cookieValue := "my_cookie_name", "my_cookie_value"
  11. // Test Set A Cookie.
  12. t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)).
  13. Expect().Status(httptest.StatusOK)
  14. // Validate cookie's existence, it should be available now.
  15. t1.Cookie(cookieName).Value().Equal(cookieValue)
  16. t1.Body().Contains(cookieValue)
  17. path := fmt.Sprintf("/cookies/%s", cookieName)
  18. // Test Retrieve A Cookie.
  19. t2 := e.GET(path).Expect().Status(httptest.StatusOK)
  20. t2.Body().Equal(cookieValue)
  21. // Test Remove A Cookie.
  22. t3 := e.DELETE(path).Expect().Status(httptest.StatusOK)
  23. t3.Body().Contains(cookieName)
  24. t4 := e.GET(path).Expect().Status(httptest.StatusOK)
  25. t4.Cookies().Empty()
  26. t4.Body().Empty()
  27. }
  1. $ go test -v -run=TestCookiesBasic$

Iris web framework itself uses this package to test itself. In the _examples repository directory you will find some useful tests as well. For more information please take a look and read the httpexpect’s documentation.