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.

72 lines
1.6KB

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