diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/headers/plugin.go | 10 | ||||
-rw-r--r-- | plugins/headers/tests/configs/.rr-headers-init.yaml | 41 | ||||
-rw-r--r-- | plugins/headers/tests/headers_plugin_test.go | 81 |
3 files changed, 127 insertions, 5 deletions
diff --git a/plugins/headers/plugin.go b/plugins/headers/plugin.go index 629c0f30..dc0aab5a 100644 --- a/plugins/headers/plugin.go +++ b/plugins/headers/plugin.go @@ -22,7 +22,7 @@ type Plugin struct { // misconfiguration. Services must not be used without proper configuration pushed first. func (s *Plugin) Init(cfg config.Configurer) error { const op = errors.Op("headers plugin init") - err := cfg.UnmarshalKey(RootPluginName, s.cfg) + err := cfg.UnmarshalKey(RootPluginName, &s.cfg) if err != nil { return errors.E(op, err) } @@ -48,11 +48,11 @@ func (s *Plugin) Middleware(next http.Handler) http.HandlerFunc { if s.cfg.Headers.CORS != nil { if r.Method == http.MethodOptions { - s.preflightRequest(w, r) + s.preflightRequest(w) return } - s.corsHeaders(w, r) + s.corsHeaders(w) } next.ServeHTTP(w, r) @@ -64,7 +64,7 @@ func (s *Plugin) Name() string { } // configure OPTIONS response -func (s *Plugin) preflightRequest(w http.ResponseWriter, r *http.Request) { +func (s *Plugin) preflightRequest(w http.ResponseWriter) { headers := w.Header() headers.Add("Vary", "Origin") @@ -95,7 +95,7 @@ func (s *Plugin) preflightRequest(w http.ResponseWriter, r *http.Request) { } // configure CORS headers -func (s *Plugin) corsHeaders(w http.ResponseWriter, r *http.Request) { +func (s *Plugin) corsHeaders(w http.ResponseWriter) { headers := w.Header() headers.Add("Vary", "Origin") diff --git a/plugins/headers/tests/configs/.rr-headers-init.yaml b/plugins/headers/tests/configs/.rr-headers-init.yaml new file mode 100644 index 00000000..cd5d4f9c --- /dev/null +++ b/plugins/headers/tests/configs/.rr-headers-init.yaml @@ -0,0 +1,41 @@ +rpc: + listen: tcp://127.0.0.1:6001 + disabled: false + +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:18903 + maxRequestSize: 1024 + middleware: [ "headers" ] + uploads: + forbid: [ ".php", ".exe", ".bat" ] + 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" ] + # Additional HTTP headers and CORS control. + headers: + cors: + allowedOrigin: "*" + allowedHeaders: "*" + allowedMethods: "GET,POST,PUT,DELETE" + allowCredentials: true + exposedHeaders: "Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma" + maxAge: 600 + request: + "Example-Request-Header": "Value" + response: + "X-Powered-By": "RoadRunner" + pool: + numWorkers: 2 + maxJobs: 0 + allocateTimeout: 60s + destroyTimeout: 60s + + diff --git a/plugins/headers/tests/headers_plugin_test.go b/plugins/headers/tests/headers_plugin_test.go index ca8701d2..a0feed81 100644 --- a/plugins/headers/tests/headers_plugin_test.go +++ b/plugins/headers/tests/headers_plugin_test.go @@ -1 +1,82 @@ package tests + +import ( + "os" + "os/signal" + "sync" + "syscall" + "testing" + "time" + + "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/plugins/config" + "github.com/spiral/roadrunner/v2/plugins/headers" + httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" + "github.com/spiral/roadrunner/v2/plugins/logger" + "github.com/spiral/roadrunner/v2/plugins/server" + "github.com/stretchr/testify/assert" +) + +func TestHeadersInit(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel)) + assert.NoError(t, err) + + cfg := &config.Viper{ + Path: "configs/.rr-headers-init.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &headers.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) + + tt := time.NewTimer(time.Second * 5) + + 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 <-tt.C: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + wg.Wait() +} |