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.

91 satır
2.1KB

  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. )
  9. func TestCrmPixelCookie(t *testing.T) {
  10. req := buildReqCrmPixel()
  11. rr, _ := getHTTPResponse(req, crmpixel)
  12. verifyPixelSetCookie(t, rr)
  13. }
  14. func verifyPixelSetCookie(t *testing.T, rr *httptest.ResponseRecorder) {
  15. //check Set-Cookie
  16. v, ok := rr.HeaderMap["Set-Cookie"] //biukop_cl=59610affc2ccce92d|323295325|1499532031|e0010c3e24a201665b783e2c48ab323b87ef52b7; Expires=Tue, 06 Jul 2027 16:40:31 GMT
  17. log.Println(v)
  18. AssertEqual(t, ok, true, "Set-Cookie shold be available")
  19. AssertEqual(t, checkRecorderCookieBiukopCL(v), true, "cookie value should be valid")
  20. //compare body content
  21. pixel, err := crmpixelImage()
  22. AssertEqual(t, err, nil, "pixel image should have no error")
  23. m, err := ioutil.ReadAll(rr.Body)
  24. AssertEqual(t, testEq(pixel, m), true, "pixel image should be the same")
  25. }
  26. func TestRedirectHandler(t *testing.T) {
  27. req := buildReqRedirect()
  28. rr, _ := getHTTPResponse(req, crmpixel)
  29. verifyPixelSetCookie(t, rr)
  30. }
  31. func checkRecorderCookieBiukopCL(v []string) bool {
  32. // Copy the Cookie over to a new Request
  33. request := &http.Request{Header: http.Header{"Cookie": v}}
  34. // Extract the dropped cookie from the request.
  35. cookie, err := request.Cookie("biukop_cl")
  36. valid := isValidCookieBiukopCL(cookie.Value)
  37. return err == nil && valid
  38. }
  39. func buildReqCrmPixel() (req *http.Request) {
  40. req, err := http.NewRequest("GET", "/crmpixel.png", nil)
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. //buildReqCommonSignature(req, IntraAPIConfig.CRMSecrete)
  45. buildReqCommonHeader(req)
  46. return req
  47. }
  48. func buildReqRedirect() (req *http.Request) {
  49. req, err := http.NewRequest("GET", "/spa/redirect?url=url=http%3A%2F%2Fkidshealth.org%2Fen%2Fparents%2Ffas.html", nil)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. //buildReqCommonSignature(req, IntraAPIConfig.CRMSecrete)
  54. buildReqCommonHeader(req)
  55. return req
  56. }
  57. //compare two byte slice
  58. func testEq(a, b []byte) bool {
  59. if a == nil && b == nil {
  60. return true
  61. }
  62. if a == nil || b == nil {
  63. return false
  64. }
  65. if len(a) != len(b) {
  66. return false
  67. }
  68. for i := range a {
  69. if a[i] != b[i] {
  70. return false
  71. }
  72. }
  73. return true
  74. }