OSDN Git Service

add api handler test and fix walletRedirectHandler (#514)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Mon, 2 Apr 2018 10:45:02 +0000 (18:45 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 2 Apr 2018 10:45:02 +0000 (18:45 +0800)
* add asset unit test: TestUpdateAssetAlias and TestListAssets

* fix walletRedirectHandler because cannot modify req.URL.Path out of http.Handler
add api unit test

* optimse api test

api/api.go
api/api_test.go [new file with mode: 0644]

index ef755d1..27ac3f7 100644 (file)
@@ -225,7 +225,6 @@ func (a *API) buildHandler() {
        m.Handle("/submit-work", jsonHandler(a.submitWork))
 
        handler := latencyHandler(m, walletEnable)
-       handler = walletRedirectHandler(handler)
        handler = maxBytesHandler(handler) // TODO(tessr): consider moving this to non-core specific mux
        handler = webAssetsHandler(handler)
        handler = gzip.Handler{Handler: handler}
@@ -307,9 +306,11 @@ func latencyHandler(m *http.ServeMux, walletEnable bool) http.Handler {
                }
 
                // when the wallet is not been opened and the url path is not been found, modify url path to error,
-               // and after redirecting to the new url in func walletRedirectHandler
+               // and redirect handler to error
                if _, pattern := m.Handler(req); pattern != req.URL.Path && !walletEnable {
                        req.URL.Path = "/error"
+                       walletRedirectHandler(w, req)
+                       return
                }
 
                m.ServeHTTP(w, req)
@@ -317,12 +318,7 @@ func latencyHandler(m *http.ServeMux, walletEnable bool) http.Handler {
 }
 
 // walletRedirectHandler redirect to error when the wallet is closed
-func walletRedirectHandler(handler http.Handler) http.Handler {
-       return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
-               if req.URL.Path == "/error" {
-                       http.Redirect(w, req, req.URL.String(), http.StatusOK)
-                       return
-               }
-               handler.ServeHTTP(w, req)
-       })
+func walletRedirectHandler(w http.ResponseWriter, req *http.Request) {
+       h := http.RedirectHandler(req.URL.String(), http.StatusMovedPermanently)
+       h.ServeHTTP(w, req)
 }
diff --git a/api/api_test.go b/api/api_test.go
new file mode 100644 (file)
index 0000000..188c8ea
--- /dev/null
@@ -0,0 +1,99 @@
+package api
+
+import (
+       "context"
+       "net/http/httptest"
+       "os"
+       "testing"
+
+       dbm "github.com/tendermint/tmlibs/db"
+
+       "github.com/bytom/accesstoken"
+       "github.com/bytom/blockchain/rpc"
+       "github.com/bytom/testutil"
+)
+
+func TestAPIHandler(t *testing.T) {
+       a := &API{}
+       response := &Response{}
+
+       // init httptest server
+       a.buildHandler()
+       server := httptest.NewServer(a.handler)
+       defer server.Close()
+
+       // create accessTokens
+       testDB := dbm.NewDB("testdb", "leveldb", "temp")
+       defer os.RemoveAll("temp")
+       a.accessTokens = accesstoken.NewStore(testDB)
+
+       client := &rpc.Client{
+               BaseURL:     server.URL,
+               AccessToken: "test-user:test-secret",
+       }
+
+       cases := []struct {
+               path        string
+               request     interface{}
+               respWant    *Response
+               respWantErr error
+       }{
+               {
+                       path: "/create-key",
+                       request: struct {
+                               Alias    string `json:"alias"`
+                               Password string `json:"password"`
+                       }{Alias: "alice", Password: "123456"},
+                       respWant: &Response{
+                               Status: "fail",
+                               Msg:    "wallet not found, please check that the wallet is open",
+                       },
+               },
+               {
+                       path:    "/error",
+                       request: nil,
+                       respWant: &Response{
+                               Status: "fail",
+                               Msg:    "wallet not found, please check that the wallet is open",
+                       },
+               },
+               {
+                       path:    "/",
+                       request: nil,
+                       respWant: &Response{
+                               Status: "",
+                               Msg:    "",
+                       },
+               },
+               {
+                       path: "/create-access-token",
+                       request: struct {
+                               ID   string `json:"id"`
+                               Type string `json:"type"`
+                       }{ID: "test-access-id", Type: "test-access-type"},
+                       respWant: &Response{
+                               Status: "success",
+                               Msg:    "",
+                               Data:   map[string]interface{}{"id": "test-access-id", "type": "test-access-type", "token": "test-access-id:440d87ae0d625a7fcf076275b18372e09a0899e37ec86398879388de90cb0c67"},
+                       },
+               },
+               {
+                       path:    "/gas-rate",
+                       request: nil,
+                       respWant: &Response{
+                               Status: "success",
+                               Msg:    "",
+                               Data:   map[string]interface{}{"gasRate": 1000},
+                       },
+               },
+       }
+
+       for _, c := range cases {
+               response = &Response{}
+               client.Call(context.Background(), c.path, c.request, &response)
+
+               if !testutil.DeepEqual(response.Status, c.respWant.Status) {
+                       t.Errorf(`got=%#v; want=%#v`, response.Status, c.respWant.Status)
+               }
+       }
+}