forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_http_source_test.go
77 lines (70 loc) · 1.47 KB
/
source_http_source_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
package test
import (
"net/http"
"testing"
"time"
httpserver "github.com/hootrhino/rulex/plugin/http_server"
"github.com/hootrhino/rulex/utils"
"github.com/hootrhino/rulex/typex"
)
/*
*
* Test_data_to_http
*
*/
func Test_http_source(t *testing.T) {
engine := RunTestEngine()
engine.Start()
hh := httpserver.NewHttpApiServer()
// HttpApiServer loaded default
if err := engine.LoadPlugin("plugin.http_server", hh); err != nil {
t.Fatal("Rule load failed:", err)
}
// http Inend
httpInend := typex.NewInEnd(
"HTTP",
"Test",
"Test", map[string]interface{}{
"port": 8088,
"host": "127.0.0.1",
},
)
if err := engine.LoadInEnd(httpInend); err != nil {
t.Fatal("httpInend load failed:", err)
}
//
// Load Rule [{"co2":10,"hum":30,"lex":22,"temp":100}]
//
callback :=
`Actions = {
function(data)
print("From http===>", data)
return false, data
end
}`
rule1 := typex.NewRule(engine,
"uuid1",
"rule1",
"rule1",
[]string{httpInend.UUID},
[]string{},
`function Success() print("[Test_data_to_http Success Callback]=> OK") end`,
callback,
`function Failed(error) print("[Test_data_to_http Failed Callback]", error) end`)
if err := engine.LoadRule(rule1); err != nil {
t.Fatal(err)
}
res, err := utils.Post(*http.DefaultClient,
map[string]interface{}{"data": "ok, let's go!"},
"http://127.0.0.1:8088/in",
map[string]string{})
if err != nil {
t.Fatal(err)
}
t.Log(res)
//
//
//
time.Sleep(3 * time.Second)
engine.Stop()
}