You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/11/03 13:58:28 UTC

[GitHub] [apisix-dashboard] membphis commented on a change in pull request #612: test: add e2e test for field hosts in `route` api

membphis commented on a change in pull request #612:
URL: https://github.com/apache/apisix-dashboard/pull/612#discussion_r515748954



##########
File path: api/test/e2e/route_test.go
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package e2e
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestRoute_Host_Params(t *testing.T) {
+	tests := []HttpTestCase{
+		{
+			caseDesc: "invalid host",
+			Object:   MangerApiExpect(t),
+			Path:     "/apisix/admin/routes/r1",
+			Method:   http.MethodPut,
+			Body: `{
+				"uri": "/hello_",
+				"host": "$%$foo.com",
+				"upstream": {
+					"nodes": {
+						"172.16.238.120:1980": 1
+					},
+					"type": "roundrobin"
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusBadRequest,
+		},
+		{
+			caseDesc: "invalid hosts",
+			Object:   MangerApiExpect(t),
+			Method:   http.MethodPut,
+			Path:     "/apisix/admin/routes/r1",
+			Body: `{
+	            "uri": "/hello_",

Review comment:
       bad indentation

##########
File path: api/test/e2e/route_test.go
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package e2e
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestRoute_Host_Params(t *testing.T) {
+	tests := []HttpTestCase{
+		{
+			caseDesc: "invalid host",

Review comment:
       I think we can remove the case `invalid host` for E2E.
   
   we can put them to a different function, eg: `TestRoute_Invalid_Host`.

##########
File path: api/test/e2e/route_test.go
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package e2e
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestRoute_Host_Params(t *testing.T) {
+	tests := []HttpTestCase{
+		{
+			caseDesc: "invalid host",
+			Object:   MangerApiExpect(t),
+			Path:     "/apisix/admin/routes/r1",
+			Method:   http.MethodPut,
+			Body: `{
+				"uri": "/hello_",
+				"host": "$%$foo.com",
+				"upstream": {
+					"nodes": {
+						"172.16.238.120:1980": 1
+					},
+					"type": "roundrobin"
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusBadRequest,
+		},
+		{
+			caseDesc: "invalid hosts",

Review comment:
       ditto

##########
File path: api/test/e2e/route_test.go
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package e2e
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestRoute_Host_Params(t *testing.T) {

Review comment:
       how about these steps? it seems clear:
   
   1. hit route `/hello` -> will fail, no route
   2. add route, `/hello` -> success
   3. hit route `/hello` -> success
   4. hit route `/not_found` ->will fail, not match route
   5. delete route `/hello` -> success
   6. hit route `/hello` -> will fail, no route
   

##########
File path: api/test/e2e/route_test.go
##########
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package e2e
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestRoute_Host_Params(t *testing.T) {
+	tests := []HttpTestCase{
+		{
+			caseDesc: "invalid host",
+			Object:   MangerApiExpect(t),
+			Path:     "/apisix/admin/routes/r1",
+			Method:   http.MethodPut,
+			Body: `{
+				"uri": "/hello_",
+				"host": "$%$foo.com",
+				"upstream": {
+					"nodes": {
+						"172.16.238.120:1980": 1
+					},
+					"type": "roundrobin"
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusBadRequest,
+		},
+		{
+			caseDesc: "invalid hosts",
+			Object:   MangerApiExpect(t),
+			Method:   http.MethodPut,
+			Path:     "/apisix/admin/routes/r1",
+			Body: `{
+	            "uri": "/hello_",

Review comment:
       please fix similar points

##########
File path: api/test/e2e/base.go
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package e2e
+
+import (
+	"bytes"
+	"io/ioutil"
+	"net/http"
+	"testing"
+	"time"
+
+	"github.com/gavv/httpexpect/v2"
+	"github.com/tidwall/gjson"
+)
+
+var token string
+
+func init() {
+	//login to get auth token
+	requestBody := []byte(`{
+		"username": "admin",
+		"password": "admin"
+	}`)
+
+	url := "http://127.0.0.1:8080/apisix/admin/user/login"
+	req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(requestBody))
+	if err != nil {
+		panic(err)
+	}
+
+	client := &http.Client{}
+	resp, err := client.Do(req)
+	if err != nil {
+		panic(err)
+	}
+
+	defer resp.Body.Close()
+
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		panic(err)
+	}
+
+	respond := gjson.ParseBytes(body)
+	token = respond.Get("data.token").String()
+}
+
+func MangerApiExpect(t *testing.T) *httpexpect.Expect {
+	return httpexpect.New(t, "http://127.0.0.1:8080")
+}
+
+func APISIXExpect(t *testing.T) *httpexpect.Expect {
+	return httpexpect.New(t, "http://127.0.0.1:9080")
+}
+
+var sleepTime = time.Duration(100) * time.Millisecond
+
+type HttpTestCase struct {
+	caseDesc      string
+	Object        *httpexpect.Expect
+	Method        string
+	Path          string
+	Body          string
+	Headers       map[string]string
+	ExpectStatus  int
+	ExpectCode    int
+	ExpectMessage string
+	ExpectBody    string
+	Sleep         time.Duration //ms
+}
+
+func testCaseCheck(tc HttpTestCase) {
+	//init
+	expectObj := tc.Object
+	var req *httpexpect.Request
+	switch tc.Method {
+	case http.MethodGet:
+		req = expectObj.GET(tc.Path)
+	case http.MethodPut:
+		req = expectObj.PUT(tc.Path)
+	case http.MethodPost:
+		req = expectObj.POST(tc.Path)
+	case http.MethodDelete:
+		req = expectObj.DELETE(tc.Path)
+	case http.MethodPatch:
+		req = expectObj.PATCH(tc.Path)
+	default:
+	}
+
+	if req == nil {
+		panic("fail to init request")
+	}
+
+	if tc.Sleep != 0 {
+		time.Sleep(tc.Sleep)
+	}
+
+	//set header
+	for key, val := range tc.Headers {
+		req.WithHeader(key, val)
+	}
+
+	//set body
+	if tc.Body != "" {
+		req.WithText(tc.Body)
+	}
+
+	//respond check
+	resp := req.Expect()
+
+	//match http status
+	if tc.ExpectStatus != 0 {
+		resp.Status(tc.ExpectStatus)
+	}
+
+	//match body

Review comment:
       @nic-chen any news about this?

##########
File path: api/test/docker/manager-api-conf.json
##########
@@ -0,0 +1,31 @@
+{
+    "conf": {
+      "syslog": {
+        "host": "127.0.0.1"
+      },
+      "listen": {
+        "host": "0.0.0.0",
+        "port": 8080
+      },
+      "dag-lib-path": "",
+      "etcd": {
+        "endpoints": "172.16.238.10:2379,172.16.238.11:2379,172.16.238.12:2379"
+      }
+    },
+    "authentication": {
+      "session": {
+        "secret": "secret",
+        "expireTime": 3600
+      },
+      "user": [
+        {
+          "username": "admin",
+          "password": "admin"
+        },
+        {

Review comment:
       two problems:
   
   1. `user` -> `users`: I will create a new issue to fix this.
   2. why do we need two users here?

##########
File path: api/test/docker/manager-api-conf.json
##########
@@ -0,0 +1,31 @@
+{
+    "conf": {
+      "syslog": {
+        "host": "127.0.0.1"
+      },
+      "listen": {
+        "host": "0.0.0.0",
+        "port": 8080
+      },
+      "dag-lib-path": "",
+      "etcd": {
+        "endpoints": "172.16.238.10:2379,172.16.238.11:2379,172.16.238.12:2379"
+      }
+    },
+    "authentication": {
+      "session": {
+        "secret": "secret",
+        "expireTime": 3600
+      },
+      "user": [
+        {
+          "username": "admin",
+          "password": "admin"
+        },
+        {

Review comment:
       support multiple user to login, I got it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org