summaryrefslogtreecommitdiff
path: root/service/headers/service_test.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-06-24 14:43:13 +0300
committerWolfy-J <[email protected]>2019-06-24 14:43:13 +0300
commitee2396c406aa4cc455248fc3febc026eb38efe24 (patch)
treef571d5ab0cea08c1160af06897c867891b4faffd /service/headers/service_test.go
parentd5b8235cbe7e7e1f48b788accbc39d54c0977aa5 (diff)
more tests
Diffstat (limited to 'service/headers/service_test.go')
-rw-r--r--service/headers/service_test.go126
1 files changed, 126 insertions, 0 deletions
diff --git a/service/headers/service_test.go b/service/headers/service_test.go
new file mode 100644
index 00000000..a7b984bf
--- /dev/null
+++ b/service/headers/service_test.go
@@ -0,0 +1,126 @@
+package headers
+
+import (
+ "encoding/json"
+ "github.com/sirupsen/logrus"
+ "github.com/sirupsen/logrus/hooks/test"
+ "github.com/spiral/roadrunner/service"
+ rrhttp "github.com/spiral/roadrunner/service/http"
+ "github.com/stretchr/testify/assert"
+ "io/ioutil"
+ "net/http"
+ "testing"
+ "time"
+)
+
+type testCfg struct {
+ httpCfg string
+ headers string
+ target string
+}
+
+func (cfg *testCfg) Get(name string) service.Config {
+ if name == rrhttp.ID {
+ return &testCfg{target: cfg.httpCfg}
+ }
+
+ if name == ID {
+ return &testCfg{target: cfg.headers}
+ }
+ return nil
+}
+
+func (cfg *testCfg) Unmarshal(out interface{}) error {
+ return json.Unmarshal([]byte(cfg.target), out)
+}
+
+func Test_RequestHeaders(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(rrhttp.ID, &rrhttp.Service{})
+ c.Register(ID, &Service{})
+
+ assert.NoError(t, c.Init(&testCfg{
+ headers: `{"request":{"input": "custom-header"}}`,
+ httpCfg: `{
+ "enable": true,
+ "address": ":6029",
+ "maxRequestSize": 1024,
+ "workers":{
+ "command": "php ../../tests/http/client.php header pipes",
+ "relay": "pipes",
+ "pool": {
+ "numWorkers": 1,
+ "allocateTimeout": 10000000,
+ "destroyTimeout": 10000000
+ }
+ }
+ }`}))
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ req, err := http.NewRequest("GET", "http://localhost:6029?hello=value", 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, 200, r.StatusCode)
+ assert.Equal(t, "CUSTOM-HEADER", string(b))
+}
+
+func Test_ResponseHeaders(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(rrhttp.ID, &rrhttp.Service{})
+ c.Register(ID, &Service{})
+
+ assert.NoError(t, c.Init(&testCfg{
+ headers: `{"response":{"output": "output-header"},"request":{"input": "custom-header"}}`,
+ httpCfg: `{
+ "enable": true,
+ "address": ":6029",
+ "maxRequestSize": 1024,
+ "workers":{
+ "command": "php ../../tests/http/client.php header pipes",
+ "relay": "pipes",
+ "pool": {
+ "numWorkers": 1,
+ "allocateTimeout": 10000000,
+ "destroyTimeout": 10000000
+ }
+ }
+ }`}))
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ req, err := http.NewRequest("GET", "http://localhost:6029?hello=value", nil)
+ assert.NoError(t, err)
+
+ r, err := http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+ defer r.Body.Close()
+
+ v := r.Header.Get("output")
+ assert.Equal(t, "output-header", v)
+
+ b, err := ioutil.ReadAll(r.Body)
+ assert.NoError(t, err)
+
+ assert.NoError(t, err)
+ assert.Equal(t, 200, r.StatusCode)
+ assert.Equal(t, "CUSTOM-HEADER", string(b))
+}