forked from FactomProject/factomd
/
wsapi.go
55 lines (43 loc) · 1.49 KB
/
wsapi.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
package testHelper
import (
"bytes"
"fmt"
"net/http"
"strconv"
"github.com/FactomProject/factomd/engine"
"github.com/FactomProject/factomd/wsapi"
)
const defaultTestPort = 8080
func InitTestState() {
state := CreateAndPopulateTestStateAndStartValidator()
state.SetPort(defaultTestPort)
if wsapi.Servers == nil {
wsapi.Servers = make(map[string]*wsapi.Server)
}
port := strconv.Itoa(defaultTestPort)
if wsapi.Servers[port] == nil {
server := wsapi.InitServer(state)
wsapi.Servers[port] = server
}
}
func getAPIUrl() string {
return "http://localhost:" + fmt.Sprint(engine.GetFnodes()[0].State.GetPort()) + "/debug"
}
func postRequest(jsonStr string) (*http.Response, error) {
req, err := http.NewRequest("POST", getAPIUrl(), bytes.NewBuffer([]byte(jsonStr)))
if err != nil {
return nil, err
}
req.Header.Set("content-type", "text/plain;")
client := &http.Client{}
return client.Do(req)
}
func SetInputFilter(apiRegex string) (*http.Response, error) {
return postRequest(`{"jsonrpc": "2.0", "id": 0, "method": "message-filter", "params":{"output-regex":"", "input-regex":"` + apiRegex + `"}}`)
}
func SetOutputFilter(apiRegex string) (*http.Response, error) {
return postRequest(`{"jsonrpc": "2.0", "id": 0, "method": "message-filter", "params":{"output-regex":"` + apiRegex + `", "input-regex":""}}`)
}
func DebugCall(method string, params string) (*http.Response, error) {
return postRequest(fmt.Sprintf(`{"jsonrpc": "2.0", "id": 0, "method": "%s", "params":%s}`, method, params))
}