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
|
package http
import (
"testing"
"github.com/spiral/roadrunner"
"os"
"github.com/stretchr/testify/assert"
"net/http"
"context"
"io/ioutil"
)
// get request and return body
func get(url string) (string, *http.Response, error) {
r, err := http.Get(url)
if err != nil {
return "", nil, err
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
return string(b), r, err
}
func TestServer_Echo(t *testing.T) {
st := &Server{
cfg: &Config{
MaxRequest: 1024,
Uploads: &UploadsConfig{
Dir: os.TempDir(),
Forbid: []string{},
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
Command: "php ../../php-src/tests/http/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
AllocateTimeout: 10000000,
DestroyTimeout: 10000000,
},
}),
}
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
hs := &http.Server{Addr: ":8077", Handler: st,}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
body, r, err := get("http://localhost:8077/?hello=world")
assert.NoError(t, err)
assert.Equal(t, 200, r.StatusCode)
assert.Equal(t, "WORLD", body)
}
func TestServer_Headers(t *testing.T) {
st := &Server{
cfg: &Config{
MaxRequest: 1024,
Uploads: &UploadsConfig{
Dir: os.TempDir(),
Forbid: []string{},
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
Command: "php ../../php-src/tests/http/client.php header pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
AllocateTimeout: 10000000,
DestroyTimeout: 10000000,
},
}),
}
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
hs := &http.Server{Addr: ":8077", Handler: st,}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
req, err := http.NewRequest("GET", "http://localhost:8077?hello=world", nil)
assert.NoError(t, err)
req.Header.Add("input", "sample")
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, 200, r.StatusCode)
assert.Equal(t, "world", r.Header.Get("Header"))
assert.Equal(t, "SAMPLE", string(b))
}
|