summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-06-24 14:54:18 +0300
committerWolfy-J <[email protected]>2019-06-24 14:54:18 +0300
commit7c578ccb1d26b0fd0a4f3998b03966b0dfdc6b82 (patch)
treea356286075b5c57ca44142487c0277cfec126beb
parentee2396c406aa4cc455248fc3febc026eb38efe24 (diff)
more tests
-rw-r--r--service/headers/service_test.go118
-rw-r--r--tests/http/headers.php11
2 files changed, 126 insertions, 3 deletions
diff --git a/service/headers/service_test.go b/service/headers/service_test.go
index a7b984bf..250f4458 100644
--- a/service/headers/service_test.go
+++ b/service/headers/service_test.go
@@ -114,13 +114,125 @@ func Test_ResponseHeaders(t *testing.T) {
assert.NoError(t, err)
defer r.Body.Close()
- v := r.Header.Get("output")
- assert.Equal(t, "output-header", v)
+ assert.Equal(t, "output-header", r.Header.Get("output"))
b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
- assert.NoError(t, err)
assert.Equal(t, 200, r.StatusCode)
assert.Equal(t, "CUSTOM-HEADER", string(b))
}
+
+func TestCORS_OPTIONS(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(rrhttp.ID, &rrhttp.Service{})
+ c.Register(ID, &Service{})
+
+ assert.NoError(t, c.Init(&testCfg{
+ headers: `{
+"cors":{
+ "allowedOrigin": "*",
+ "allowedHeaders": "*",
+ "allowedMethods": "GET,POST,PUT,DELETE",
+ "allowCredentials": true,
+ "exposedHeaders": "Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma",
+ "maxAge": 600
+}
+}`,
+ httpCfg: `{
+ "enable": true,
+ "address": ":6029",
+ "maxRequestSize": 1024,
+ "workers":{
+ "command": "php ../../tests/http/client.php headers pipes",
+ "relay": "pipes",
+ "pool": {
+ "numWorkers": 1,
+ "allocateTimeout": 10000000,
+ "destroyTimeout": 10000000
+ }
+ }
+ }`}))
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ req, err := http.NewRequest("OPTIONS", "http://localhost:6029", nil)
+ assert.NoError(t, err)
+
+ r, err := http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+ defer r.Body.Close()
+
+ 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)
+}
+
+func TestCORS_Pass(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(rrhttp.ID, &rrhttp.Service{})
+ c.Register(ID, &Service{})
+
+ assert.NoError(t, c.Init(&testCfg{
+ headers: `{
+"cors":{
+ "allowedOrigin": "*",
+ "allowedHeaders": "*",
+ "allowedMethods": "GET,POST,PUT,DELETE",
+ "allowCredentials": true,
+ "exposedHeaders": "Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma",
+ "maxAge": 600
+}
+}`,
+ httpCfg: `{
+ "enable": true,
+ "address": ":6029",
+ "maxRequestSize": 1024,
+ "workers":{
+ "command": "php ../../tests/http/client.php headers pipes",
+ "relay": "pipes",
+ "pool": {
+ "numWorkers": 1,
+ "allocateTimeout": 10000000,
+ "destroyTimeout": 10000000
+ }
+ }
+ }`}))
+
+ go func() { c.Serve() }()
+ time.Sleep(time.Millisecond * 100)
+ defer c.Stop()
+
+ req, err := http.NewRequest("GET", "http://localhost:6029", nil)
+ assert.NoError(t, err)
+
+ r, err := http.DefaultClient.Do(req)
+ assert.NoError(t, err)
+ defer r.Body.Close()
+
+ 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)
+}
diff --git a/tests/http/headers.php b/tests/http/headers.php
new file mode 100644
index 00000000..c85d5caf
--- /dev/null
+++ b/tests/http/headers.php
@@ -0,0 +1,11 @@
+<?php
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+
+function handleRequest(ServerRequestInterface $req, ResponseInterface $resp): ResponseInterface
+{
+ $resp->getBody()->write(json_encode($req->getHeaders()));
+
+ return $resp;
+} \ No newline at end of file