summaryrefslogtreecommitdiff
path: root/service/http
diff options
context:
space:
mode:
authorAnton Titov <[email protected]>2020-03-02 11:45:50 +0300
committerGitHub <[email protected]>2020-03-02 11:45:50 +0300
commitb74aedd13ef6aa0bb78bc08bb521caa36e099e9d (patch)
treebb727c3105a082ece946142fde1696ce2e895067 /service/http
parent9960aff462c985f89e1e9387e0b87bab24504d0e (diff)
parent456c9934a898fbbaf8e92e1b6bd815b6a8ffeffb (diff)
Merge pull request #260 from spiral/address_already_in_use_error
Address already in use error
Diffstat (limited to 'service/http')
-rw-r--r--service/http/fcgi_test.go8
-rw-r--r--service/http/h2c_test.go17
-rw-r--r--service/http/rpc_test.go44
-rw-r--r--service/http/service_test.go55
-rw-r--r--service/http/ssl_test.go48
5 files changed, 101 insertions, 71 deletions
diff --git a/service/http/fcgi_test.go b/service/http/fcgi_test.go
index 0cfc6e41..e68b2e7f 100644
--- a/service/http/fcgi_test.go
+++ b/service/http/fcgi_test.go
@@ -37,8 +37,7 @@ func Test_FCGI_Service_Echo(t *testing.T) {
s.(*Service).Stop()
go func() { assert.NoError(t, c.Serve()) }()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
+ time.Sleep(time.Second * 1)
fcgiConnFactory := gofast.SimpleConnFactory("tcp", "0.0.0.0:6082")
@@ -56,6 +55,7 @@ func Test_FCGI_Service_Echo(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 201, w.Result().StatusCode)
assert.Equal(t, "WORLD", string(body))
+ c.Stop()
}
func Test_FCGI_Service_Request_Uri(t *testing.T) {
@@ -83,8 +83,7 @@ func Test_FCGI_Service_Request_Uri(t *testing.T) {
s.(*Service).Stop()
go func() { assert.NoError(t, c.Serve()) }()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
+ time.Sleep(time.Second * 1)
fcgiConnFactory := gofast.SimpleConnFactory("tcp", "0.0.0.0:6083")
@@ -102,4 +101,5 @@ func Test_FCGI_Service_Request_Uri(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 200, w.Result().StatusCode)
assert.Equal(t, "http://site.local/hello-world", string(body))
+ c.Stop()
}
diff --git a/service/http/h2c_test.go b/service/http/h2c_test.go
index 7bbc30ac..a2465a0a 100644
--- a/service/http/h2c_test.go
+++ b/service/http/h2c_test.go
@@ -52,16 +52,15 @@ func Test_Service_H2C(t *testing.T) {
req.Header.Add("Connection", "HTTP2-Settings")
req.Header.Add("HTTP2-Settings", "")
- r, err := http.DefaultClient.Do(req)
- assert.NoError(t, err)
- defer func() {
- err := r.Body.Close()
- if err != nil {
- t.Errorf("fail to close the Body: error %v", err)
- }
- }()
+ r, err2 := http.DefaultClient.Do(req)
+ if err2 != nil {
+ t.Fatal(err2)
+ }
assert.Equal(t, "101 Switching Protocols", r.Status)
- // will fail with h2c notice
+ err3 := r.Body.Close()
+ if err3 != nil {
+ t.Errorf("fail to close the Body: error %v", err3)
+ }
}
diff --git a/service/http/rpc_test.go b/service/http/rpc_test.go
index 0e4b2c0a..1971f237 100644
--- a/service/http/rpc_test.go
+++ b/service/http/rpc_test.go
@@ -55,10 +55,13 @@ func Test_RPC(t *testing.T) {
t.Errorf("error during the Serve: error %v", err)
}
}()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
- res, _, _ := get("http://localhost:6029")
+ time.Sleep(time.Second)
+
+ res, _, err := get("http://localhost:6029")
+ if err != nil {
+ t.Fatal(err)
+ }
assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), res)
cl, err := rs.Client()
@@ -68,9 +71,13 @@ func Test_RPC(t *testing.T) {
assert.NoError(t, cl.Call("http.Reset", true, &r))
assert.Equal(t, "OK", r)
- res2, _, _ := get("http://localhost:6029")
+ res2, _, err := get("http://localhost:6029")
+ if err != nil {
+ t.Fatal()
+ }
assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), res2)
assert.NotEqual(t, res, res2)
+ c.Stop()
}
func Test_RPC_Unix(t *testing.T) {
@@ -121,22 +128,39 @@ func Test_RPC_Unix(t *testing.T) {
t.Errorf("error during the Serve: error %v", err)
}
}()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
- res, _, _ := get("http://localhost:6029")
- assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), res)
+ time.Sleep(time.Second)
+
+ res, _, err := get("http://localhost:6029")
+ if err != nil {
+ c.Stop()
+ t.Fatal(err)
+ }
+ if ss.rr.Workers() != nil && len(ss.rr.Workers()) > 0 {
+ assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), res)
+ } else {
+ c.Stop()
+ t.Fatal("no workers initialized")
+ }
cl, err := rs.Client()
- assert.NoError(t, err)
+ if err != nil {
+ c.Stop()
+ t.Fatal(err)
+ }
r := ""
assert.NoError(t, cl.Call("http.Reset", true, &r))
assert.Equal(t, "OK", r)
- res2, _, _ := get("http://localhost:6029")
+ res2, _, err := get("http://localhost:6029")
+ if err != nil {
+ c.Stop()
+ t.Fatal(err)
+ }
assert.Equal(t, strconv.Itoa(*ss.rr.Workers()[0].Pid), res2)
assert.NotEqual(t, res, res2)
+ c.Stop()
}
func Test_Workers(t *testing.T) {
diff --git a/service/http/service_test.go b/service/http/service_test.go
index c4b2c2c4..1a1c32ae 100644
--- a/service/http/service_test.go
+++ b/service/http/service_test.go
@@ -115,7 +115,7 @@ func Test_Service_Echo(t *testing.T) {
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
"enable": true,
- "address": ":6029",
+ "address": ":6536",
"maxRequestSize": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
@@ -146,26 +146,24 @@ func Test_Service_Echo(t *testing.T) {
}
}()
time.Sleep(time.Millisecond * 100)
- defer c.Stop()
- req, err := http.NewRequest("GET", "http://localhost:6029?hello=world", nil)
+ req, err := http.NewRequest("GET", "http://localhost:6536?hello=world", nil)
assert.NoError(t, err)
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer func() {
- err := r.Body.Close()
- if err != nil {
- t.Errorf("error closing the Body: error %v", err)
- }
- }()
-
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))
+
+ err2 := r.Body.Close()
+ if err2 != nil {
+ t.Errorf("error closing the Body: error %v", err2)
+ }
+ c.Stop()
}
func Test_Service_Env(t *testing.T) {
@@ -178,7 +176,7 @@ func Test_Service_Env(t *testing.T) {
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
"enable": true,
- "address": ":6029",
+ "address": ":6031",
"maxRequestSize": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
@@ -208,10 +206,10 @@ func Test_Service_Env(t *testing.T) {
t.Errorf("serve error: %v", err)
}
}()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
- req, err := http.NewRequest("GET", "http://localhost:6029", nil)
+ time.Sleep(time.Second * 1)
+
+ req, err := http.NewRequest("GET", "http://localhost:6031", nil)
assert.NoError(t, err)
r, err := http.DefaultClient.Do(req)
@@ -229,6 +227,7 @@ func Test_Service_Env(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 200, r.StatusCode)
assert.Equal(t, "ENV_VALUE", string(b))
+ c.Stop()
}
func Test_Service_ErrorEcho(t *testing.T) {
@@ -240,7 +239,7 @@ func Test_Service_ErrorEcho(t *testing.T) {
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
"enable": true,
- "address": ":6029",
+ "address": ":6030",
"maxRequestSize": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
@@ -276,10 +275,10 @@ func Test_Service_ErrorEcho(t *testing.T) {
t.Errorf("serve error: %v", err)
}
}()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
- req, err := http.NewRequest("GET", "http://localhost:6029?hello=world", nil)
+ time.Sleep(time.Second)
+
+ req, err := http.NewRequest("GET", "http://localhost:6030?hello=world", nil)
assert.NoError(t, err)
r, err := http.DefaultClient.Do(req)
@@ -299,6 +298,7 @@ func Test_Service_ErrorEcho(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 201, r.StatusCode)
assert.Equal(t, "WORLD", string(b))
+ c.Stop()
}
func Test_Service_Middleware(t *testing.T) {
@@ -310,7 +310,7 @@ func Test_Service_Middleware(t *testing.T) {
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
"enable": true,
- "address": ":6029",
+ "address": ":6032",
"maxRequestSize": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
@@ -351,10 +351,9 @@ func Test_Service_Middleware(t *testing.T) {
t.Errorf("serve error: %v", err)
}
}()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
+ time.Sleep(time.Second)
- req, err := http.NewRequest("GET", "http://localhost:6029?hello=world", nil)
+ req, err := http.NewRequest("GET", "http://localhost:6032?hello=world", nil)
assert.NoError(t, err)
r, err := http.DefaultClient.Do(req)
@@ -372,7 +371,7 @@ func Test_Service_Middleware(t *testing.T) {
t.Errorf("error closing the Body: error %v", err)
}
- req, err = http.NewRequest("GET", "http://localhost:6029/halt", nil)
+ req, err = http.NewRequest("GET", "http://localhost:6032/halt", nil)
assert.NoError(t, err)
r, err = http.DefaultClient.Do(req)
@@ -386,8 +385,10 @@ func Test_Service_Middleware(t *testing.T) {
err = r.Body.Close()
if err != nil {
+ c.Stop()
t.Errorf("error closing the Body: error %v", err)
}
+ c.Stop()
}
func Test_Service_Listener(t *testing.T) {
@@ -399,7 +400,7 @@ func Test_Service_Listener(t *testing.T) {
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
"enable": true,
- "address": ":6029",
+ "address": ":6033",
"maxRequestSize": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
@@ -448,7 +449,7 @@ func Test_Service_Error(t *testing.T) {
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
"enable": true,
- "address": ":6029",
+ "address": ":6034",
"maxRequestSize": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
@@ -477,7 +478,7 @@ func Test_Service_Error2(t *testing.T) {
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
"enable": true,
- "address": ":6029",
+ "address": ":6035",
"maxRequestSize": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
@@ -506,7 +507,7 @@ func Test_Service_Error3(t *testing.T) {
assert.Error(t, c.Init(&testCfg{httpCfg: `{
"enable": true,
- "address": ":6029",
+ "address": ":6036",
"maxRequestSize": 1024,
"uploads": {
"dir": ` + tmpDir() + `,
diff --git a/service/http/ssl_test.go b/service/http/ssl_test.go
index 49bba6cb..b82aa75c 100644
--- a/service/http/ssl_test.go
+++ b/service/http/ssl_test.go
@@ -84,9 +84,9 @@ func Test_SSL_Service_NoRedirect(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
- "address": ":6029",
+ "address": ":6030",
"ssl": {
- "port": 6900,
+ "port": 6901,
"key": "fixtures/server.key",
"cert": "fixtures/server.crt"
},
@@ -109,19 +109,16 @@ func Test_SSL_Service_NoRedirect(t *testing.T) {
t.Errorf("error during the Serve: error %v", err)
}
}()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
- req, err := http.NewRequest("GET", "http://localhost:6029?hello=world", nil)
+ time.Sleep(time.Second)
+
+ req, err := http.NewRequest("GET", "http://localhost:6030?hello=world", nil)
assert.NoError(t, err)
r, err := sslClient.Do(req)
assert.NoError(t, err)
defer func() {
- err := r.Body.Close()
- if err != nil {
- t.Errorf("fail to close the Body: error %v", err)
- }
+
}()
assert.Nil(t, r.TLS)
@@ -132,6 +129,12 @@ func Test_SSL_Service_NoRedirect(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 201, r.StatusCode)
assert.Equal(t, "WORLD", string(b))
+
+ err2 := r.Body.Close()
+ if err2 != nil {
+ t.Errorf("fail to close the Body: error %v", err2)
+ }
+ c.Stop()
}
func Test_SSL_Service_Redirect(t *testing.T) {
@@ -142,9 +145,9 @@ func Test_SSL_Service_Redirect(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
- "address": ":6029",
+ "address": ":6031",
"ssl": {
- "port": 6900,
+ "port": 6902,
"redirect": true,
"key": "fixtures/server.key",
"cert": "fixtures/server.crt"
@@ -168,19 +171,16 @@ func Test_SSL_Service_Redirect(t *testing.T) {
t.Errorf("error during the Serve: error %v", err)
}
}()
- time.Sleep(time.Millisecond * 100)
- defer c.Stop()
- req, err := http.NewRequest("GET", "http://localhost:6029?hello=world", nil)
+ time.Sleep(time.Second)
+
+ req, err := http.NewRequest("GET", "http://localhost:6031?hello=world", nil)
assert.NoError(t, err)
r, err := sslClient.Do(req)
assert.NoError(t, err)
defer func() {
- err := r.Body.Close()
- if err != nil {
- t.Errorf("fail to close the Body: error %v", err)
- }
+
}()
assert.NotNil(t, r.TLS)
@@ -191,6 +191,12 @@ func Test_SSL_Service_Redirect(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 201, r.StatusCode)
assert.Equal(t, "WORLD", string(b))
+
+ err2 := r.Body.Close()
+ if err2 != nil {
+ t.Errorf("fail to close the Body: error %v", err2)
+ }
+ c.Stop()
}
func Test_SSL_Service_Push(t *testing.T) {
@@ -201,9 +207,9 @@ func Test_SSL_Service_Push(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{httpCfg: `{
- "address": ":6029",
+ "address": ":6032",
"ssl": {
- "port": 6900,
+ "port": 6903,
"redirect": true,
"key": "fixtures/server.key",
"cert": "fixtures/server.crt"
@@ -230,7 +236,7 @@ func Test_SSL_Service_Push(t *testing.T) {
time.Sleep(time.Millisecond * 100)
defer c.Stop()
- req, err := http.NewRequest("GET", "https://localhost:6900?hello=world", nil)
+ req, err := http.NewRequest("GET", "https://localhost:6903?hello=world", nil)
assert.NoError(t, err)
r, err := sslClient.Do(req)