summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-13 23:26:09 +0300
committerWolfy-J <[email protected]>2018-06-13 23:26:09 +0300
commita74169fef1316c2b2ff3598576b57fd9617be8fe (patch)
tree921623fd2314f46a619a206ff4febe83d9d37d20
parent6634465106fa1ed94445b5a546b1af5b87e7944c (diff)
static service test
-rw-r--r--php-src/tests/http/sample.txt1
-rw-r--r--service/static/service.go1
-rw-r--r--service/static/service_test.go176
3 files changed, 178 insertions, 0 deletions
diff --git a/php-src/tests/http/sample.txt b/php-src/tests/http/sample.txt
new file mode 100644
index 00000000..eed7e79a
--- /dev/null
+++ b/php-src/tests/http/sample.txt
@@ -0,0 +1 @@
+sample \ No newline at end of file
diff --git a/service/static/service.go b/service/static/service.go
index 1631f610..a89cb495 100644
--- a/service/static/service.go
+++ b/service/static/service.go
@@ -58,6 +58,7 @@ func (s *Service) Stop() {}
// middleware must return true if request/response pair is handled withing the middleware.
func (s *Service) middleware(w http.ResponseWriter, r *http.Request) bool {
fPath := r.URL.Path
+
if !strings.HasPrefix(fPath, "/") {
fPath = "/" + fPath
}
diff --git a/service/static/service_test.go b/service/static/service_test.go
new file mode 100644
index 00000000..7acc2880
--- /dev/null
+++ b/service/static/service_test.go
@@ -0,0 +1,176 @@
+package static
+
+import (
+ "testing"
+ "github.com/sirupsen/logrus/hooks/test"
+ "github.com/sirupsen/logrus"
+ "github.com/spiral/roadrunner/service"
+ "github.com/stretchr/testify/assert"
+ "time"
+ rrhttp "github.com/spiral/roadrunner/service/http"
+ "encoding/json"
+ "io/ioutil"
+ "net/http"
+ "os"
+ "bytes"
+ "io"
+)
+
+type testCfg struct {
+ httpCfg string
+ static 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.static}
+ }
+ return nil
+}
+func (cfg *testCfg) Unmarshal(out interface{}) error {
+ return json.Unmarshal([]byte(cfg.target), out)
+}
+
+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 Test_Files(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{
+ static: `{"enable":true, "dir":"../../php-src/tests/http", "forbid":[]}`,
+ httpCfg: `{
+ "enable": true,
+ "address": ":6029",
+ "maxRequest": 1024,
+ "uploads": {
+ "dir": ` + tmpDir() + `,
+ "forbid": []
+ },
+ "workers":{
+ "command": "php ../../php-src/tests/http/client.php pid pipes",
+ "relay": "pipes",
+ "pool": {
+ "numWorkers": 1,
+ "allocateTimeout": 10000000,
+ "destroyTimeout": 10000000
+ }
+ }
+ }`}))
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ b, _, _ := get("http://localhost:6029/sample.txt")
+ assert.Equal(t, "sample", b)
+}
+
+func Test_Files_Forbid(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{
+ static: `{"enable":true, "dir":"../../php-src/tests/http", "forbid":[".php"]}`,
+ httpCfg: `{
+ "enable": true,
+ "address": ":6029",
+ "maxRequest": 1024,
+ "uploads": {
+ "dir": ` + tmpDir() + `,
+ "forbid": []
+ },
+ "workers":{
+ "command": "php ../../php-src/tests/http/client.php pid pipes",
+ "relay": "pipes",
+ "pool": {
+ "numWorkers": 1,
+ "allocateTimeout": 10000000,
+ "destroyTimeout": 10000000
+ }
+ }
+ }`}))
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ b, _, _ := get("http://localhost:6029/client.php")
+ assert.NotEqual(t, "sample", b)
+}
+
+func Test_Files_NotForbid(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{
+ static: `{"enable":true, "dir":"../../php-src/tests/http", "forbid":[]}`,
+ httpCfg: `{
+ "enable": true,
+ "address": ":6029",
+ "maxRequest": 1024,
+ "uploads": {
+ "dir": ` + tmpDir() + `,
+ "forbid": []
+ },
+ "workers":{
+ "command": "php ../../php-src/tests/http/client.php pid pipes",
+ "relay": "pipes",
+ "pool": {
+ "numWorkers": 1,
+ "allocateTimeout": 10000000,
+ "destroyTimeout": 10000000
+ }
+ }
+ }`}))
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ b, _, _ := get("http://localhost:6029/client.php")
+ assert.NotEqual(t, all("../../php-src/tests/http/client.php")+"s", b)
+}
+
+func tmpDir() string {
+ p := os.TempDir()
+ r, _ := json.Marshal(p)
+
+ return string(r)
+}
+
+func all(fn string) string {
+ f, _ := os.Open(fn)
+ defer f.Close()
+
+ b := &bytes.Buffer{}
+ io.Copy(b, f)
+
+ return b.String()
+}