-
Notifications
You must be signed in to change notification settings - Fork 10
/
document_test.go
127 lines (104 loc) · 3.54 KB
/
document_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package go_openrpc_reflect
import (
"encoding/json"
"net"
"regexp"
"testing"
"time"
"github.com/etclabscore/go-openrpc-reflect/internal/fakearithmetic"
meta_schema "github.com/open-rpc/meta-schema"
"github.com/stretchr/testify/assert"
)
//var TestMetaRegisterer = &MetaRegistererTester{}
var TestMetaRegisterer = &MetaT{
GetServersFn: getServers,
GetInfoFn: getInfo,
GetExternalDocsFn: getExternalDocs,
}
func getInfo() (info *meta_schema.InfoObject) {
title := "Calculator API"
version := time.Now().Format(time.RFC3339)
return &meta_schema.InfoObject{
Title: (*meta_schema.InfoObjectProperties)(&title),
Description: nil,
TermsOfService: nil,
Version: (*meta_schema.InfoObjectVersion)(&version),
Contact: nil,
License: nil,
}
}
func getExternalDocs() (*meta_schema.ExternalDocumentationObject) {
return nil
}
func getServers() func (listeners []net.Listener) (*meta_schema.Servers, error) {
return StandardReflector.GetServers()
}
func newDocument() *Document {
return &Document{}
}
func TestDocument_Discover(t *testing.T) {
t.Run("standard", func(t *testing.T) {
d := newDocument().WithMeta(TestMetaRegisterer).WithReflector(StandardReflector)
listener, err := net.Listen("tcp", "127.0.0.1:0")
assert.NoError(t, err)
assert.NotNil(t, listener)
defer listener.Close()
d.RegisterListener(listener)
calculatorRPC := new(fakearithmetic.CalculatorRPC)
d.RegisterReceiver(calculatorRPC)
out, err := d.Discover()
if !assert.NoError(t, err) {
t.Fatal("discover error, fatal")
}
b, _ := json.MarshalIndent(out, "", " ")
t.Log(string(b))
jsonTests := map[string]interface{}{
"openrpc": "1.2.6",
"info.title": "Calculator API",
"info.version": regexp.MustCompile(time.Now().Format("2006")),
"servers.0.url": listener.Addr().String(),
"methods.#": float64(5),
"methods.0.name": "CalculatorRPC.Add",
"methods.0.params.#": float64(1),
"methods.0.params.0.name": "arg",
"methods.0.params.0.schema.properties.a.type": "integer",
"methods.0.result.name": "reply",
"methods.0.result.schema.type": "integer",
"components": nil,
}
testJSON(t, b, jsonTests)
})
t.Run("ethereum", func(t *testing.T) {
d := newDocument().WithMeta(TestMetaRegisterer).WithReflector(EthereumReflector)
listener, err := net.Listen("tcp", "127.0.0.1:0")
defer listener.Close()
if !assert.NoError(t, err) {
t.Fatal("discover error, fatal")
}
d.RegisterListener(listener)
calculator := new(fakearithmetic.Calculator)
d.RegisterReceiver(calculator)
out, err := d.Discover()
if !assert.NoError(t, err) {
t.Fatal("discover error, fatal")
}
b, _ := json.MarshalIndent(out, "", " ")
str := string(b)
t.Log(str)
jsonTests := map[string]interface{}{
"openrpc": "1.2.6",
"info.title": "Calculator API",
"info.version": regexp.MustCompile(time.Now().Format("2006")),
"servers.0.url": listener.Addr().String(),
"methods.#": float64(13),
"methods.0.name": "calculator_add",
"methods.0.params.#": float64(2),
"methods.0.params.0.name": "argA",
"methods.0.params.0.schema.type": "integer",
"methods.0.result.name": "int",
"methods.0.result.schema.type": "integer",
"components": nil,
}
testJSON(t, b, jsonTests)
})
}