From 9402ea1e88d6e63b8b0388ad662d20b727b940d0 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 30 Nov 2020 12:54:41 +0300 Subject: Static plugin initial commit --- plugins/static/tests/configs/.rr-http-static.yaml | 30 ++ plugins/static/tests/static_plugin_test.go | 142 +++++++ plugins/static/tests/static_tests_old.go | 476 ++++++++++++++++++++++ 3 files changed, 648 insertions(+) create mode 100644 plugins/static/tests/configs/.rr-http-static.yaml create mode 100644 plugins/static/tests/static_plugin_test.go create mode 100644 plugins/static/tests/static_tests_old.go (limited to 'plugins/static/tests') diff --git a/plugins/static/tests/configs/.rr-http-static.yaml b/plugins/static/tests/configs/.rr-http-static.yaml new file mode 100644 index 00000000..f717dcf8 --- /dev/null +++ b/plugins/static/tests/configs/.rr-http-static.yaml @@ -0,0 +1,30 @@ +server: + command: "php ../../../tests/http/client.php pid pipes" + user: "" + group: "" + env: + "RR_HTTP": "true" + relay: "pipes" + relayTimeout: "20s" + +http: + debug: true + address: 127.0.0.1:21603 + maxRequestSize: 1024 + middleware: [ "gzip", "static" ] + trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ] + uploads: + forbid: [ ".php", ".exe", ".bat" ] + static: + dir: "../../../tests" + forbid: [ ".php", ".htaccess" ] + request: + "Example-Request-Header": "Value" + # Automatically add headers to every response. + response: + "X-Powered-By": "RoadRunner" + pool: + numWorkers: 2 + maxJobs: 0 + allocateTimeout: 60s + destroyTimeout: 60s \ No newline at end of file diff --git a/plugins/static/tests/static_plugin_test.go b/plugins/static/tests/static_plugin_test.go new file mode 100644 index 00000000..ef09952d --- /dev/null +++ b/plugins/static/tests/static_plugin_test.go @@ -0,0 +1,142 @@ +package tests + +import ( + "bytes" + "io" + "io/ioutil" + "net/http" + "os" + "os/signal" + "sync" + "syscall" + "testing" + "time" + + j "github.com/json-iterator/go" + "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/plugins/config" + "github.com/spiral/roadrunner/v2/plugins/gzip" + httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" + "github.com/spiral/roadrunner/v2/plugins/logger" + "github.com/spiral/roadrunner/v2/plugins/server" + "github.com/spiral/roadrunner/v2/plugins/static" + "github.com/stretchr/testify/assert" +) + +var json = j.ConfigCompatibleWithStandardLibrary + +func TestStaticPlugin(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-http-static.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &gzip.Gzip{}, + &static.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + go func() { + tt := time.NewTimer(time.Second * 10) + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + t.Run("ServeSample", serveStaticSample) + wg.Wait() +} + +func serveStaticSample(t *testing.T) { + b, r, err := get("http://localhost:21603/sample.txt") + assert.NoError(t, err) + assert.Equal(t, "sample", b) + _ = r.Body.Close() +} + +func get(url string) (string, *http.Response, error) { + r, err := http.Get(url) + if err != nil { + return "", nil, err + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + return "", nil, err + } + + err = r.Body.Close() + if err != nil { + return "", nil, err + } + + return string(b), r, err +} + +func tmpDir() string { + p := os.TempDir() + + r, _ := j.Marshal(p) + + return string(r) +} + +func all(fn string) string { + f, _ := os.Open(fn) + + b := new(bytes.Buffer) + _, err := io.Copy(b, f) + if err != nil { + return "" + } + + err = f.Close() + if err != nil { + return "" + } + + return b.String() +} diff --git a/plugins/static/tests/static_tests_old.go b/plugins/static/tests/static_tests_old.go new file mode 100644 index 00000000..96a65178 --- /dev/null +++ b/plugins/static/tests/static_tests_old.go @@ -0,0 +1,476 @@ +package tests + +//package static +// +//import ( +//"bytes" +//json "github.com/json-iterator/go" +//"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" +//"io/ioutil" +//"net/http" +//"os" +//"testing" +//"time" +//) +// +//type testCfg struct { +// httpCfg string +// static string +// target string +//} +// + +// +//func Test_Disabled(t *testing.T) { +// logger, _ := test.NewNullLogger() +// logger.SetLevel(logrus.DebugLevel) +// +// c := service.NewContainer(logger) +// c.Register(ID, &Service{}) +// +// assert.NoError(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"../../tests", "forbid":[]}`, +// })) +// +// s, st := c.Get(ID) +// assert.NotNil(t, s) +// assert.Equal(t, service.StatusInactive, st) +//} +// +//func Test_Files_Disable(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":false, "dir":"../../tests", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8030", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Second) +// +// b, _, err := get("http://localhost:8030/client.php?hello=world") +// if err != nil { +// t.Fatal(err) +// } +// assert.Equal(t, "WORLD", b) +// c.Stop() +//} +// +//func Test_Files_Error(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.Error(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"dir/invalid", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8031", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +//} +// +//func Test_Files_Error2(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.Error(t, c.Init(&testCfg{ +// static: `{"enable":true, "dir":"dir/invalid", "forbid":[".php"]`, +// httpCfg: `{ +// "enable": true, +// "address": ":8032", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +//} +// +//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":"../../tests", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8033", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// time.Sleep(time.Millisecond * 500) +// +// b, _, err := get("http://localhost:8033/client.php?hello=world") +// if err != nil { +// t.Fatal(err) +// } +// assert.Equal(t, "WORLD", b) +// c.Stop() +//} +// +//func Test_Files_Always(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":"../../tests", "forbid":[".php"], "always":[".ico"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8034", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Millisecond * 500) +// +// _, r, err := get("http://localhost:8034/favicon.ico") +// if err != nil { +// t.Fatal(err) +// } +// assert.Equal(t, 404, r.StatusCode) +// c.Stop() +//} +// +//func Test_Files_NotFound(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":"../../tests", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8035", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Millisecond * 500) +// +// b, _, _ := get("http://localhost:8035/client.XXX?hello=world") +// assert.Equal(t, "WORLD", b) +// c.Stop() +//} +// +//func Test_Files_Dir(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":"../../tests", "forbid":[".php"]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8036", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php echo pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// time.Sleep(time.Millisecond * 500) +// +// b, _, _ := get("http://localhost:8036/http?hello=world") +// assert.Equal(t, "WORLD", b) +// c.Stop() +//} +// +//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":"../../tests", "forbid":[]}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8037", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php pid pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Millisecond * 500) +// +// b, _, _ := get("http://localhost:8037/client.php") +// assert.Equal(t, all("../../tests/client.php"), b) +// assert.Equal(t, all("../../tests/client.php"), b) +// c.Stop() +//} +// +//func TestStatic_Headers(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":"../../tests", "forbid":[], "request":{"input": "custom-header"}, "response":{"output": "output-header"}}`, +// httpCfg: `{ +// "enable": true, +// "address": ":8037", +// "maxRequestSize": 1024, +// "uploads": { +// "dir": ` + tmpDir() + `, +// "forbid": [] +// }, +// "workers":{ +// "command": "php ../../tests/http/client.php pid pipes", +// "relay": "pipes", +// "pool": { +// "numWorkers": 1, +// "allocateTimeout": 10000000, +// "destroyTimeout": 10000000 +// } +// } +// }`})) +// +// go func() { +// err := c.Serve() +// if err != nil { +// t.Errorf("serve error: %v", err) +// } +// }() +// +// time.Sleep(time.Millisecond * 500) +// +// req, err := http.NewRequest("GET", "http://localhost:8037/client.php", nil) +// if err != nil { +// t.Fatal(err) +// } +// +// resp, err := http.DefaultClient.Do(req) +// if err != nil { +// t.Fatal(err) +// } +// +// if resp.Header.Get("Output") != "output-header" { +// t.Fatal("can't find output header in response") +// } +// +// +// b, err := ioutil.ReadAll(resp.Body) +// if err != nil { +// t.Fatal(err) +// } +// +// assert.Equal(t, all("../../tests/client.php"), string(b)) +// assert.Equal(t, all("../../tests/client.php"), string(b)) +// c.Stop() +//} +// +//func get(url string) (string, *http.Response, error) { +// r, err := http.Get(url) +// if err != nil { +// return "", nil, err +// } +// +// b, err := ioutil.ReadAll(r.Body) +// if err != nil { +// return "", nil, err +// } +// +// err = r.Body.Close() +// if err != nil { +// return "", nil, err +// } +// +// return string(b), r, err +//} +// +//func tmpDir() string { +// p := os.TempDir() +// j := json.ConfigCompatibleWithStandardLibrary +// r, _ := j.Marshal(p) +// +// return string(r) +//} +// +//func all(fn string) string { +// f, _ := os.Open(fn) +// +// b := &bytes.Buffer{} +// _, err := io.Copy(b, f) +// if err != nil { +// return "" +// } +// +// err = f.Close() +// if err != nil { +// return "" +// } +// +// return b.String() +//} -- cgit v1.2.3 From ddac6f8fcd8bf740981d2f78d573b46bb61acf56 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 30 Nov 2020 14:11:22 +0300 Subject: Finish plugin for static --- .../tests/configs/.rr-http-static-disabled.yaml | 30 ++ .../configs/.rr-http-static-files-disable.yaml | 30 ++ .../tests/configs/.rr-http-static-files.yaml | 31 ++ plugins/static/tests/configs/.rr-http-static.yaml | 7 +- plugins/static/tests/static_plugin_test.go | 284 ++++++++++++ plugins/static/tests/static_tests_old.go | 476 --------------------- 6 files changed, 378 insertions(+), 480 deletions(-) create mode 100644 plugins/static/tests/configs/.rr-http-static-disabled.yaml create mode 100644 plugins/static/tests/configs/.rr-http-static-files-disable.yaml create mode 100644 plugins/static/tests/configs/.rr-http-static-files.yaml delete mode 100644 plugins/static/tests/static_tests_old.go (limited to 'plugins/static/tests') diff --git a/plugins/static/tests/configs/.rr-http-static-disabled.yaml b/plugins/static/tests/configs/.rr-http-static-disabled.yaml new file mode 100644 index 00000000..d0b9b388 --- /dev/null +++ b/plugins/static/tests/configs/.rr-http-static-disabled.yaml @@ -0,0 +1,30 @@ +server: + command: "php ../../../tests/http/client.php pid pipes" + user: "" + group: "" + env: + "RR_HTTP": "true" + relay: "pipes" + relayTimeout: "20s" + +http: + debug: true + address: 127.0.0.1:21234 + maxRequestSize: 1024 + middleware: [ "gzip", "static" ] + trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ] + uploads: + forbid: [ ".php", ".exe", ".bat" ] + static: + dir: "abc" #not exists + forbid: [ ".php", ".htaccess" ] + request: + "Example-Request-Header": "Value" + # Automatically add headers to every response. + response: + "X-Powered-By": "RoadRunner" + pool: + numWorkers: 2 + maxJobs: 0 + allocateTimeout: 60s + destroyTimeout: 60s \ No newline at end of file diff --git a/plugins/static/tests/configs/.rr-http-static-files-disable.yaml b/plugins/static/tests/configs/.rr-http-static-files-disable.yaml new file mode 100644 index 00000000..a3d814a3 --- /dev/null +++ b/plugins/static/tests/configs/.rr-http-static-files-disable.yaml @@ -0,0 +1,30 @@ +server: + command: "php ../../../tests/http/client.php echo pipes" + user: "" + group: "" + env: + "RR_HTTP": "true" + relay: "pipes" + relayTimeout: "20s" + +http: + debug: true + address: 127.0.0.1:45877 + maxRequestSize: 1024 + middleware: [ "gzip", "static" ] + trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ] + uploads: + forbid: [ ".php", ".exe", ".bat" ] + static: + dir: "../../../tests" + forbid: [ ".php" ] + request: + "Example-Request-Header": "Value" + # Automatically add headers to every response. + response: + "X-Powered-By": "RoadRunner" + pool: + numWorkers: 2 + maxJobs: 0 + allocateTimeout: 60s + destroyTimeout: 60s \ No newline at end of file diff --git a/plugins/static/tests/configs/.rr-http-static-files.yaml b/plugins/static/tests/configs/.rr-http-static-files.yaml new file mode 100644 index 00000000..35938b80 --- /dev/null +++ b/plugins/static/tests/configs/.rr-http-static-files.yaml @@ -0,0 +1,31 @@ +server: + command: "php ../../../tests/http/client.php echo pipes" + user: "" + group: "" + env: + "RR_HTTP": "true" + relay: "pipes" + relayTimeout: "20s" + +http: + debug: true + address: 127.0.0.1:34653 + maxRequestSize: 1024 + middleware: [ "gzip", "static" ] + trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ] + uploads: + forbid: [ ".php", ".exe", ".bat" ] + static: + dir: "../../../tests" + forbid: [ ".php", ".htaccess" ] + always: [ ".ico" ] + request: + "Example-Request-Header": "Value" + # Automatically add headers to every response. + response: + "X-Powered-By": "RoadRunner" + pool: + numWorkers: 2 + maxJobs: 0 + allocateTimeout: 60s + destroyTimeout: 60s \ No newline at end of file diff --git a/plugins/static/tests/configs/.rr-http-static.yaml b/plugins/static/tests/configs/.rr-http-static.yaml index f717dcf8..80a1fa7f 100644 --- a/plugins/static/tests/configs/.rr-http-static.yaml +++ b/plugins/static/tests/configs/.rr-http-static.yaml @@ -17,12 +17,11 @@ http: forbid: [ ".php", ".exe", ".bat" ] static: dir: "../../../tests" - forbid: [ ".php", ".htaccess" ] + forbid: [ "" ] request: - "Example-Request-Header": "Value" - # Automatically add headers to every response. + "input": "custom-header" response: - "X-Powered-By": "RoadRunner" + "output": "output-header" pool: numWorkers: 2 maxJobs: 0 diff --git a/plugins/static/tests/static_plugin_test.go b/plugins/static/tests/static_plugin_test.go index ef09952d..5717d1e8 100644 --- a/plugins/static/tests/static_plugin_test.go +++ b/plugins/static/tests/static_plugin_test.go @@ -12,8 +12,10 @@ import ( "testing" "time" + "github.com/golang/mock/gomock" j "github.com/json-iterator/go" "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/mocks" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/gzip" httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" @@ -87,9 +89,43 @@ func TestStaticPlugin(t *testing.T) { }() t.Run("ServeSample", serveStaticSample) + t.Run("StaticNotForbid", staticNotForbid) + t.Run("StaticHeaders", staticHeaders) wg.Wait() } +func staticHeaders(t *testing.T) { + req, err := http.NewRequest("GET", "http://localhost:21603/client.php", nil) + if err != nil { + t.Fatal(err) + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + + if resp.Header.Get("Output") != "output-header" { + t.Fatal("can't find output header in response") + } + + b, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, all("../../../tests/client.php"), string(b)) + assert.Equal(t, all("../../../tests/client.php"), string(b)) +} + +func staticNotForbid(t *testing.T) { + b, r, err := get("http://localhost:21603/client.php") + assert.NoError(t, err) + assert.Equal(t, all("../../../tests/client.php"), b) + assert.Equal(t, all("../../../tests/client.php"), b) + _ = r.Body.Close() +} + func serveStaticSample(t *testing.T) { b, r, err := get("http://localhost:21603/sample.txt") assert.NoError(t, err) @@ -97,6 +133,254 @@ func serveStaticSample(t *testing.T) { _ = r.Body.Close() } +func TestStaticDisabled(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-http-static-disabled.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &gzip.Gzip{}, + &static.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + go func() { + tt := time.NewTimer(time.Second * 10) + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + t.Run("StaticDisabled", staticDisabled) + wg.Wait() +} + +func staticDisabled(t *testing.T) { + _, r, err := get("http://localhost:21234/sample.txt") + assert.Error(t, err) + assert.Nil(t, r) +} + +func TestStaticFilesDisabled(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-http-static-files-disable.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &gzip.Gzip{}, + &static.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + go func() { + tt := time.NewTimer(time.Second * 10) + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + t.Run("StaticFilesDisabled", staticFilesDisabled) + wg.Wait() +} + +func staticFilesDisabled(t *testing.T) { + b, r, err := get("http://localhost:45877/client.php?hello=world") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "WORLD", b) + _ = r.Body.Close() +} + +func TestStaticFilesForbid(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-http-static-files.yaml", + Prefix: "rr", + } + + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Debug("http handler response received", "elapsed", gomock.Any(), "remote address", "127.0.0.1").AnyTimes() + mockLogger.EXPECT().Error("file open error", "error", gomock.Any()).AnyTimes() + + err = cont.RegisterAll( + cfg, + mockLogger, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &gzip.Gzip{}, + &static.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + go func() { + tt := time.NewTimer(time.Second * 10) + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + t.Run("StaticTestFilesDir", staticTestFilesDir) + t.Run("StaticNotFound", staticNotFound) + t.Run("StaticFilesForbid", staticFilesForbid) + t.Run("StaticFilesAlways", staticFilesAlways) + wg.Wait() +} + +func staticTestFilesDir(t *testing.T) { + b, r, err := get("http://localhost:34653/http?hello=world") + assert.NoError(t, err) + assert.Equal(t, "WORLD", b) + _ = r.Body.Close() +} + +func staticNotFound(t *testing.T) { + b, _, _ := get("http://localhost:34653/client.XXX?hello=world") + assert.Equal(t, "WORLD", b) +} + +func staticFilesAlways(t *testing.T) { + _, r, err := get("http://localhost:34653/favicon.ico") + assert.NoError(t, err) + assert.Equal(t, 404, r.StatusCode) + _ = r.Body.Close() +} + +func staticFilesForbid(t *testing.T) { + b, r, err := get("http://localhost:34653/client.php?hello=world") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "WORLD", b) + _ = r.Body.Close() +} + +// HELPERS func get(url string) (string, *http.Response, error) { r, err := http.Get(url) if err != nil { diff --git a/plugins/static/tests/static_tests_old.go b/plugins/static/tests/static_tests_old.go deleted file mode 100644 index 96a65178..00000000 --- a/plugins/static/tests/static_tests_old.go +++ /dev/null @@ -1,476 +0,0 @@ -package tests - -//package static -// -//import ( -//"bytes" -//json "github.com/json-iterator/go" -//"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" -//"io/ioutil" -//"net/http" -//"os" -//"testing" -//"time" -//) -// -//type testCfg struct { -// httpCfg string -// static string -// target string -//} -// - -// -//func Test_Disabled(t *testing.T) { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// assert.NoError(t, c.Init(&testCfg{ -// static: `{"enable":true, "dir":"../../tests", "forbid":[]}`, -// })) -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusInactive, st) -//} -// -//func Test_Files_Disable(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":false, "dir":"../../tests", "forbid":[".php"]}`, -// httpCfg: `{ -// "enable": true, -// "address": ":8030", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// -// time.Sleep(time.Second) -// -// b, _, err := get("http://localhost:8030/client.php?hello=world") -// if err != nil { -// t.Fatal(err) -// } -// assert.Equal(t, "WORLD", b) -// c.Stop() -//} -// -//func Test_Files_Error(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.Error(t, c.Init(&testCfg{ -// static: `{"enable":true, "dir":"dir/invalid", "forbid":[".php"]}`, -// httpCfg: `{ -// "enable": true, -// "address": ":8031", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -//} -// -//func Test_Files_Error2(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.Error(t, c.Init(&testCfg{ -// static: `{"enable":true, "dir":"dir/invalid", "forbid":[".php"]`, -// httpCfg: `{ -// "enable": true, -// "address": ":8032", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -//} -// -//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":"../../tests", "forbid":[".php"]}`, -// httpCfg: `{ -// "enable": true, -// "address": ":8033", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// time.Sleep(time.Millisecond * 500) -// -// b, _, err := get("http://localhost:8033/client.php?hello=world") -// if err != nil { -// t.Fatal(err) -// } -// assert.Equal(t, "WORLD", b) -// c.Stop() -//} -// -//func Test_Files_Always(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":"../../tests", "forbid":[".php"], "always":[".ico"]}`, -// httpCfg: `{ -// "enable": true, -// "address": ":8034", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// -// time.Sleep(time.Millisecond * 500) -// -// _, r, err := get("http://localhost:8034/favicon.ico") -// if err != nil { -// t.Fatal(err) -// } -// assert.Equal(t, 404, r.StatusCode) -// c.Stop() -//} -// -//func Test_Files_NotFound(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":"../../tests", "forbid":[".php"]}`, -// httpCfg: `{ -// "enable": true, -// "address": ":8035", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// -// time.Sleep(time.Millisecond * 500) -// -// b, _, _ := get("http://localhost:8035/client.XXX?hello=world") -// assert.Equal(t, "WORLD", b) -// c.Stop() -//} -// -//func Test_Files_Dir(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":"../../tests", "forbid":[".php"]}`, -// httpCfg: `{ -// "enable": true, -// "address": ":8036", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// time.Sleep(time.Millisecond * 500) -// -// b, _, _ := get("http://localhost:8036/http?hello=world") -// assert.Equal(t, "WORLD", b) -// c.Stop() -//} -// -//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":"../../tests", "forbid":[]}`, -// httpCfg: `{ -// "enable": true, -// "address": ":8037", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php pid pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// -// time.Sleep(time.Millisecond * 500) -// -// b, _, _ := get("http://localhost:8037/client.php") -// assert.Equal(t, all("../../tests/client.php"), b) -// assert.Equal(t, all("../../tests/client.php"), b) -// c.Stop() -//} -// -//func TestStatic_Headers(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":"../../tests", "forbid":[], "request":{"input": "custom-header"}, "response":{"output": "output-header"}}`, -// httpCfg: `{ -// "enable": true, -// "address": ":8037", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php pid pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`})) -// -// go func() { -// err := c.Serve() -// if err != nil { -// t.Errorf("serve error: %v", err) -// } -// }() -// -// time.Sleep(time.Millisecond * 500) -// -// req, err := http.NewRequest("GET", "http://localhost:8037/client.php", nil) -// if err != nil { -// t.Fatal(err) -// } -// -// resp, err := http.DefaultClient.Do(req) -// if err != nil { -// t.Fatal(err) -// } -// -// if resp.Header.Get("Output") != "output-header" { -// t.Fatal("can't find output header in response") -// } -// -// -// b, err := ioutil.ReadAll(resp.Body) -// if err != nil { -// t.Fatal(err) -// } -// -// assert.Equal(t, all("../../tests/client.php"), string(b)) -// assert.Equal(t, all("../../tests/client.php"), string(b)) -// c.Stop() -//} -// -//func get(url string) (string, *http.Response, error) { -// r, err := http.Get(url) -// if err != nil { -// return "", nil, err -// } -// -// b, err := ioutil.ReadAll(r.Body) -// if err != nil { -// return "", nil, err -// } -// -// err = r.Body.Close() -// if err != nil { -// return "", nil, err -// } -// -// return string(b), r, err -//} -// -//func tmpDir() string { -// p := os.TempDir() -// j := json.ConfigCompatibleWithStandardLibrary -// r, _ := j.Marshal(p) -// -// return string(r) -//} -// -//func all(fn string) string { -// f, _ := os.Open(fn) -// -// b := &bytes.Buffer{} -// _, err := io.Copy(b, f) -// if err != nil { -// return "" -// } -// -// err = f.Close() -// if err != nil { -// return "" -// } -// -// return b.String() -//} -- cgit v1.2.3 From d6e7b554ab56ee1f423ce915cc9dbb132457d689 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 30 Nov 2020 14:13:31 +0300 Subject: Remove unused imports --- plugins/static/tests/static_plugin_test.go | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'plugins/static/tests') diff --git a/plugins/static/tests/static_plugin_test.go b/plugins/static/tests/static_plugin_test.go index 5717d1e8..a9cb1fd1 100644 --- a/plugins/static/tests/static_plugin_test.go +++ b/plugins/static/tests/static_plugin_test.go @@ -13,7 +13,6 @@ import ( "time" "github.com/golang/mock/gomock" - j "github.com/json-iterator/go" "github.com/spiral/endure" "github.com/spiral/roadrunner/v2/mocks" "github.com/spiral/roadrunner/v2/plugins/config" @@ -25,8 +24,6 @@ import ( "github.com/stretchr/testify/assert" ) -var json = j.ConfigCompatibleWithStandardLibrary - func TestStaticPlugin(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel), endure.Visualize(endure.StdOut, "")) assert.NoError(t, err) @@ -400,14 +397,6 @@ func get(url string) (string, *http.Response, error) { return string(b), r, err } -func tmpDir() string { - p := os.TempDir() - - r, _ := j.Marshal(p) - - return string(r) -} - func all(fn string) string { f, _ := os.Open(fn) -- cgit v1.2.3