From 25b6f7d574b10451ad811c2e063c257e1bc83831 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sun, 9 Jul 2017 04:12:32 +1000 Subject: [PATCH] redirect handler tested. --- crmpixel_test.go | 19 +++++++++++++++++++ main.go | 25 +++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/crmpixel_test.go b/crmpixel_test.go index e2ae171..32b1351 100644 --- a/crmpixel_test.go +++ b/crmpixel_test.go @@ -4,13 +4,17 @@ import ( "io/ioutil" "log" "net/http" + "net/http/httptest" "testing" ) func TestCrmPixelCookie(t *testing.T) { req := buildReqCrmPixel() rr, _ := getHTTPResponse(req, crmpixel) + verifyPixelSetCookie(t, rr) +} +func verifyPixelSetCookie(t *testing.T, rr *httptest.ResponseRecorder) { //check Set-Cookie v, ok := rr.HeaderMap["Set-Cookie"] //biukop_cl=59610affc2ccce92d|323295325|1499532031|e0010c3e24a201665b783e2c48ab323b87ef52b7; Expires=Tue, 06 Jul 2027 16:40:31 GMT log.Println(v) @@ -22,7 +26,12 @@ func TestCrmPixelCookie(t *testing.T) { AssertEqual(t, err, nil, "pixel image should have no error") m, err := ioutil.ReadAll(rr.Body) AssertEqual(t, testEq(pixel, m), true, "pixel image should be the same") +} +func TestRedirectHandler(t *testing.T) { + req := buildReqRedirect() + rr, _ := getHTTPResponse(req, crmpixel) + verifyPixelSetCookie(t, rr) } func checkRecorderCookieBiukopCL(v []string) bool { @@ -46,6 +55,16 @@ func buildReqCrmPixel() (req *http.Request) { return req } +func buildReqRedirect() (req *http.Request) { + req, err := http.NewRequest("GET", "/redirect?url=url=http%3A%2F%2Fkidshealth.org%2Fen%2Fparents%2Ffas.html", nil) + if err != nil { + log.Fatal(err) + } + //buildReqCommonSignature(req, IntraAPIConfig.CRMSecrete) + buildReqCommonHeader(req) + return req +} + //compare two byte slice func testEq(a, b []byte) bool { diff --git a/main.go b/main.go index 0c8fd20..23b84a3 100644 --- a/main.go +++ b/main.go @@ -60,7 +60,7 @@ func setupHTTPHandler() { http.HandleFunc("/crmfiles/", crmAttachmentHandler) http.HandleFunc("/dumprequest", dumpReuestHandler) http.HandleFunc("/MP_verify_6JqVkftKr39GMakA.txt", mpDomainAuthSecret) - http.HandleFunc("/profile_newly_register", initialRegistrationHandler) + http.HandleFunc("/redirect", setTrackingCookieAndRecirect) http.HandleFunc("/iapi/getAccessToken", supplyAccessToken) http.HandleFunc("/iapi/createWechatQr", iapiCreateWechatQrCode) http.HandleFunc("/crmpixel.png", crmpixel) //tracking pixel. @@ -189,14 +189,19 @@ func mpDomainAuthSecret(w http.ResponseWriter, r *http.Request) { //or if the user has already been registered, //redirect user to a URL "/pages/dashboard" // -func initialRegistrationHandler(w http.ResponseWriter, r *http.Request) { - //TODO: verify url parameters and make sure its username is correctly set - // - expiration := time.Now().Add(10 * 365 * 24 * time.Hour) - str := time.Now().String() - cookie := http.Cookie{Name: "username", Value: str, Expires: expiration} - http.SetCookie(w, &cookie) - cookie = http.Cookie{Name: "signature", Value: "abcee", Expires: expiration} +func setTrackingCookieAndRecirect(w http.ResponseWriter, r *http.Request) { + + cookie := crmpixelCookie(r) http.SetCookie(w, &cookie) - http.Redirect(w, r, "http://192.168.1.39:4200/#pages/charts/chartist-js", 307) //302 temp redirect + + rq := r.URL.RawQuery + m, _ := url.ParseQuery(rq) + + url, ok := m["url"] + if ok { + http.Redirect(w, r, url[0], 307) //302 temp redirect + return + } + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "Not Found URL") }