Skip to content

Commit

Permalink
chore: Improve the http body check in e2e test case (#1250)
Browse files Browse the repository at this point in the history
* chore: improve the http body check in e2e testcase

Signed-off-by: imjoey <majunjiev@gmail.com>

* Restore the test case mistakenly delete

Signed-off-by: imjoey <majunjiev@gmail.com>

Co-authored-by: 琚致远 <juzhiyuan@apache.org>
Co-authored-by: nic-chen <33000667+nic-chen@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 12, 2021
1 parent cdab902 commit 1385fc2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 55 deletions.
68 changes: 46 additions & 22 deletions api/test/e2e/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net"
"net/http"
"os/exec"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -163,21 +164,21 @@ func BatchTestServerPort(t *testing.T, times int) map[string]int {
var sleepTime = time.Duration(300) * time.Millisecond

type HttpTestCase struct {
Desc string
Object *httpexpect.Expect
Method string
Path string
Query string
Body string
Headers map[string]string
Headers_test map[string]interface{}
ExpectStatus int
ExpectCode int
ExpectMessage string
ExpectBody string
UnexpectedBody string
ExpectHeaders map[string]string
Sleep time.Duration //ms
Desc string
Object *httpexpect.Expect
Method string
Path string
Query string
Body string
Headers map[string]string
Headers_test map[string]interface{}
ExpectStatus int
ExpectCode int
ExpectMessage string
ExpectBody interface{}
UnexpectBody interface{}
ExpectHeaders map[string]string
Sleep time.Duration //ms
}

func testCaseCheck(tc HttpTestCase, t *testing.T) {
Expand Down Expand Up @@ -241,15 +242,38 @@ func testCaseCheck(tc HttpTestCase, t *testing.T) {
}

// match body
if tc.ExpectBody != "" {
resp.Body().Contains(tc.ExpectBody)
if tc.ExpectBody != nil {
assert.Contains(t, []string{"string", "[]string"}, reflect.TypeOf(tc.ExpectBody).String())
if body, ok := tc.ExpectBody.(string); ok {
if body == "" {
// "" indicates the body is expected to be empty
resp.Body().Empty()
} else {
resp.Body().Contains(body)
}
} else if bodies, ok := tc.ExpectBody.([]string); ok && len(bodies) != 0 {
for _, b := range bodies {
resp.Body().Contains(b)
}
}
}

// match UnexpectedBody
if tc.UnexpectedBody != "" {
resp.Body().NotContains(tc.UnexpectedBody)
// match UnexpectBody
if tc.UnexpectBody != nil {
assert.Contains(t, []string{"string", "[]string"}, reflect.TypeOf(tc.UnexpectBody).String())
if body, ok := tc.UnexpectBody.(string); ok {
// "" indicates the body is expected to be non empty
if body == "" {
resp.Body().NotEmpty()
} else {
resp.Body().NotContains(body)
}
} else if bodies, ok := tc.UnexpectBody.([]string); ok && len(bodies) != 0 {
for _, b := range bodies {
resp.Body().NotContains(b)
}
}
}

})
}

Expand All @@ -276,7 +300,7 @@ func CleanAPISIXErrorLog(t *testing.T) {
pwd := string(pwdByte)
pwd = strings.Replace(pwd, "\n", "", 1)
pwd = pwd[:strings.Index(pwd, "/e2e")]
cmd = exec.Command("sudo", "echo", " > ", pwd + "/docker/apisix_logs/error.log")
cmd = exec.Command("sudo", "echo", " > ", pwd+"/docker/apisix_logs/error.log")
_, err = cmd.CombinedOutput()
if err != nil {
fmt.Println("cmd error:", err.Error())
Expand Down
57 changes: 24 additions & 33 deletions api/test/e2e/server_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,13 @@ func TestServerInfo_Get_OmitEmptyValue(t *testing.T) {
time.Sleep(2 * time.Second)
testCases := []HttpTestCase{
{
Desc: "get server info",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/server_info/apisix-server1",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
UnexpectedBody: "\"create_time\":",
},
{
Desc: "get server info",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/server_info/apisix-server1",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
UnexpectedBody: "\"update_time\":",
Desc: "get server info",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/server_info/apisix-server1",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
UnexpectBody: []string{"\"create_time\":", "\"update_time\":"},
},
}

Expand All @@ -121,25 +112,25 @@ func TestServerInfo_Get_OmitEmptyValue(t *testing.T) {
func TestServerInfo_List_OmitEmptyValue(t *testing.T) {
testCases := []HttpTestCase{
{
Desc: "list all server info",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/server_info",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: "\"total_size\":2",
UnexpectedBody: "\"create_time\":",
Desc: "list all server info",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/server_info",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: "\"total_size\":2",
UnexpectBody: []string{"\"create_time\":", "\"update_time\":"},
},
{
Desc: "list server info with hostname",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/server_info",
Query: "hostname=apisix_",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: "\"total_size\":2",
UnexpectedBody: "\"update_time\":",
Desc: "list server info with hostname",
Object: ManagerApiExpect(t),
Path: "/apisix/admin/server_info",
Query: "hostname=apisix_",
Method: http.MethodGet,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
ExpectBody: "\"total_size\":2",
UnexpectBody: []string{"\"create_time\":", "\"update_time\":"},
},
}

Expand Down

0 comments on commit 1385fc2

Please sign in to comment.