diff options
Diffstat (limited to 'plugins/http/tests')
-rw-r--r-- | plugins/http/tests/configs/.rr-echoErr.yaml | 28 | ||||
-rw-r--r-- | plugins/http/tests/configs/.rr-http.yaml | 2 | ||||
-rw-r--r-- | plugins/http/tests/http_test.go | 192 | ||||
-rw-r--r-- | plugins/http/tests/plugin_middleware.go | 61 | ||||
-rw-r--r-- | plugins/http/tests/plugin_test_old.go | 122 |
5 files changed, 281 insertions, 124 deletions
diff --git a/plugins/http/tests/configs/.rr-echoErr.yaml b/plugins/http/tests/configs/.rr-echoErr.yaml new file mode 100644 index 00000000..5a97723d --- /dev/null +++ b/plugins/http/tests/configs/.rr-echoErr.yaml @@ -0,0 +1,28 @@ +rpc: + listen: tcp://127.0.0.1:6001 + disabled: false + +server: + command: "php ../../../tests/http/client.php echoerr pipes" + user: "" + group: "" + env: + "RR_HTTP": "true" + relay: "pipes" + relayTimeout: "20s" + +http: + debug: true + address: 127.0.0.1:8080 + maxRequestSize: 1024 + middleware: [ "" ] + uploads: + forbid: [ "" ] + 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" ] + pool: + numWorkers: 1 + maxJobs: 0 + allocateTimeout: 60s + destroyTimeout: 60s + + diff --git a/plugins/http/tests/configs/.rr-http.yaml b/plugins/http/tests/configs/.rr-http.yaml index c989df24..efb3d4fb 100644 --- a/plugins/http/tests/configs/.rr-http.yaml +++ b/plugins/http/tests/configs/.rr-http.yaml @@ -3,7 +3,7 @@ rpc: disabled: false server: - command: "php psr-worker.php" + command: "php ../../../tests/http/client.php echo pipes" user: "" group: "" env: diff --git a/plugins/http/tests/http_test.go b/plugins/http/tests/http_test.go index 9ea474a3..bd329758 100644 --- a/plugins/http/tests/http_test.go +++ b/plugins/http/tests/http_test.go @@ -15,9 +15,11 @@ import ( "testing" "time" + "github.com/golang/mock/gomock" "github.com/spiral/endure" "github.com/spiral/goridge/v2" "github.com/spiral/roadrunner/v2" + "github.com/spiral/roadrunner/v2/mocks" "github.com/spiral/roadrunner/v2/plugins/config" httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" "github.com/spiral/roadrunner/v2/plugins/informer" @@ -178,8 +180,8 @@ func echoHTTP(t *testing.T) { assert.NoError(t, err) b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) - assert.Equal(t, 200, r.StatusCode) - assert.Equal(t, "hello world", string(b)) + assert.Equal(t, 201, r.StatusCode) + assert.Equal(t, "WORLD", string(b)) err = r.Body.Close() assert.NoError(t, err) @@ -583,6 +585,7 @@ func TestFastCGI_RequestUri(t *testing.T) { } func fcgiReqURI(t *testing.T) { + time.Sleep(time.Second * 2) fcgiConnFactory := gofast.SimpleConnFactory("tcp", "127.0.0.1:6921") fcgiHandler := gofast.NewHandler( @@ -773,6 +776,191 @@ func h2c(t *testing.T) { } } +func TestHttpMiddleware(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.yaml", + Prefix: "rr", + } + + err = cont.RegisterAll( + cfg, + &rpcPlugin.Plugin{}, + &logger.ZapLogger{}, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &PluginMiddleware{}, + &PluginMiddleware2{}, + ) + 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("MiddlewareTest", middleware) + wg.Wait() +} + +func middleware(t *testing.T) { + req, err := http.NewRequest("GET", "http://localhost:8084?hello=world", 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, 201, r.StatusCode) + assert.Equal(t, "WORLD", string(b)) + + err = r.Body.Close() + assert.NoError(t, err) + + req, err = http.NewRequest("GET", "http://localhost:8084/halt", 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, 500, r.StatusCode) + assert.Equal(t, "halted", string(b)) + + err = r.Body.Close() + assert.NoError(t, err) +} + +func TestHttpEchoErr(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-echoErr.yaml", + Prefix: "rr", + } + + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Info("response received", "elapsed", gomock.Any(), "remote address", "127.0.0.1") + mockLogger.EXPECT().Debug("WORLD", "pid", gomock.Any()) + mockLogger.EXPECT().Info("worker event received", "event", roadrunner.EventWorkerLog, "worker state", gomock.Any()) + + err = cont.RegisterAll( + cfg, + mockLogger, + &server.Plugin{}, + &httpPlugin.Plugin{}, + &PluginMiddleware{}, + &PluginMiddleware2{}, + ) + 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 * 5) + 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("HttpEchoError", echoError) + wg.Wait() +} + +func echoError(t *testing.T) { + req, err := http.NewRequest("GET", "http://localhost:8080?hello=world", 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, 201, r.StatusCode) + assert.Equal(t, "WORLD", string(b)) + err = r.Body.Close() + assert.NoError(t, err) +} + func get(url string) (string, *http.Response, error) { r, err := http.Get(url) if err != nil { diff --git a/plugins/http/tests/plugin_middleware.go b/plugins/http/tests/plugin_middleware.go new file mode 100644 index 00000000..de829d34 --- /dev/null +++ b/plugins/http/tests/plugin_middleware.go @@ -0,0 +1,61 @@ +package tests + +import ( + "net/http" + + "github.com/spiral/roadrunner/v2/plugins/config" +) + +type PluginMiddleware struct { + config config.Configurer +} + +func (p *PluginMiddleware) Init(cfg config.Configurer) error { + p.config = cfg + return nil +} + +func (p *PluginMiddleware) Middleware(next http.Handler) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/halt" { + w.WriteHeader(500) + _, err := w.Write([]byte("halted")) + if err != nil { + panic("error writing the data to the http reply") + } + } else { + next.ServeHTTP(w, r) + } + } +} + +func (p *PluginMiddleware) Name() string { + return "pluginMiddleware" +} + +type PluginMiddleware2 struct { + config config.Configurer +} + +func (p *PluginMiddleware2) Init(cfg config.Configurer) error { + p.config = cfg + return nil +} + +func (p *PluginMiddleware2) Middleware(next http.Handler) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/boom" { + w.WriteHeader(555) + _, err := w.Write([]byte("boom")) + if err != nil { + panic("error writing the data to the http reply") + } + } else { + next.ServeHTTP(w, r) + } + } +} + +func (p *PluginMiddleware2) Name() string { + return "pluginMiddleware2" +} diff --git a/plugins/http/tests/plugin_test_old.go b/plugins/http/tests/plugin_test_old.go index 1ef7002e..dae18bb4 100644 --- a/plugins/http/tests/plugin_test_old.go +++ b/plugins/http/tests/plugin_test_old.go @@ -348,127 +348,7 @@ package tests // } //} // -//func Test_Service_Middleware(t *testing.T) { -// bkoff := backoff.NewExponentialBackOff() -// bkoff.MaxElapsedTime = time.Second * 15 -// -// err := backoff.Retry(func() error { -// logger, _ := test.NewNullLogger() -// logger.SetLevel(logrus.DebugLevel) -// -// c := service.NewContainer(logger) -// c.Register(ID, &Service{}) -// -// err := c.Init(&testCfg{httpCfg: `{ -// "enable": true, -// "address": ":6032", -// "maxRequestSize": 1024, -// "uploads": { -// "dir": ` + tmpDir() + `, -// "forbid": [] -// }, -// "workers":{ -// "command": "php ../../tests/http/client.php echo pipes", -// "relay": "pipes", -// "pool": { -// "numWorkers": 1, -// "allocateTimeout": 10000000, -// "destroyTimeout": 10000000 -// } -// } -// }`}) -// if err != nil { -// return err -// } -// -// s, st := c.Get(ID) -// assert.NotNil(t, s) -// assert.Equal(t, service.StatusOK, st) -// -// s.(*Service).AddMiddleware(func(f http.HandlerFunc) http.HandlerFunc { -// return func(w http.ResponseWriter, r *http.Request) { -// if r.URL.Path == "/halt" { -// w.WriteHeader(500) -// _, err := w.Write([]byte("halted")) -// if err != nil { -// t.Errorf("error writing the data to the http reply: error %v", err) -// } -// } else { -// f(w, r) -// } -// } -// }) -// -// 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:6032?hello=world", nil) -// if err != nil { -// c.Stop() -// return err -// } -// -// r, err := http.DefaultClient.Do(req) -// if err != nil { -// c.Stop() -// return err -// } -// -// b, err := ioutil.ReadAll(r.Body) -// if err != nil { -// c.Stop() -// return err -// } -// -// assert.Equal(t, 201, r.StatusCode) -// assert.Equal(t, "WORLD", string(b)) -// -// err = r.Body.Close() -// if err != nil { -// c.Stop() -// return err -// } -// -// req, err = http.NewRequest("GET", "http://localhost:6032/halt", nil) -// if err != nil { -// c.Stop() -// return err -// } -// -// r, err = http.DefaultClient.Do(req) -// if err != nil { -// c.Stop() -// return err -// } -// b, err = ioutil.ReadAll(r.Body) -// if err != nil { -// c.Stop() -// return err -// } -// -// assert.Equal(t, 500, r.StatusCode) -// assert.Equal(t, "halted", string(b)) -// -// err = r.Body.Close() -// if err != nil { -// c.Stop() -// return err -// } -// c.Stop() -// -// return nil -// }, bkoff) -// -// if err != nil { -// t.Fatal(err) -// } -// -//} + // //func Test_Service_Listener(t *testing.T) { // bkoff := backoff.NewExponentialBackOff() |