Procházet zdrojové kódy

redirect handler tested.

master
Patrick Peng Sun před 8 roky
rodič
revize
25b6f7d574
2 změnil soubory, kde provedl 34 přidání a 10 odebrání
  1. +19
    -0
      crmpixel_test.go
  2. +15
    -10
      main.go

+ 19
- 0
crmpixel_test.go Zobrazit soubor

@@ -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 {


+ 15
- 10
main.go Zobrazit soubor

@@ -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")
}

Načítá se…
Zrušit
Uložit