12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543 |
- /*
- * 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 route_test
- import (
- "net/http"
- "strings"
- "time"
- . "github.com/onsi/ginkgo/v2"
- "github.com/stretchr/testify/assert"
- "github.com/apisix/manager-api/test/e2e/base"
- )
- var _ = Describe("Route", func() {
- Context("test route export data empty", func() {
- It("Export route when data is empty", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `{"code":10000,"message":"Route data is empty, cannot be exported","data":null`,
- })
- })
- })
- Context("test route export", func() {
- // 1.Export data as the route of URIs Hosts
- exportStrR1 := `
- "/hello_": {
- "get": {
- "operationId": "aaaaGET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "x-apisix-enable_websocket": false,
- "x-apisix-hosts": ["foo.com", "*.bar.com"],
- "x-apisix-id":"r1",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-plugins": {
- "limit-count": {
- "count": 2,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- },
- "post": {
- "operationId": "aaaaPOST",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "x-apisix-enable_websocket": false,
- "x-apisix-hosts": ["foo.com", "*.bar.com"],
- "x-apisix-id":"r1",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-plugins": {
- "limit-count": {
- "count": 2,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- }
- }`
- exportStrR1 = replaceStr(exportStrR1)
- It("hit route that not exist", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello_",
- Headers: map[string]string{"Host": "foo.com"},
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- })
- })
- It("create route with uris and hosts to test whether the uris parsing is correct", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "name": "aaaa",
- "labels": {
- "build":"16",
- "env":"production",
- "version":"v2"
- },
- "plugins": {
- "limit-count": {
- "count": 2,
- "time_window": 60,
- "rejected_code": 503,
- "key": "remote_addr",
- "policy": "local"
- }
- },
- "status": 1,
- "uris": ["/hello_"],
- "hosts": ["foo.com", "*.bar.com"],
- "methods": ["GET", "POST"],
- "upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("export route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `{"components":{},"info":{"title":"RoutesExport","version":"3.0.0"},"openapi":"3.0.0","paths":{` + exportStrR1 + "}}",
- })
- })
- // 2.Export data as the route of URI host
- exportStrR2 := `
- "/hello2": {
- "get": {
- "operationId": "aaaa2GET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "x-apisix-enable_websocket": false,
- "x-apisix-host": "*.bar.com",
- "x-apisix-id":"r2",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-plugins": {
- "limit-count": {
- "count": 2,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- },
- "post": {
- "operationId": "aaaa2POST",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "x-apisix-enable_websocket": false,
- "x-apisix-host": "*.bar.com",
- "x-apisix-id":"r2",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-plugins": {
- "limit-count": {
- "count": 2,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- }
- }`
- exportStrR2 = replaceStr(exportStrR2)
- It("hit route2 that not exist", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello2",
- Headers: map[string]string{"Host": "bar.com"},
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- })
- })
- It("create route2 with uri and host to test whether the uri parsing is correct", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r2",
- Body: `{
- "name": "aaaa2",
- "labels": {
- "build":"16",
- "env":"production",
- "version":"v2"
- },
- "plugins": {
- "limit-count": {
- "count": 2,
- "time_window": 60,
- "rejected_code": 503,
- "key": "remote_addr",
- "policy": "local"
- }
- },
- "status": 1,
- "uri": "/hello2",
- "host": "*.bar.com",
- "methods": ["GET", "POST"],
- "upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("export route2", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `{"components":{},"info":{"title":"RoutesExport","version":"3.0.0"},"openapi":"3.0.0","paths":{` + exportStrR2 + "}}",
- })
- })
- It("export route and route2", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r1,r2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `{"components":{},"info":{"title":"RoutesExport","version":"3.0.0"},"openapi":"3.0.0","paths":{` + exportStrR2 + "," + exportStrR1 + "}}",
- })
- })
- It("use the exportall interface to export all routes", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `{"components":{},"info":{"title":"RoutesExport","version":"3.0.0"},"openapi":"3.0.0","paths":{` + exportStrR2 + "," + exportStrR1 + "}}",
- })
- })
- It("delete the route just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello_",
- Headers: map[string]string{"Host": "bar.com"},
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- It("delete the route2 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route2 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello2",
- Headers: map[string]string{"Host": "bar.com"},
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- // 4.Create a service that contains complete data and use the service_ id create route
- serviceStrS1 := `
- "name": "testservice",
- "desc": "testservice_desc",
- "upstream": {
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }],
- "type": "roundrobin"
- },
- "plugins": {
- "limit-count": {
- "count": 100,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- }
- },
- "labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "enable_websocket": true
- `
- serviceStrS1 = replaceStr(serviceStrS1)
- exportStrR3 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/hello3": {
- "get": {
- "operationId": "route3GET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "x-apisix-enable_websocket": false,
- "x-apisix-id":"r3",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-plugins": {
- "limit-count": {
- "count": 100,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }],
- "type": "roundrobin"
- }
- }
- }
- }`
- exportStrR3 = replaceStr(exportStrR3)
- It("create service with all options", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- Body: `{
- "name": "testservice",
- "desc": "testservice_desc",
- "labels": {
- "build":"16",
- "env":"production",
- "version":"v2"
- },
- "enable_websocket":true,
- "plugins": {
- "limit-count": {
- "count": 100,
- "time_window": 60,
- "policy": "local",
- "rejected_code": 503,
- "key": "remote_addr"
- }
- },
- "upstream": {
- "type": "roundrobin",
- "create_time":1602883670,
- "update_time":1602893670,
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- ExpectStatus: http.StatusOK,
- })
- })
- It("get the service s1", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectCode: http.StatusOK,
- ExpectBody: serviceStrS1,
- })
- })
- It("create route3 using the service id just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r3",
- Body: `{
- "name": "route3",
- "methods": ["GET"],
- "uri": "/hello3",
- "service_id": "s1"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectCode: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route3", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r3",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectCode: http.StatusOK,
- ExpectBody: exportStrR3,
- })
- })
- It("delete the route3 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r3",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectCode: http.StatusOK,
- })
- })
- It("hit the route3 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello3",
- ExpectStatus: http.StatusNotFound,
- Sleep: base.SleepTime,
- })
- })
- It("delete the service1", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- // 5.Create a service containing plugin and a route containing plugin to test the fusion of exported data
- // This test case tests the creation of a complete service.
- // And create a complete route, export rules as route upstream data for high priority, direct use.
- // However, if the data in the service plugins does not exist in the route, it will be fused and exported.
- // If it exists, the data in route will be used first
- serviceStrS2 := `
- "name": "testservice",
- "desc": "testservice_desc",
- "upstream": {
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }],
- "type": "roundrobin"
- },
- "plugins": {
- "limit-count": {
- "count": 100,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- }
- },
- "labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "enable_websocket": true`
- serviceStrS2 = replaceStr(serviceStrS2)
- exportStrR4 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/hello": {
- "get": {
- "operationId": "route4GET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "x-apisix-enable_websocket": false,
- "x-apisix-id":"r4",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-plugins": {
- "limit-count": {
- "count": 100,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- },
- "prometheus": {
- "disable": false
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }],
- "type": "roundrobin"
- }
- }
- }
- }`
- exportStrR4 = replaceStr(exportStrR4)
- It("create service with all options", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- Body: `{
- "name": "testservice",
- "desc": "testservice_desc",
- "labels": {
- "build":"16",
- "env":"production",
- "version":"v2"
- },
- "enable_websocket":true,
- "plugins": {
- "limit-count": {
- "count": 100,
- "time_window": 60,
- "rejected_code": 503,
- "key": "remote_addr",
- "policy": "local"
- }
- },
- "upstream": {
- "type": "roundrobin",
- "create_time":1602883670,
- "update_time":1602893670,
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- ExpectStatus: http.StatusOK,
- })
- })
- It("get the service s2", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: serviceStrS2,
- })
- })
- It("Create Route4 and test the priority merging function of upstream, label and plugin when both service and route are included", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r4",
- Body: `{
- "name": "route4",
- "methods": ["GET"],
- "uri": "/hello",
- "service_id": "s2",
- "enable_websocket":false,
- "plugins": {
- "prometheus": {
- "disable": false
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route4", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r4",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrR4,
- })
- })
- It("delete the route4 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r4",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route4 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- })
- })
- It("delete the service2", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- // 6.Create a service according to the upstream ID and a route according to the service ID.
- // The test export also contains the upstream.
- // Use the upstream of route.
- serviceStrS3 := `
- "name": "testservice",
- "desc": "testservice_desc",
- "upstream_id": "1",
- "plugins": {
- "limit-count": {
- "count": 100,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- }
- },
- "labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "enable_websocket": true`
- serviceStrS3 = replaceStr(serviceStrS3)
- exportStrR5 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/hello5": {
- "get": {
- "operationId": "route5GET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "x-apisix-enable_websocket": false,
- "x-apisix-id":"r5",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-plugins": {
- "limit-count": {
- "count": 100,
- "key": "remote_addr",
- "policy": "local",
- "rejected_code": 503,
- "time_window": 60
- },
- "prometheus": {
- "disable": false
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1981,
- "weight": 1
- }],
- "type": "roundrobin"
- }
- }
- }
- }`
- exportStrR5 = replaceStr(exportStrR5)
- It("create upstream", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/upstreams/1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- Body: `{
- "nodes": [
- {
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }
- ],
- "type": "roundrobin"
- }`,
- ExpectStatus: http.StatusOK,
- })
- })
- It("create service with upstream id", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s3",
- Headers: map[string]string{"Authorization": base.GetToken()},
- Body: `{
- "name": "testservice",
- "desc": "testservice_desc",
- "labels": {
- "build":"16",
- "env":"production",
- "version":"v2"
- },
- "enable_websocket":true,
- "plugins": {
- "limit-count": {
- "count": 100,
- "time_window": 60,
- "rejected_code": 503,
- "key": "remote_addr",
- "policy": "local"
- }
- },
- "upstream_id": "1"
- }`,
- ExpectStatus: http.StatusOK,
- })
- })
- It("get the service s3", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s3",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: serviceStrS3,
- })
- })
- It("Create a route5 with the id of the service3 created with upstream id", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r5",
- Body: `{
- "name": "route5",
- "methods": ["GET"],
- "uri": "/hello5",
- "service_id": "s3",
- "enable_websocket":false,
- "plugins": {
- "prometheus": {
- "disable": false
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1981,
- "weight": 1
- }]
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route5", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r5",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrR5,
- })
- })
- It("delete the route5 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r5",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route5 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello5",
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- It("delete the service3", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s3",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("remove upstream", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/upstreams/1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- // 8.Create and export route according to upstream ID
- exportStrR8 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/hello": {
- "get": {
- "operationId": "route8GET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "x-apisix-enable_websocket": false,
- "x-apisix-id":"r8",
- "x-apisix-plugins": {
- "prometheus": {
- "disable": false
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "id": "3"
- `
- exportStrR8 = replaceStr(exportStrR8)
- It("create upstream3", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/upstreams/3",
- Body: `{
- "nodes": [
- {
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }
- ],
- "type": "roundrobin"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("Create a route8 using upstream id", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r8",
- Body: `{
- "name": "route8",
- "methods": ["GET"],
- "uri": "/hello",
- "enable_websocket":false,
- "plugins": {
- "prometheus": {
- "disable": false
- }
- },
- "upstream_id": "3"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route8", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r8",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrR8,
- })
- })
- It("delete the route8 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r8",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route8 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- It("remove upstream3", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/upstreams/3",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- // 9.Create service according to upstream1 ID
- // Create route according to upstream2 ID and service ID
- // Export route
- serviceStrS4 := `
- "name": "testservice",
- "desc": "testservice_desc",
- "upstream_id": "4",
- "enable_websocket": true`
- serviceStrS4 = replaceStr(serviceStrS4)
- exportStrR9 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/hello": {
- "get": {
- "operationId": "route_allGET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "summary": "所有",
- "x-apisix-enable_websocket": false,
- "x-apisix-id":"r9",
- "x-apisix-labels": {
- "API_VERSION": "v1",
- "test": "1"
- },
- "x-apisix-plugins": {
- "prometheus": {
- "disable": false
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "id": "5"
- `
- exportStrR9 = replaceStr(exportStrR9)
- It("create upstream4", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/upstreams/4",
- Body: `{
- "nodes": [
- {
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }
- ],
- "type": "roundrobin"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("create upstream5", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/upstreams/5",
- Body: `{
- "name": "upstream5",
- "nodes": [
- {
- "host": "` + base.UpstreamIp + `",
- "port": 1981,
- "weight": 1
- }
- ],
- "type": "roundrobin"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("create service", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s4",
- Body: `{
- "name": "testservice",
- "desc": "testservice_desc",
- "enable_websocket":true,
- "upstream_id": "4"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("get the service s4", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s4",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: serviceStrS4,
- })
- })
- It("Create a route9 using upstream id and service id", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r9",
- Body: `{
- "name": "route_all",
- "desc": "所有",
- "status": 1,
- "methods": ["GET"],
- "priority": 0,
- "service_id": "s4",
- "labels": {
- "test": "1",
- "API_VERSION": "v1"
- },
- "vars": [
- ["arg_name", "==", "test"]
- ],
- "uri": "/hello",
- "enable_websocket":false,
- "plugins": {
- "prometheus": {
- "disable": false
- }
- },
- "upstream_id": "5"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route9", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r9",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrR9,
- })
- })
- It("delete the route9 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r9",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route9 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- It("delete the service4", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s4",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("remove upstream4", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/upstreams/4",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("remove upstream5", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/upstreams/5",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- // 10.Creating route10 using service ID does not contain upstream data
- serviceStrS5 := `
- "name": "testservice",
- "desc": "testservice_desc",
- "upstream_id": "6",
- "enable_websocket": true`
- serviceStrS5 = replaceStr(serviceStrS5)
- exportStrR10 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/hello": {
- "get": {
- "operationId": "route_allGET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "summary": "所有",
- "x-apisix-enable_websocket": false,
- "x-apisix-id":"r10",
- "x-apisix-labels": {
- "API_VERSION": "v1",
- "test": "1"
- },
- "x-apisix-plugins": {
- "prometheus": {
- "disable": false
- }
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "id": "6"
- `
- exportStrR10 = replaceStr(exportStrR10)
- It("create upstream6", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/upstreams/6",
- Body: `{
- "nodes": [
- {
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }
- ],
- "type": "roundrobin"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("create service", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s5",
- Body: `{
- "name": "testservice",
- "desc": "testservice_desc",
- "enable_websocket":true,
- "upstream_id": "6"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("get the service s5", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s5",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: serviceStrS5,
- })
- })
- It("Creating route10 using service ID does not contain upstream data", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r10",
- Body: `{
- "name": "route_all",
- "desc": "所有",
- "status": 1,
- "methods": ["GET"],
- "priority": 0,
- "service_id": "s5",
- "labels": {
- "test": "1",
- "API_VERSION": "v1"
- },
- "vars": [
- ["arg_name", "==", "test"]
- ],
- "uri": "/hello",
- "enable_websocket":false,
- "plugins": {
- "prometheus": {
- "disable": false
- }
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route10", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r10",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrR10,
- })
- })
- It("delete the route10 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r10",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route10 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- It("delete the service5", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s5",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("remove upstream6", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/upstreams/6",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- })
- Context("test export route with jwt plugin", func() {
- It("make sure the route is not created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- })
- })
- It("create route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "name": "route1",
- "uri": "/hello",
- "plugins": {
- "jwt-auth": {}
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `"code":0`,
- })
- })
- It("make sure the consumer is not created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/consumers/jack",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusNotFound,
- })
- })
- It("create consumer", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/consumers",
- Body: `{
- "username": "jack",
- "plugins": {
- "jwt-auth": {
- "key": "user-key",
- "secret": "my-secret-key",
- "algorithm": "HS256"
- }
- },
- "desc": "test description"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("create public api for JWT sign", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/jwt-sign",
- Body: `{
- "name": "jwt-auth",
- "uri": "/apisix/plugin/jwt/sign",
- "plugins": {
- "public-api": {}
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `"code":0`,
- })
- })
- jwtToken := ""
- exportStrJWT := ""
- It("sign jwt token", func() {
- time.Sleep(base.SleepTime)
- // sign jwt token
- body, status, err := base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=user-key", nil)
- assert.Nil(GinkgoT(), err)
- assert.Equal(GinkgoT(), http.StatusOK, status)
- jwtToken = string(body)
- // sign jwt token with not exists key
- _, status, err = base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=not-exist-key", nil)
- assert.Nil(GinkgoT(), err)
- assert.Equal(GinkgoT(), http.StatusNotFound, status)
- exportStrJWT = `
- "components": {
- "securitySchemes": {
- "bearerAuth": {
- "bearerFormat": "JWT",
- "scheme": "bearer",
- "type": "http"
- }
- }
- },
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- `
- exportStrJWT = replaceStr(exportStrJWT)
- // verify token and clean test data
- })
- It("verify route with correct jwt token", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- Headers: map[string]string{"Authorization": jwtToken},
- ExpectStatus: http.StatusOK,
- ExpectBody: "hello world",
- Sleep: base.SleepTime,
- })
- })
- It("export route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrJWT,
- })
- })
- It("delete consumer", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/consumers/jack",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("verify route with the jwt token from just deleted consumer", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- Headers: map[string]string{"Authorization": jwtToken},
- ExpectStatus: http.StatusUnauthorized,
- ExpectBody: `{"message":"Missing related consumer"}`,
- Sleep: base.SleepTime,
- })
- })
- It("delete route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("verify the deleted route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- ExpectStatus: http.StatusNotFound,
- Sleep: base.SleepTime,
- })
- })
- exportStrJWTNoAlgorithm := `
- "components": {
- "securitySchemes": {
- "bearerAuth": {
- "bearerFormat": "JWT",
- "scheme": "bearer",
- "type": "http"
- }
- }
- },
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- `
- exportStrJWTNoAlgorithm = replaceStr(exportStrJWTNoAlgorithm)
- It("create consumer with jwt (no algorithm)", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/consumers",
- Body: `{
- "username":"consumer_1",
- "desc": "test description",
- "plugins":{
- "jwt-auth":{
- "exp":86400,
- "key":"user-key",
- "secret":"my-secret-key"
- }
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectBody: `"code":0`,
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("get the consumer", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/consumers/consumer_1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `"username":"consumer_1"`,
- Sleep: base.SleepTime,
- })
- })
- It("create the route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "name": "route1",
- "uri": "/hello",
- "plugins": {
- "jwt-auth": {}
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- jwttoken := ""
- It("sign jwt token", func() {
- // sign jwt token
- body, status, err := base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=user-key", nil)
- assert.Nil(GinkgoT(), err)
- assert.Equal(GinkgoT(), http.StatusOK, status)
- jwttoken = string(body)
- })
- It("hit route with jwt token", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- Headers: map[string]string{"Authorization": jwttoken},
- ExpectStatus: http.StatusOK,
- ExpectBody: "hello world",
- Sleep: base.SleepTime,
- })
- })
- It("export route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrJWTNoAlgorithm,
- })
- })
- It("delete consumer", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/consumers/consumer_1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: `"code":0`,
- })
- })
- It("after delete consumer verify it again", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/consumers/jack",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusNotFound,
- Sleep: base.SleepTime,
- })
- })
- It("delete the route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- })
- Context("test export route with auth plugin", func() {
- It("make sure the route is not created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- ExpectStatus: http.StatusNotFound,
- })
- })
- It("create route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "name": "route1",
- "uri": "/hello",
- "plugins": {
- "key-auth": {},
- "basic-auth": {}
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": [{
- "host": "` + base.UpstreamIp + `",
- "port": 1980,
- "weight": 1
- }]
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectBody: `"code":0`,
- ExpectStatus: http.StatusOK,
- })
- })
- It("make sure the consumer is not created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/consumers/jack",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusNotFound,
- })
- })
- It("create consumer", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/consumers",
- Body: `{
- "username": "jack",
- "plugins": {
- "key-auth": {
- "key": "auth-one"
- },
- "basic-auth": {
- "username": "jack",
- "password": "123456"
- }
- },
- "desc": "test description"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- time.Sleep(base.SleepTime)
- exportStrAuth := `
- "components": {
- "securitySchemes": {
- "api_key": {
- "in": "header",
- "name": "X-XSRF-TOKEN",
- "type": "apiKey"
- },
- "basicAuth": {
- "in": "header",
- "name": "basicAuth",
- "type": "basicAuth"
- }
- }
- },
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0"`
- time.Sleep(base.SleepTime)
- exportStrAuth = replaceStr(exportStrAuth)
- It("verify route with correct basic-auth and key-auth token", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- Headers: map[string]string{"Authorization": "Basic amFjazoxMjM0NTYKIA==", "apikey": "auth-one"},
- ExpectStatus: http.StatusOK,
- ExpectBody: "hello world",
- Sleep: base.SleepTime,
- })
- })
- It("export route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrAuth,
- })
- })
- It("delete consumer", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/consumers/jack",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("verify route with the basic-auth and key-auth token from just deleted consumer", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- Headers: map[string]string{"Authorization": "Basic amFjazoxMjM0NTYKIA==", "apikey": "auth-one"},
- ExpectStatus: http.StatusUnauthorized,
- ExpectBody: `{"message":"Missing related consumer"}`,
- Sleep: base.SleepTime,
- })
- })
- It("delete route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("verify the deleted route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- })
- Context("test route export label", func() {
- // 10.Create a service with label data and a route with label data, and export the route.
- // Label is the original data of the route
- serviceStrS1 := `
- "name": "testservice",
- "desc": "testservice_desc",
- "labels": {
- "build": "10"
- },
- "enable_websocket": true`
- serviceStrS1 = replaceStr(serviceStrS1)
- exportStrR1 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/hello": {
- "get": {
- "operationId": "route_allGET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "summary": "所有",
- "x-apisix-enable_websocket": false,
- "x-apisix-id":"r1",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-vars":[
- [
- "arg_name",
- "==",
- "test"
- ]
- ]
- }`
- exportStrR1 = replaceStr(exportStrR1)
- It("create service", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- Body: `{
- "name": "testservice",
- "desc": "testservice_desc",
- "enable_websocket":true,
- "labels": {
- "build": "10"
- }
- }`,
- ExpectStatus: http.StatusOK,
- })
- })
- It("get the service s1", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectCode: http.StatusOK,
- ExpectBody: serviceStrS1,
- })
- })
- It("Create a service with label data and a route with label data", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "name": "route_all",
- "desc": "所有",
- "status": 1,
- "methods": ["GET"],
- "service_id": "s1",
- "labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "vars": [
- ["arg_name", "==", "test"]
- ],
- "uri": "/hello"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectCode: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route1", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectCode: http.StatusOK,
- ExpectBody: exportStrR1,
- })
- })
- It("delete the route1 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectCode: http.StatusOK,
- })
- })
- It("hit the route1 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- It("delete the service1", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- // 11.Create a service with label data and a route without label data, and export the route.
- // Label is the data of the service
- serviceStrS2 := `
- "name": "testservice",
- "desc": "testservice_desc",
- "labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "enable_websocket": true`
- serviceStrS2 = replaceStr(serviceStrS2)
- exportStrR2 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/hello": {
- "get": {
- "operationId": "route_allGET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "summary": "所有",
- "x-apisix-enable_websocket": false,
- "x-apisix-id":"r2",
- "x-apisix-labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- },
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-vars":[
- [
- "arg_name",
- "==",
- "test"
- ]
- ]
- }`
- exportStrR2 = replaceStr(exportStrR2)
- It("create service", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/services/s2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- Body: `{
- "name": "testservice",
- "desc": "testservice_desc",
- "enable_websocket":true,
- "labels": {
- "build": "16",
- "env": "production",
- "version": "v2"
- }
- }`,
- ExpectStatus: http.StatusOK,
- })
- })
- It("get the service s2", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/services/s2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectBody: serviceStrS2,
- ExpectStatus: http.StatusOK,
- })
- })
- It("Create a service with label data and a route without label data", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r2",
- Body: `{
- "name": "route_all",
- "desc": "所有",
- "status": 1,
- "methods": ["GET"],
- "service_id": "s2",
- "vars": [
- ["arg_name", "==", "test"]
- ],
- "uri": "/hello"
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route2", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectBody: exportStrR2,
- ExpectStatus: http.StatusOK,
- })
- })
- It("delete the route2 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route2 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
- Sleep: base.SleepTime,
- })
- })
- It("delete the service2", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/services/s2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- })
- Context("test route export request validation", func() {
- // 12.Test export route request_ validation data correctness
- exportStrR1 := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/test-test": {
- "get": {
- "operationId": "route_allGET",
- "parameters": [{
- "in": "header",
- "name": "test",
- "schema": {
- "type": "string"
- }
- }],
- "requestBody": {
- "content": {
- "*/*": {
- "schema": {
- "properties": {
- "boolean_payload": {
- "type": "boolean"
- },
- "required_payload": {
- "type": "string"
- }
- },
- "required": ["required_payload"],
- "type": "object"
- }
- }
- }
- },
- "responses": {
- "default": {
- "description": ""
- }
- },
- "security": [],
- "summary": "所有",
- "x-apisix-enable_websocket": false,
- "x-apisix-hosts": ["test.com"],
- "x-apisix-id":"r1",
- "x-apisix-priority": 0,
- "x-apisix-status": 1
- }
- }
- }`
- exportStrR1 = replaceStr(exportStrR1)
- It("Create a route containing request_ validation data", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "uris": ["/test-test"],
- "name": "route_all",
- "desc": "所有",
- "methods": ["GET"],
- "hosts": ["test.com"],
- "plugins": {
- "request-validation": {
- "body_schema": {
- "properties": {
- "boolean_payload": {
- "type": "boolean"
- },
- "required_payload": {
- "type": "string"
- }
- },
- "required": ["required_payload"],
- "type": "object"
- },
- "disable": false,
- "header_schema": {
- "properties": {
- "test": {
- "enum": "test-enum",
- "type": "string"
- }
- },
- "type": "string"
- }
- }
- },
- "status": 1
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("export route1", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrR1,
- })
- })
- It("delete the route1 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route1 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- })
- Context("test route export equal uri", func() {
- // 13.Add suffix when testing the same URI export
- exportStrAll := `
- "components": {},
- "info": {
- "title": "RoutesExport",
- "version": "3.0.0"
- },
- "openapi": "3.0.0",
- "paths": {
- "/test-test": {
- "get": {
- "operationId": "route_allGET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "summary": "所有",
- "x-apisix-enable_websocket": false,
- "x-apisix-hosts": ["test.com"],
- "x-apisix-id":"r1",
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- }
- },
- "/test-test-APISIX-REPEAT-URI-2": {
- "get": {
- "operationId": "route_all2GET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "summary": "所有1",
- "x-apisix-enable_websocket": false,
- "x-apisix-hosts": ["test.com"],
- "x-apisix-id":"r2",
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- }
- },
- "/test-test-APISIX-REPEAT-URI-3": {
- "get": {
- "operationId": "route_all3GET",
- "requestBody": {},
- "responses": {
- "default": {
- "description": ""
- }
- },
- "summary": "所有2",
- "x-apisix-enable_websocket": false,
- "x-apisix-hosts": ["test.com"],
- "x-apisix-id":"r3",
- "x-apisix-priority": 0,
- "x-apisix-status": 1,
- "x-apisix-upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1981": 1
- },
- "type": "roundrobin"
- }
- }
- }
- }`
- exportStrAll = replaceStr(exportStrAll)
- It("Create a route", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r1",
- Body: `{
- "uris": ["/test-test"],
- "name": "route_all",
- "desc": "所有",
- "methods": ["GET"],
- "hosts": ["test.com"],
- "status": 1,
- "upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("Create a route2", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r2",
- Body: `{
- "uris": ["/test-test"],
- "name": "route_all2",
- "desc": "所有1",
- "methods": ["GET"],
- "hosts": ["test.com"],
- "status": 1,
- "upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1980": 1
- },
- "type": "roundrobin"
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("Create a route3", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodPut,
- Path: "/apisix/admin/routes/r3",
- Body: `{
- "uris": ["/test-test"],
- "name": "route_all3",
- "desc": "所有2",
- "methods": ["GET"],
- "hosts": ["test.com"],
- "status": 1,
- "upstream": {
- "nodes": {
- "` + base.UpstreamIp + `:1981": 1
- },
- "type": "roundrobin"
- }
- }`,
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- Sleep: base.SleepTime,
- })
- })
- It("use the exportall interface to export all routes", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodGet,
- Path: "/apisix/admin/export/routes/r1,r2,r3",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- ExpectBody: exportStrAll,
- })
- })
- It("delete the route1 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r1",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route1 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- It("delete the route2 just created", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.ManagerApiExpect(),
- Method: http.MethodDelete,
- Path: "/apisix/admin/routes/r2",
- Headers: map[string]string{"Authorization": base.GetToken()},
- ExpectStatus: http.StatusOK,
- })
- })
- It("hit the route2 just deleted", func() {
- base.RunTestCase(base.HttpTestCase{
- Object: base.APISIXExpect(),
- Method: http.MethodGet,
- Path: "/hello",
- ExpectStatus: http.StatusNotFound,
- ExpectBody: `{"error_msg":"404 Route Not Found"}`,
- Sleep: base.SleepTime,
- })
- })
- })
- })
- func replaceStr(str string) string {
- str = strings.Replace(str, "\n", "", -1)
- str = strings.Replace(str, "\t", "", -1)
- str = strings.Replace(str, " ", "", -1)
- return str
- }
|