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}
}
// 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)
}
// 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)
}
--- /dev/null
+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)
+ }
+ }
+}