From 329decb9395bb15a4e65316121b38061d3fe5ed7 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Thu, 6 Jul 2017 19:18:41 +1000 Subject: [PATCH] test temp qr tested --- server_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/server_test.go b/server_test.go index d8b6f60..5d6a0db 100644 --- a/server_test.go +++ b/server_test.go @@ -94,7 +94,9 @@ func TestGetAccesstokenUnAuthorized(t *testing.T) { func TestCreatePermenentWechatQr(t *testing.T) { scene := "edit_profile" //do not create rubbish, create something that we can use later on + //permenant ticked for "0" expected := "gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA" + expected = "gQFR8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZnNRMVJmZ3VjU2gxMDAwME0wN28AAgTQaSVZAwQAAAAA" req := buildReqPermQr(scene) rr, _ := getHTTPResponse(req, iapiCreateWechatQrCode) m := rr.Body.String() @@ -106,6 +108,22 @@ func TestCreatePermenentWechatQr(t *testing.T) { AssertEqual(t, info.Ticket, expected, "expected ticket not match") } +func TestCreateTmpWechatQr(t *testing.T) { + qrValue := int32(time.Now().Unix()) + log.Println(qrValue) + qrExpire := int32(1200) + req := buildReqTmpQr(qrValue, qrExpire) + rr, _ := getHTTPResponse(req, iapiCreateWechatQrCode) + m := rr.Body.String() + info := QRSrcInfo{} + err := json.Unmarshal([]byte(m), &info) + AssertEqual(t, err, nil, "decode json should be correct") + log.Println(info) + log.Printf("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s", info.Ticket) + AssertEqual(t, info.Ticket != "", true, "expected ticket not match") + //use wechat to check scan code is correct +} + func getHTTPResponse(req *http.Request, handler http.HandlerFunc) (rr *httptest.ResponseRecorder, err error) { // Our handlers satisfy http.Handler, so we can call their ServeHTTP method @@ -223,6 +241,22 @@ func buildReqPermQr(scene string) *http.Request { } +func buildReqTmpQr(val, expire int32) *http.Request { + req, err := http.NewRequest("GET", "/iapi/createWechatQr", nil) + if err != nil { + log.Fatal(err) + } + q := req.URL.Query() + q.Add("qrValue", fmt.Sprintf("%d", val)) + q.Add("expire", fmt.Sprintf("%d", expire)) + req.URL.RawQuery = q.Encode() + + buildReqCommonSignature(req, IntraAPIConfig.CRMSecrete) + buildReqCommonHeader(req) + + return req +} + func buildSignature(token string) (signature, timestamp, nonce string) { timestamp = fmt.Sprintf("%d", int32(time.Now().Unix())) nonce = "1461107899" //a randome string cut from previous wechat request