-
Notifications
You must be signed in to change notification settings - Fork 14
/
swagger_test.go
97 lines (91 loc) · 2.72 KB
/
swagger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package docs_test
import (
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
"testing"
"unsafe"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server/api"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pundiai/fx-core/v8/testutil/helpers"
)
func TestSwaggerConfig(t *testing.T) {
data, err := os.ReadFile("config.json")
require.NoError(t, err)
var c config
require.NoError(t, json.Unmarshal(data, &c))
assert.Equal(t, "2.0", c.Swagger)
assert.Equal(t, "0.4.0", c.Info.Version)
assert.Len(t, c.Apis, 23)
app := helpers.NewApp()
clientCtx := client.Context{
InterfaceRegistry: app.InterfaceRegistry(),
}
apiSrv := api.New(clientCtx, app.Logger(), nil)
app.RegisterAPIRoutes(apiSrv, srvconfig.APIConfig{Swagger: true})
assert.NotNil(t, apiSrv.Router.Path("/swagger/"))
handler := reflect.Indirect(reflect.ValueOf(apiSrv.GRPCGatewayRouter)).Field(0).MapRange()
route := make(map[string]int)
for handler.Next() {
for i := 0; i < handler.Value().Len(); i++ {
field := handler.Value().Index(i).Field(0)
pat := reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface().(runtime.Pattern)
split := strings.Split(pat.String(), "/")
assert.Greater(t, len(split), 3)
if len(split) > 4 && split[3] != "v1" && split[3] != "v1beta1" && (split[4] == "v1" || split[4] == "v1beta1") {
split[3] = fmt.Sprintf("%s/%s", split[3], split[4])
}
key := fmt.Sprintf("%s/%s/%s", split[1], split[2], split[3])
if key == "ibc/apps/transfer/v1" {
key = "ibc/applications/transfer/v1"
}
if key == "fx/crosschain/v1" {
key = "fx/gravity/crosschain/v1"
}
route[key] = route[key] + 1
}
if handler.Key().String() == "POST" {
assert.Equal(t, 7, handler.Value().Len())
}
if handler.Key().String() == "GET" {
assert.Equal(t, 196, handler.Value().Len())
}
}
assert.Len(t, route, 31)
ignoreLen := len(route) - len(c.Apis)
for _, v := range c.Apis {
for key := range route {
if strings.HasPrefix(v.Url, "./tmp-swagger-gen/"+key) {
delete(route, key)
}
}
}
// ignore routes:
// 1. other/v1/gas_price
// 2. fx/other/gas_price
// 3. fx/base/v1
// 4. ibc/applications/transfer/v1
// 5. ibc/core/channel/v1
// 6. ibc/core/client/v1
// 7. ibc/core/connection/v1
// 8. cosmos/gov/v1beta1
assert.Len(t, route, ignoreLen)
assert.Len(t, route, 8)
}
type config struct {
Swagger string `json:"swagger"`
Info struct {
Title string `json:"title"`
Description string `json:"description"`
Version string `json:"version"`
} `json:"info"`
Apis []struct {
Url string `json:"url"`
} `json:"apis"`
}