package api import ( "context" "net/http/httptest" "os" "testing" "github.com/vapor/accesstoken" "github.com/vapor/blockchain/rpc" dbm "github.com/vapor/database/leveldb" "github.com/vapor/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 }{ { 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) } } }