From 991cd591d1bb712daa94cba85b2336355530d433 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Wed, 26 Apr 2017 21:41:47 +1000 Subject: [PATCH] web init and api signature check test case passed --- server.go | 2 +- server_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 90 insertions(+), 15 deletions(-) diff --git a/server.go b/server.go index e2d1cb4..efd1295 100644 --- a/server.go +++ b/server.go @@ -106,7 +106,7 @@ func webrootHandler(w http.ResponseWriter, r *http.Request) { rq := r.URL.RawQuery m, _ := url.ParseQuery(rq) for index, element := range m { - fmt.Fprintf(w, "\r\n%s => %s", index, element) + fmt.Fprintf(w, "\n%s => %s", index, element) } logRequestDebug(httputil.DumpRequest(r, true)) diff --git a/server_test.go b/server_test.go index 6ae1f60..64f28be 100644 --- a/server_test.go +++ b/server_test.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "log" "net/http" "net/http/httptest" "testing" @@ -8,35 +10,108 @@ import ( //when we setup wechate parameters,we chat will verify us func TestInitialSetup(t *testing.T) { + SetupConfig() + + req := buildReqWechatAPISetup() + + rr, _ := getHTTPResponse(req, answerInitialAuth) + + // Check the response body is what we expect. + expected := `913461463450840893` + if rr.Body.String() != expected { + t.Errorf("handler returned unexpected body: got %v want %v", + rr.Body.String(), expected) + } } func TestWebRootHandler(t *testing.T) { - // Create a request to pass to our handler. We don't have any query parameters for now, so we'll - // pass 'nil' as the third parameter. - req, err := http.NewRequest("GET", "/dummydir", nil) - if err != nil { - t.Fatal(err) + SetupConfig() + req := buildReqWechatWebRoot() + rr, _ := getHTTPResponse(req, webrootHandler) + + // Check the response body is what we expect. + expected := `Hi there, I love dummydir! +echostr => [913461463450840893] +signature => [e39de9f2e28079c01ebb4b803dfc3442b819545c]` + + if rr.Body.String() != expected { + t.Errorf("handler returned unexpected body: got %v want %v", + rr.Body.String(), expected) } - // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response. - rr := httptest.NewRecorder() - handler := http.HandlerFunc(webrootHandler) +} + +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 // directly and pass in our Request and ResponseRecorder. + rr = httptest.NewRecorder() + handler.ServeHTTP(rr, req) // Check the status code is what we expect. if status := rr.Code; status != http.StatusOK { - t.Errorf("handler returned wrong status code: got %v want %v", + err = fmt.Errorf("wrong HTTP status code: got %v want %v", status, http.StatusOK) } + return +} - // Check the response body is what we expect. - expected := `Hi there, I love dummydir!` - if rr.Body.String() != expected { - t.Errorf("handler returned unexpected body: got %v want %v", - rr.Body.String(), expected) +func buildReqWechatWebRoot() *http.Request { + req, _ := http.NewRequest("GET", "/dummydir", nil) + buildReqCommonHeader(req) + q := req.URL.Query() + q.Add("signature", "e39de9f2e28079c01ebb4b803dfc3442b819545c") + q.Add("echostr", "913461463450840893") + req.URL.RawQuery = q.Encode() + return req +} + +func buildReqWechatAPISetup() *http.Request { + // Create a request to pass to our handler. + //We don't have any query body for now, so we'll + // pass 'nil' as the third parameter. + req, err := http.NewRequest("GET", "/apii", nil) + if err != nil { + log.Fatal(err) } + + q := req.URL.Query() + q.Add("signature", "e39de9f2e28079c01ebb4b803dfc3442b819545c") + q.Add("echostr", "913461463450840893") + q.Add("timestamp", "1492970761") + q.Add("nonce", "1850971833") + req.URL.RawQuery = q.Encode() + + buildReqCommonHeader(req) + return req +} + +func buildReqCommonHeader(r *http.Request) { + // + // example request + // + // GET /api?signature=e39de9f2e28079c01ebb4b803dfc3442b819545c&echostr=913461463450840893×tamp=1492970761&nonce=1850971833 HTTP/1.1 + // Host: wechat.hitxy.org.au + // Accept: */* + // Cache-Control: no-cache + // Connection: Keep-Alive + // Pragma: no-cache + // User-Agent: Mozilla/4.0 + // X-Forwarded-For: 103.7.30.107 + // X-Forwarded-Host: wechat.hitxy.org.au + // X-Forwarded-Server: wechat.hitxy.org.au + + h := r.Header + h.Set("Host", "wechat.hitxy.org.au") + h.Set("Accept", "*/*") + h.Set("Cache-Control", "no-cache") + h.Set("Connection", "Keep-Alive") + h.Set("Pragma", "no-cache") + h.Set("User-Agent", "Patrick testcase") + h.Set("X-Forwarded-For", "103.7.30.107") + h.Set("X-Forwarded-Host", "wechat.hitxy.org.au") + h.Set("X-Forwarded-Server", "wechat.hitxy.org.au") + }