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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
package http
import (
"testing"
"github.com/spiral/roadrunner/service"
"github.com/sirupsen/logrus/hooks/test"
"github.com/sirupsen/logrus"
"encoding/json"
"github.com/stretchr/testify/assert"
"os"
"time"
"net/http"
"io/ioutil"
)
type testCfg struct{ httpCfg string }
func (cfg *testCfg) Get(name string) service.Config {
if name == Name {
return &testCfg{cfg.httpCfg}
}
return nil
}
func (cfg *testCfg) Unmarshal(out interface{}) error {
json.Unmarshal([]byte(cfg.httpCfg), out)
return nil
}
func Test_Service_NoConfig(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
c := service.NewContainer(logger)
c.Register(Name, &Service{})
assert.NoError(t, c.Init(&testCfg{`{}`}))
s, st := c.Get(Name)
assert.NotNil(t, s)
assert.Equal(t, service.StatusRegistered, st)
}
func Test_Service_Configure_Disable(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
c := service.NewContainer(logger)
c.Register(Name, &Service{})
assert.NoError(t, c.Init(&testCfg{`{
"enable": false,
"address": ":8070",
"maxRequest": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
"forbid": []
},
"workers":{
"command": "php ../../php-src/tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
"allocateTimeout": 10000000,
"destroyTimeout": 10000000
}
}
}`}))
s, st := c.Get(Name)
assert.NotNil(t, s)
assert.Equal(t, service.StatusRegistered, st)
}
func Test_Service_Configure_Enable(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
c := service.NewContainer(logger)
c.Register(Name, &Service{})
assert.NoError(t, c.Init(&testCfg{`{
"enable": true,
"address": ":8070",
"maxRequest": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
"forbid": []
},
"workers":{
"command": "php ../../php-src/tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
"allocateTimeout": 10000000,
"destroyTimeout": 10000000
}
}
}`}))
s, st := c.Get(Name)
assert.NotNil(t, s)
assert.Equal(t, service.StatusConfigured, st)
}
func Test_Service_Echo(t *testing.T) {
logger, _ := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
c := service.NewContainer(logger)
c.Register(Name, &Service{})
assert.NoError(t, c.Init(&testCfg{`{
"enable": true,
"address": ":8029",
"maxRequest": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
"forbid": []
},
"workers":{
"command": "php ../../php-src/tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
"allocateTimeout": 10000000,
"destroyTimeout": 10000000
}
}
}`}))
s, st := c.Get(Name)
assert.NotNil(t, s)
assert.Equal(t, service.StatusConfigured, st)
go func() { c.Serve() }()
time.Sleep(time.Millisecond * 10)
defer c.Stop()
req, err := http.NewRequest("GET", "http://localhost:8029?hello=world", nil)
assert.NoError(t, err)
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, 201, r.StatusCode)
assert.Equal(t, "WORLD", string(b))
}
func tmpDir() string {
p := os.TempDir()
r, _ := json.Marshal(p)
return string(r)
}
|