summaryrefslogtreecommitdiff
path: root/service/http/service_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'service/http/service_test.go')
-rw-r--r--service/http/service_test.go77
1 files changed, 64 insertions, 13 deletions
diff --git a/service/http/service_test.go b/service/http/service_test.go
index 69cb7003..c4b2c2c4 100644
--- a/service/http/service_test.go
+++ b/service/http/service_test.go
@@ -53,7 +53,8 @@ func Test_Service_NoConfig(t *testing.T) {
c := service.NewContainer(logger)
c.Register(ID, &Service{})
- c.Init(&testCfg{httpCfg: `{"Enable":true}`})
+ err := c.Init(&testCfg{httpCfg: `{"Enable":true}`})
+ assert.Error(t, err)
s, st := c.Get(ID)
assert.NotNil(t, s)
@@ -138,7 +139,12 @@ func Test_Service_Echo(t *testing.T) {
// should do nothing
s.(*Service).Stop()
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 100)
defer c.Stop()
@@ -147,7 +153,12 @@ func Test_Service_Echo(t *testing.T) {
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer r.Body.Close()
+ 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)
@@ -191,7 +202,12 @@ func Test_Service_Env(t *testing.T) {
// should do nothing
s.(*Service).Stop()
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 100)
defer c.Stop()
@@ -200,7 +216,12 @@ func Test_Service_Env(t *testing.T) {
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer r.Body.Close()
+ 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)
@@ -249,7 +270,12 @@ func Test_Service_ErrorEcho(t *testing.T) {
}
})
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 100)
defer c.Stop()
@@ -258,7 +284,12 @@ func Test_Service_ErrorEcho(t *testing.T) {
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer r.Body.Close()
+ 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)
@@ -304,14 +335,22 @@ func Test_Service_Middleware(t *testing.T) {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/halt" {
w.WriteHeader(500)
- w.Write([]byte("halted"))
+ _, 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() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 100)
defer c.Stop()
@@ -320,7 +359,6 @@ func Test_Service_Middleware(t *testing.T) {
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
@@ -329,19 +367,27 @@ func Test_Service_Middleware(t *testing.T) {
assert.Equal(t, 201, r.StatusCode)
assert.Equal(t, "WORLD", string(b))
+ err = r.Body.Close()
+ if err != nil {
+ t.Errorf("error closing the Body: error %v", err)
+ }
+
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))
+
+ err = r.Body.Close()
+ if err != nil {
+ t.Errorf("error closing the Body: error %v", err)
+ }
}
func Test_Service_Listener(t *testing.T) {
@@ -381,7 +427,12 @@ func Test_Service_Listener(t *testing.T) {
}
})
- go func() { c.Serve() }()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 100)
c.Stop()