OSDN Git Service

Merge branch 'dev' into dev-verify
[bytom/bytom.git] / api / api_test.go
1 package api
2
3 import (
4         "context"
5         "net/http/httptest"
6         "os"
7         "testing"
8
9         dbm "github.com/tendermint/tmlibs/db"
10
11         "github.com/bytom/accesstoken"
12         "github.com/bytom/blockchain/rpc"
13         "github.com/bytom/testutil"
14 )
15
16 func TestAPIHandler(t *testing.T) {
17         a := &API{}
18         response := &Response{}
19
20         // init httptest server
21         a.buildHandler()
22         server := httptest.NewServer(a.handler)
23         defer server.Close()
24
25         // create accessTokens
26         testDB := dbm.NewDB("testdb", "leveldb", "temp")
27         defer os.RemoveAll("temp")
28         a.accessTokens = accesstoken.NewStore(testDB)
29
30         client := &rpc.Client{
31                 BaseURL:     server.URL,
32                 AccessToken: "test-user:test-secret",
33         }
34
35         cases := []struct {
36                 path        string
37                 request     interface{}
38                 respWant    *Response
39         }{
40                 {
41                         path: "/create-key",
42                         request: struct {
43                                 Alias    string `json:"alias"`
44                                 Password string `json:"password"`
45                         }{Alias: "alice", Password: "123456"},
46                         respWant: &Response{
47                                 Status: "fail",
48                                 Msg:    "wallet not found, please check that the wallet is open",
49                         },
50                 },
51                 {
52                         path:    "/error",
53                         request: nil,
54                         respWant: &Response{
55                                 Status: "fail",
56                                 Msg:    "wallet not found, please check that the wallet is open",
57                         },
58                 },
59                 {
60                         path:    "/",
61                         request: nil,
62                         respWant: &Response{
63                                 Status: "",
64                                 Msg:    "",
65                         },
66                 },
67                 {
68                         path: "/create-access-token",
69                         request: struct {
70                                 ID   string `json:"id"`
71                                 Type string `json:"type"`
72                         }{ID: "test-access-id", Type: "test-access-type"},
73                         respWant: &Response{
74                                 Status: "success",
75                                 Msg:    "",
76                                 Data:   map[string]interface{}{"id": "test-access-id", "type": "test-access-type", "token": "test-access-id:440d87ae0d625a7fcf076275b18372e09a0899e37ec86398879388de90cb0c67"},
77                         },
78                 },
79                 {
80                         path:    "/gas-rate",
81                         request: nil,
82                         respWant: &Response{
83                                 Status: "success",
84                                 Msg:    "",
85                                 Data:   map[string]interface{}{"gasRate": 1000},
86                         },
87                 },
88         }
89
90         for _, c := range cases {
91                 response = &Response{}
92                 client.Call(context.Background(), c.path, c.request, &response)
93
94                 if !testutil.DeepEqual(response.Status, c.respWant.Status) {
95                         t.Errorf(`got=%#v; want=%#v`, response.Status, c.respWant.Status)
96                 }
97         }
98 }