summaryrefslogtreecommitdiff
path: root/tests/plugins
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-04-26 21:04:37 +0300
committerValery Piashchynski <[email protected]>2021-04-26 21:04:37 +0300
commit9c07d12a0cc137de0dc79eb94057470985ee5e6c (patch)
tree049713ae78ccac2328675457b1e2427db4403ea3 /tests/plugins
parentf6359114607f9daa41aa90d452ebdc970615c3ab (diff)
- Totally rework static plugin
- Remove old one, now it is part of the HTTP plugin Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'tests/plugins')
-rw-r--r--tests/plugins/http/configs/.rr-http-static-disabled.yaml (renamed from tests/plugins/static/configs/.rr-http-static-disabled.yaml)6
-rw-r--r--tests/plugins/http/configs/.rr-http-static-files-disable.yaml (renamed from tests/plugins/static/configs/.rr-http-static-files-disable.yaml)10
-rw-r--r--tests/plugins/http/configs/.rr-http-static-files.yaml (renamed from tests/plugins/static/configs/.rr-http-static-files.yaml)7
-rw-r--r--tests/plugins/http/configs/.rr-http-static.yaml (renamed from tests/plugins/static/configs/.rr-http-static.yaml)8
-rw-r--r--tests/plugins/http/http_plugin_test.go374
-rw-r--r--tests/plugins/static/static_plugin_test.go387
6 files changed, 370 insertions, 422 deletions
diff --git a/tests/plugins/static/configs/.rr-http-static-disabled.yaml b/tests/plugins/http/configs/.rr-http-static-disabled.yaml
index a85bc408..d248ce48 100644
--- a/tests/plugins/static/configs/.rr-http-static-disabled.yaml
+++ b/tests/plugins/http/configs/.rr-http-static-disabled.yaml
@@ -17,10 +17,6 @@ http:
static:
dir: "abc" #not exists
forbid: [ ".php", ".htaccess" ]
- request:
- Example-Request-Header: "Value"
- response:
- X-Powered-By: "RoadRunner"
pool:
num_workers: 2
max_jobs: 0
@@ -28,4 +24,4 @@ http:
destroy_timeout: 60s
logs:
mode: development
- level: error \ No newline at end of file
+ level: error
diff --git a/tests/plugins/static/configs/.rr-http-static-files-disable.yaml b/tests/plugins/http/configs/.rr-http-static-files-disable.yaml
index 6ba47c91..9f91d75b 100644
--- a/tests/plugins/static/configs/.rr-http-static-files-disable.yaml
+++ b/tests/plugins/http/configs/.rr-http-static-files-disable.yaml
@@ -14,14 +14,6 @@ http:
trusted_subnets: [ "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:
num_workers: 2
max_jobs: 0
@@ -29,4 +21,4 @@ http:
destroy_timeout: 60s
logs:
mode: development
- level: error \ No newline at end of file
+ level: error
diff --git a/tests/plugins/static/configs/.rr-http-static-files.yaml b/tests/plugins/http/configs/.rr-http-static-files.yaml
index 0e003dae..5d8b50e8 100644
--- a/tests/plugins/static/configs/.rr-http-static-files.yaml
+++ b/tests/plugins/http/configs/.rr-http-static-files.yaml
@@ -10,14 +10,15 @@ server:
http:
address: 127.0.0.1:34653
max_request_size: 1024
- middleware: [ "gzip", "static" ]
+ middleware: [ "gzip" ]
trusted_subnets: [ "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"
+ dir: "../../../"
+ pattern: "/tests/"
+ allow: [ ".ico" ]
forbid: [ ".php", ".htaccess" ]
- always: [ ".ico" ]
pool:
num_workers: 2
diff --git a/tests/plugins/static/configs/.rr-http-static.yaml b/tests/plugins/http/configs/.rr-http-static.yaml
index e5af9043..9bb2abc0 100644
--- a/tests/plugins/static/configs/.rr-http-static.yaml
+++ b/tests/plugins/http/configs/.rr-http-static.yaml
@@ -10,13 +10,15 @@ server:
http:
address: 127.0.0.1:21603
max_request_size: 1024
- middleware: [ "gzip", "static" ]
+ middleware: [ "gzip" ]
trusted_subnets: [ "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"
+ dir: "../../../"
+ pattern: "/tests/"
forbid: [ "" ]
+ allow: [ ".txt", ".php" ]
request:
"input": "custom-header"
response:
@@ -28,4 +30,4 @@ http:
destroy_timeout: 60s
logs:
mode: development
- level: error \ No newline at end of file
+ level: error
diff --git a/tests/plugins/http/http_plugin_test.go b/tests/plugins/http/http_plugin_test.go
index 0e43dac4..8bfaa2e8 100644
--- a/tests/plugins/http/http_plugin_test.go
+++ b/tests/plugins/http/http_plugin_test.go
@@ -5,6 +5,7 @@ import (
"crypto/rand"
"crypto/tls"
"fmt"
+ "io"
"io/ioutil"
"net"
"net/http"
@@ -23,6 +24,7 @@ import (
"github.com/spiral/roadrunner/v2/pkg/events"
"github.com/spiral/roadrunner/v2/pkg/process"
"github.com/spiral/roadrunner/v2/plugins/config"
+ "github.com/spiral/roadrunner/v2/plugins/gzip"
"github.com/spiral/roadrunner/v2/plugins/informer"
"github.com/spiral/roadrunner/v2/plugins/logger"
"github.com/spiral/roadrunner/v2/plugins/resetter"
@@ -1397,21 +1399,6 @@ func informerTestAfter(t *testing.T) {
assert.NotEqual(t, workerPid, list.Workers[0].Pid)
}
-func get(url string) (string, *http.Response, error) {
- r, err := http.Get(url) //nolint:gosec
- if err != nil {
- return "", nil, err
- }
- b, err := ioutil.ReadAll(r.Body)
- if err != nil {
- return "", nil, err
- }
- defer func() {
- _ = r.Body.Close()
- }()
- return string(b), r, err
-}
-
// get request and return body
func getHeader(url string, h map[string]string) (string, *http.Response, error) {
req, err := http.NewRequest("GET", url, bytes.NewBuffer(nil))
@@ -1574,3 +1561,360 @@ func bigEchoHTTP(t *testing.T) {
err = r.Body.Close()
assert.NoError(t, err)
}
+
+func TestStaticPlugin(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
+ 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.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)
+
+ stopCh := make(chan struct{}, 1)
+
+ go func() {
+ 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 <-stopCh:
+ // timeout
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ }
+ }
+ }()
+
+ time.Sleep(time.Second)
+ t.Run("ServeSample", serveStaticSample)
+ t.Run("StaticNotForbid", staticNotForbid)
+ t.Run("StaticHeaders", staticHeaders)
+
+ stopCh <- struct{}{}
+ wg.Wait()
+}
+
+func staticHeaders(t *testing.T) {
+ req, err := http.NewRequest("GET", "http://localhost:21603/tests/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)
+ }
+
+ defer func() {
+ _ = resp.Body.Close()
+ }()
+
+ 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/tests/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/tests/sample.txt")
+ assert.NoError(t, err)
+ assert.Equal(t, "sample", b)
+ _ = r.Body.Close()
+}
+
+func TestStaticDisabled_Error(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
+ 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.Plugin{},
+ )
+ assert.NoError(t, err)
+ assert.Error(t, cont.Init())
+}
+
+func TestStaticFilesDisabled(t *testing.T) {
+ cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
+ 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.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)
+
+ stopCh := make(chan struct{}, 1)
+
+ go func() {
+ 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 <-stopCh:
+ // timeout
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ }
+ }
+ }()
+
+ time.Sleep(time.Second)
+ t.Run("StaticFilesDisabled", staticFilesDisabled)
+
+ stopCh <- struct{}{}
+ 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.ErrorLevel))
+ 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("worker destructed", "pid", gomock.Any()).AnyTimes()
+ mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).AnyTimes()
+ mockLogger.EXPECT().Debug("201 GET http://localhost:34653/tests/http?hello=world", "remote", "127.0.0.1", "elapsed", gomock.Any()).MinTimes(1)
+ mockLogger.EXPECT().Debug("201 GET http://localhost:34653/tests/client.XXX?hello=world", "remote", "127.0.0.1", "elapsed", gomock.Any()).MinTimes(1)
+ mockLogger.EXPECT().Debug("201 GET http://localhost:34653/tests/client.php?hello=world", "remote", "127.0.0.1", "elapsed", gomock.Any()).MinTimes(1)
+ mockLogger.EXPECT().Error("file open error", "error", gomock.Any()).AnyTimes()
+ mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() // placeholder for the workerlogerror
+
+ err = cont.RegisterAll(
+ cfg,
+ mockLogger,
+ &server.Plugin{},
+ &httpPlugin.Plugin{},
+ &gzip.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)
+
+ stopCh := make(chan struct{}, 1)
+
+ go func() {
+ 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 <-stopCh:
+ // timeout
+ err = cont.Stop()
+ if err != nil {
+ assert.FailNow(t, "error", err.Error())
+ }
+ return
+ }
+ }
+ }()
+
+ time.Sleep(time.Second)
+ t.Run("StaticTestFilesDir", staticTestFilesDir)
+ t.Run("StaticNotFound", staticNotFound)
+ t.Run("StaticFilesForbid", staticFilesForbid)
+ t.Run("StaticFilesAlways", staticFilesAlways)
+
+ stopCh <- struct{}{}
+ wg.Wait()
+}
+
+func staticTestFilesDir(t *testing.T) {
+ b, r, err := get("http://localhost:34653/tests/http?hello=world")
+ assert.NoError(t, err)
+ assert.Equal(t, "403 Forbidden\n", b)
+ _ = r.Body.Close()
+}
+
+func staticNotFound(t *testing.T) {
+ b, _, _ := get("http://localhost:34653/tests/client.XXX?hello=world") //nolint:bodyclose
+ assert.Equal(t, "404 page not found\n", b)
+}
+
+func staticFilesAlways(t *testing.T) {
+ _, r, err := get("http://localhost:34653/tests/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/tests/client.php?hello=world")
+ if err != nil {
+ t.Fatal(err)
+ }
+ assert.Equal(t, "403 Forbidden\n", b)
+ _ = r.Body.Close()
+}
+
+// HELPERS
+func get(url string) (string, *http.Response, error) {
+ r, err := http.Get(url) //nolint:gosec
+ 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 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/tests/plugins/static/static_plugin_test.go b/tests/plugins/static/static_plugin_test.go
deleted file mode 100644
index b58f1f6b..00000000
--- a/tests/plugins/static/static_plugin_test.go
+++ /dev/null
@@ -1,387 +0,0 @@
-package static
-
-import (
- "bytes"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "os/signal"
- "sync"
- "syscall"
- "testing"
- "time"
-
- "github.com/golang/mock/gomock"
- endure "github.com/spiral/endure/pkg/container"
- "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/spiral/roadrunner/v2/tests/mocks"
- "github.com/stretchr/testify/assert"
-)
-
-func TestStaticPlugin(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
- 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.Plugin{},
- &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)
-
- stopCh := make(chan struct{}, 1)
-
- go func() {
- 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 <-stopCh:
- // timeout
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- }
- }
- }()
-
- time.Sleep(time.Second)
- t.Run("ServeSample", serveStaticSample)
- t.Run("StaticNotForbid", staticNotForbid)
- t.Run("StaticHeaders", staticHeaders)
-
- stopCh <- struct{}{}
- 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)
- }
-
- defer func() {
- _ = resp.Body.Close()
- }()
-
- 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)
- assert.Equal(t, "sample", b)
- _ = r.Body.Close()
-}
-
-func TestStaticDisabled_Error(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
- 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.Plugin{},
- &static.Plugin{},
- )
- assert.NoError(t, err)
- assert.Error(t, cont.Init())
-}
-
-func TestStaticFilesDisabled(t *testing.T) {
- cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
- 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.Plugin{},
- &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)
-
- stopCh := make(chan struct{}, 1)
-
- go func() {
- 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 <-stopCh:
- // timeout
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- }
- }
- }()
-
- time.Sleep(time.Second)
- t.Run("StaticFilesDisabled", staticFilesDisabled)
-
- stopCh <- struct{}{}
- 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.ErrorLevel))
- 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("worker destructed", "pid", gomock.Any()).AnyTimes()
- mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).AnyTimes()
- mockLogger.EXPECT().Debug("201 GET http://localhost:34653/http?hello=world", "remote", "127.0.0.1", "elapsed", gomock.Any()).MinTimes(1)
- mockLogger.EXPECT().Debug("201 GET http://localhost:34653/client.XXX?hello=world", "remote", "127.0.0.1", "elapsed", gomock.Any()).MinTimes(1)
- mockLogger.EXPECT().Debug("201 GET http://localhost:34653/client.php?hello=world", "remote", "127.0.0.1", "elapsed", gomock.Any()).MinTimes(1)
- mockLogger.EXPECT().Error("file open error", "error", gomock.Any()).AnyTimes()
- mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() // placeholder for the workerlogerror
-
- err = cont.RegisterAll(
- cfg,
- //mockLogger,
- &logger.ZapLogger{},
- &server.Plugin{},
- &httpPlugin.Plugin{},
- &gzip.Plugin{},
- &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)
-
- stopCh := make(chan struct{}, 1)
-
- go func() {
- 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 <-stopCh:
- // timeout
- err = cont.Stop()
- if err != nil {
- assert.FailNow(t, "error", err.Error())
- }
- return
- }
- }
- }()
-
- time.Sleep(time.Second)
- t.Run("StaticTestFilesDir", staticTestFilesDir)
- t.Run("StaticNotFound", staticNotFound)
- t.Run("StaticFilesForbid", staticFilesForbid)
- t.Run("StaticFilesAlways", staticFilesAlways)
-
- stopCh <- struct{}{}
- 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") //nolint:bodyclose
- 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) //nolint:gosec
- 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 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()
-}