summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--service/http/service_test.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/service/http/service_test.go b/service/http/service_test.go
index ddb6b603..49b70bd3 100644
--- a/service/http/service_test.go
+++ b/service/http/service_test.go
@@ -151,6 +151,80 @@ func Test_Service_Echo(t *testing.T) {
assert.Equal(t, "WORLD", string(b))
}
+func Test_Service_Middleware(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(Name, &Service{})
+
+ assert.NoError(t, c.Init(&testCfg{`{
+ "enable": true,
+ "address": ":6029",
+ "maxRequest": 1024,
+ "uploads": {
+ "dir": ` + tmpDir() + `,
+ "forbid": []
+ },
+ "workers":{
+ "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "relay": "pipes",
+ "pool": {
+ "numWorkers": 1,
+ "allocateTimeout": 10000000,
+ "destroyTimeout": 10000000
+ }
+ }
+ }`}))
+
+ s, st := c.Get(Name)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusConfigured, st)
+
+ s.(*Service).AddMiddleware(func(w http.ResponseWriter, r *http.Request) bool {
+ if r.URL.Path == "/halt" {
+ w.WriteHeader(500)
+ w.Write([]byte("halted"))
+ return true
+ }
+
+ return false
+ })
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ req, err := http.NewRequest("GET", "http://localhost:6029?hello=world", nil)
+ assert.NoError(t, err)
+
+ r, err := http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+ defer r.Body.Close()
+
+ b, err := ioutil.ReadAll(r.Body)
+ assert.NoError(t, err)
+
+ assert.NoError(t, err)
+ assert.Equal(t, 201, r.StatusCode)
+ assert.Equal(t, "WORLD", string(b))
+
+
+ req, err = http.NewRequest("GET", "http://localhost:6029/halt", nil)
+ assert.NoError(t, err)
+
+ r, err = http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+ defer r.Body.Close()
+
+ b, err = ioutil.ReadAll(r.Body)
+ assert.NoError(t, err)
+
+ assert.NoError(t, err)
+ assert.Equal(t, 500, r.StatusCode)
+ assert.Equal(t, "halted", string(b))
+}
+
func tmpDir() string {
p := os.TempDir()
r, _ := json.Marshal(p)