package tests import ( "io/ioutil" "net/http" "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.ErrorLevel)) 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() } func TestRequestHeaders(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) assert.NoError(t, err) cfg := &config.Viper{ Path: "configs/.rr-req-headers.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 * 10) 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 } } }() time.Sleep(time.Second) t.Run("RequestHeaders", reqHeaders) wg.Wait() } func reqHeaders(t *testing.T) { req, err := http.NewRequest("GET", "http://localhost:22655?hello=value", nil) assert.NoError(t, err) r, err := http.DefaultClient.Do(req) assert.NoError(t, err) b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) assert.Equal(t, 200, r.StatusCode) assert.Equal(t, "CUSTOM-HEADER", string(b)) err = r.Body.Close() assert.NoError(t, err) } func TestResponseHeaders(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) assert.NoError(t, err) cfg := &config.Viper{ Path: "configs/.rr-res-headers.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 * 10) 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 } } }() time.Sleep(time.Second) t.Run("ResponseHeaders", resHeaders) wg.Wait() } func resHeaders(t *testing.T) { req, err := http.NewRequest("GET", "http://localhost:22455?hello=value", nil) assert.NoError(t, err) r, err := http.DefaultClient.Do(req) assert.NoError(t, err) assert.Equal(t, "output-header", r.Header.Get("output")) b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) assert.Equal(t, 200, r.StatusCode) assert.Equal(t, "CUSTOM-HEADER", string(b)) err = r.Body.Close() assert.NoError(t, err) } func TestCORSHeaders(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) assert.NoError(t, err) cfg := &config.Viper{ Path: "configs/.rr-cors-headers.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 * 10) 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 } } }() time.Sleep(time.Second) t.Run("CORSHeaders", corsHeaders) t.Run("CORSHeadersPass", corsHeadersPass) wg.Wait() } func corsHeadersPass(t *testing.T) { req, err := http.NewRequest("GET", "http://localhost:22855", nil) assert.NoError(t, err) r, err := http.DefaultClient.Do(req) assert.NoError(t, err) assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Headers")) assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) _, err = ioutil.ReadAll(r.Body) assert.NoError(t, err) assert.Equal(t, 200, r.StatusCode) err = r.Body.Close() assert.NoError(t, err) } func corsHeaders(t *testing.T) { req, err := http.NewRequest("OPTIONS", "http://localhost:22855", nil) assert.NoError(t, err) r, err := http.DefaultClient.Do(req) assert.NoError(t, err) assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Headers")) assert.Equal(t, "GET,POST,PUT,DELETE", r.Header.Get("Access-Control-Allow-Methods")) assert.Equal(t, "*", r.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "600", r.Header.Get("Access-Control-Max-Age")) assert.Equal(t, "true", r.Header.Get("Access-Control-Allow-Credentials")) _, err = ioutil.ReadAll(r.Body) assert.NoError(t, err) assert.Equal(t, 200, r.StatusCode) err = r.Body.Close() assert.NoError(t, err) }