| /* |
| * 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 ( |
| "io/ioutil" |
| "net/http" |
| "testing" |
| "time" |
| |
| "github.com/stretchr/testify/assert" |
| "github.com/tidwall/gjson" |
| ) |
| |
| func TestID_Using_Int(t *testing.T) { |
| tests := []HttpTestCase{ |
| { |
| Desc: "create upstream", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/upstreams", |
| Body: `{ |
| "id": 1, |
| "nodes": [{ |
| "host": "172.16.238.20", |
| "port": 1980, |
| "weight": 1 |
| }], |
| "type": "roundrobin" |
| }`, |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| }, |
| { |
| Desc: "create route using the upstream just created", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/routes/1", |
| Body: `{ |
| "name": "route1", |
| "uri": "/hello", |
| "upstream_id": 1 |
| }`, |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit the route just created", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusOK, |
| ExpectBody: "hello world", |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "create service", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/services", |
| Body: `{ |
| "id": 1, |
| "upstream_id": 1 |
| }`, |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| }, |
| { |
| Desc: "update route to use the service just created", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/routes/1", |
| Body: `{ |
| "name": "route1", |
| "uri": "/hello", |
| "service_id": 1 |
| }`, |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit the route just updated", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusOK, |
| ExpectBody: "hello world", |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "delete the route", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/routes/1", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| }, |
| { |
| Desc: "delete the service", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/services/1", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "make sure the service has been deleted", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodGet, |
| Path: "/apisix/admin/services/1", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "delete the upstream", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/upstreams/1", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| }, |
| { |
| Desc: "make sure the upstream has been deleted", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodGet, |
| Path: "/apisix/admin/upstreams/1", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit deleted route", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| } |
| |
| for _, tc := range tests { |
| testCaseCheck(tc, t) |
| } |
| } |
| |
| func TestID_Using_String(t *testing.T) { |
| tests := []HttpTestCase{ |
| { |
| Desc: "create upstream", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/upstreams", |
| Body: `{ |
| "id": "2", |
| "nodes": [{ |
| "host": "172.16.238.20", |
| "port": 1980, |
| "weight": 1 |
| }], |
| "type": "roundrobin" |
| }`, |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| }, |
| { |
| Desc: "create route using the upstream just created", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/routes/2", |
| Body: `{ |
| "name": "route2", |
| "uri": "/hello", |
| "upstream_id": "2" |
| }`, |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit the route just created", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusOK, |
| ExpectBody: "hello world", |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "delete the route", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/routes/2", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| }, |
| { |
| Desc: "delete the upstream", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/upstreams/2", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "make sure the upstream has been deleted", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodGet, |
| Path: "/apisix/admin/upstreams/2", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit deleted route", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| } |
| |
| for _, tc := range tests { |
| testCaseCheck(tc, t) |
| } |
| } |
| |
| func TestID_Crossing(t *testing.T) { |
| tests := []HttpTestCase{ |
| { |
| Desc: "create upstream by admin api", |
| Object: APISIXExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/upstreams", |
| Body: `{ |
| "id": 3, |
| "nodes": [{ |
| "host": "172.16.238.20", |
| "port": 1980, |
| "weight": 1 |
| }], |
| "type": "roundrobin" |
| }`, |
| Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"}, |
| ExpectStatus: http.StatusCreated, |
| }, |
| { |
| Desc: "create route by admin api", |
| Object: APISIXExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/routes/3", |
| Body: `{ |
| "name": "route3", |
| "uri": "/hello", |
| "upstream_id": 3 |
| }`, |
| Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"}, |
| ExpectStatus: http.StatusCreated, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "verify that the upstream is available for manager api", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodGet, |
| Path: "/apisix/admin/upstreams/3", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| ExpectBody: `"id":3`, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "verify that the route is available for manager api", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodGet, |
| Path: "/apisix/admin/routes/3", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| ExpectBody: `"upstream_id":3`, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit the route just created", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusOK, |
| ExpectBody: "hello world", |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "delete the route", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/routes/3", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| }, |
| { |
| Desc: "delete the upstream", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/upstreams/3", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "make sure the upstream has been deleted", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodGet, |
| Path: "/apisix/admin/upstreams/3", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit deleted route", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| } |
| |
| for _, tc := range tests { |
| testCaseCheck(tc, t) |
| } |
| } |
| |
| func TestID_Not_In_Body(t *testing.T) { |
| tests := []HttpTestCase{ |
| { |
| Desc: "make sure the route is not created", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "create route that has no ID in request body by admin api", |
| Object: APISIXExpect(t), |
| Method: http.MethodPut, |
| Path: "/apisix/admin/routes/r1", |
| Body: `{ |
| "name": "route1", |
| "uri": "/hello", |
| "upstream": { |
| "type": "roundrobin", |
| "nodes": { |
| "172.16.238.20:1980": 1 |
| } |
| } |
| }`, |
| Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"}, |
| ExpectStatus: http.StatusCreated, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "verify that the route is available for manager api", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodGet, |
| Path: "/apisix/admin/routes/r1", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| ExpectBody: `"id":"r1"`, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit the route just created", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusOK, |
| ExpectBody: "hello world", |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "delete the route", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/routes/r1", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| }, |
| { |
| Desc: "hit deleted route", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusNotFound, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "create route that has no ID in request body by admin api (POST)", |
| Object: APISIXExpect(t), |
| Method: http.MethodPost, |
| Path: "/apisix/admin/routes", |
| Body: `{ |
| "uri": "/hello", |
| "upstream": { |
| "type": "roundrobin", |
| "nodes": { |
| "172.16.238.20:1980": 1 |
| } |
| } |
| }`, |
| Headers: map[string]string{"X-API-KEY": "edd1c9f034335f136f87ad84b625c8f1"}, |
| ExpectStatus: http.StatusCreated, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "verify that the route is available for manager api", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodGet, |
| Path: "/apisix/admin/routes", |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| ExpectBody: `"uri":"/hello"`, |
| Sleep: sleepTime, |
| }, |
| { |
| Desc: "hit the route just created", |
| Object: APISIXExpect(t), |
| Method: http.MethodGet, |
| Path: "/hello", |
| ExpectStatus: http.StatusOK, |
| ExpectBody: "hello world", |
| Sleep: sleepTime, |
| }, |
| } |
| |
| for _, tc := range tests { |
| testCaseCheck(tc, t) |
| } |
| |
| // delete the route created by POST |
| time.Sleep(time.Duration(100) * time.Millisecond) |
| request, _ := http.NewRequest("GET", ManagerAPIHost+"/apisix/admin/routes", nil) |
| request.Header.Add("Authorization", token) |
| resp, err := http.DefaultClient.Do(request) |
| assert.Nil(t, err) |
| defer resp.Body.Close() |
| respBody, _ := ioutil.ReadAll(resp.Body) |
| list := gjson.Get(string(respBody), "data.rows").Value().([]interface{}) |
| for _, item := range list { |
| route := item.(map[string]interface{}) |
| tc := HttpTestCase{ |
| Desc: "delete the route", |
| Object: ManagerApiExpect(t), |
| Method: http.MethodDelete, |
| Path: "/apisix/admin/routes/" + route["id"].(string), |
| Headers: map[string]string{"Authorization": token}, |
| ExpectStatus: http.StatusOK, |
| } |
| testCaseCheck(tc, t) |
| } |
| } |