route_export_test.go 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package route_test
  18. import (
  19. "net/http"
  20. "strings"
  21. "time"
  22. . "github.com/onsi/ginkgo/v2"
  23. "github.com/stretchr/testify/assert"
  24. "github.com/apisix/manager-api/test/e2e/base"
  25. )
  26. var _ = Describe("Route", func() {
  27. Context("test route export data empty", func() {
  28. It("Export route when data is empty", func() {
  29. base.RunTestCase(base.HttpTestCase{
  30. Object: base.ManagerApiExpect(),
  31. Method: http.MethodGet,
  32. Path: "/apisix/admin/export/routes",
  33. Headers: map[string]string{"Authorization": base.GetToken()},
  34. ExpectStatus: http.StatusOK,
  35. ExpectBody: `{"code":10000,"message":"Route data is empty, cannot be exported","data":null`,
  36. })
  37. })
  38. })
  39. Context("test route export", func() {
  40. // 1.Export data as the route of URIs Hosts
  41. exportStrR1 := `
  42. "/hello_": {
  43. "get": {
  44. "operationId": "aaaaGET",
  45. "requestBody": {},
  46. "responses": {
  47. "default": {
  48. "description": ""
  49. }
  50. },
  51. "security": [],
  52. "x-apisix-enable_websocket": false,
  53. "x-apisix-hosts": ["foo.com", "*.bar.com"],
  54. "x-apisix-id":"r1",
  55. "x-apisix-labels": {
  56. "build": "16",
  57. "env": "production",
  58. "version": "v2"
  59. },
  60. "x-apisix-plugins": {
  61. "limit-count": {
  62. "count": 2,
  63. "key": "remote_addr",
  64. "policy": "local",
  65. "rejected_code": 503,
  66. "time_window": 60
  67. }
  68. },
  69. "x-apisix-priority": 0,
  70. "x-apisix-status": 1,
  71. "x-apisix-upstream": {
  72. "nodes": {
  73. "` + base.UpstreamIp + `:1980": 1
  74. },
  75. "type": "roundrobin"
  76. }
  77. },
  78. "post": {
  79. "operationId": "aaaaPOST",
  80. "requestBody": {},
  81. "responses": {
  82. "default": {
  83. "description": ""
  84. }
  85. },
  86. "security": [],
  87. "x-apisix-enable_websocket": false,
  88. "x-apisix-hosts": ["foo.com", "*.bar.com"],
  89. "x-apisix-id":"r1",
  90. "x-apisix-labels": {
  91. "build": "16",
  92. "env": "production",
  93. "version": "v2"
  94. },
  95. "x-apisix-plugins": {
  96. "limit-count": {
  97. "count": 2,
  98. "key": "remote_addr",
  99. "policy": "local",
  100. "rejected_code": 503,
  101. "time_window": 60
  102. }
  103. },
  104. "x-apisix-priority": 0,
  105. "x-apisix-status": 1,
  106. "x-apisix-upstream": {
  107. "nodes": {
  108. "` + base.UpstreamIp + `:1980": 1
  109. },
  110. "type": "roundrobin"
  111. }
  112. }
  113. }`
  114. exportStrR1 = replaceStr(exportStrR1)
  115. It("hit route that not exist", func() {
  116. base.RunTestCase(base.HttpTestCase{
  117. Object: base.APISIXExpect(),
  118. Method: http.MethodGet,
  119. Path: "/hello_",
  120. Headers: map[string]string{"Host": "foo.com"},
  121. ExpectStatus: http.StatusNotFound,
  122. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  123. })
  124. })
  125. It("create route with uris and hosts to test whether the uris parsing is correct", func() {
  126. base.RunTestCase(base.HttpTestCase{
  127. Object: base.ManagerApiExpect(),
  128. Method: http.MethodPut,
  129. Path: "/apisix/admin/routes/r1",
  130. Body: `{
  131. "name": "aaaa",
  132. "labels": {
  133. "build":"16",
  134. "env":"production",
  135. "version":"v2"
  136. },
  137. "plugins": {
  138. "limit-count": {
  139. "count": 2,
  140. "time_window": 60,
  141. "rejected_code": 503,
  142. "key": "remote_addr",
  143. "policy": "local"
  144. }
  145. },
  146. "status": 1,
  147. "uris": ["/hello_"],
  148. "hosts": ["foo.com", "*.bar.com"],
  149. "methods": ["GET", "POST"],
  150. "upstream": {
  151. "nodes": {
  152. "` + base.UpstreamIp + `:1980": 1
  153. },
  154. "type": "roundrobin"
  155. }
  156. }`,
  157. Headers: map[string]string{"Authorization": base.GetToken()},
  158. ExpectStatus: http.StatusOK,
  159. })
  160. })
  161. It("export route", func() {
  162. base.RunTestCase(base.HttpTestCase{
  163. Object: base.ManagerApiExpect(),
  164. Method: http.MethodGet,
  165. Path: "/apisix/admin/export/routes/r1",
  166. Headers: map[string]string{"Authorization": base.GetToken()},
  167. ExpectStatus: http.StatusOK,
  168. ExpectBody: `{"components":{},"info":{"title":"RoutesExport","version":"3.0.0"},"openapi":"3.0.0","paths":{` + exportStrR1 + "}}",
  169. })
  170. })
  171. // 2.Export data as the route of URI host
  172. exportStrR2 := `
  173. "/hello2": {
  174. "get": {
  175. "operationId": "aaaa2GET",
  176. "requestBody": {},
  177. "responses": {
  178. "default": {
  179. "description": ""
  180. }
  181. },
  182. "security": [],
  183. "x-apisix-enable_websocket": false,
  184. "x-apisix-host": "*.bar.com",
  185. "x-apisix-id":"r2",
  186. "x-apisix-labels": {
  187. "build": "16",
  188. "env": "production",
  189. "version": "v2"
  190. },
  191. "x-apisix-plugins": {
  192. "limit-count": {
  193. "count": 2,
  194. "key": "remote_addr",
  195. "policy": "local",
  196. "rejected_code": 503,
  197. "time_window": 60
  198. }
  199. },
  200. "x-apisix-priority": 0,
  201. "x-apisix-status": 1,
  202. "x-apisix-upstream": {
  203. "nodes": {
  204. "` + base.UpstreamIp + `:1980": 1
  205. },
  206. "type": "roundrobin"
  207. }
  208. },
  209. "post": {
  210. "operationId": "aaaa2POST",
  211. "requestBody": {},
  212. "responses": {
  213. "default": {
  214. "description": ""
  215. }
  216. },
  217. "security": [],
  218. "x-apisix-enable_websocket": false,
  219. "x-apisix-host": "*.bar.com",
  220. "x-apisix-id":"r2",
  221. "x-apisix-labels": {
  222. "build": "16",
  223. "env": "production",
  224. "version": "v2"
  225. },
  226. "x-apisix-plugins": {
  227. "limit-count": {
  228. "count": 2,
  229. "key": "remote_addr",
  230. "policy": "local",
  231. "rejected_code": 503,
  232. "time_window": 60
  233. }
  234. },
  235. "x-apisix-priority": 0,
  236. "x-apisix-status": 1,
  237. "x-apisix-upstream": {
  238. "nodes": {
  239. "` + base.UpstreamIp + `:1980": 1
  240. },
  241. "type": "roundrobin"
  242. }
  243. }
  244. }`
  245. exportStrR2 = replaceStr(exportStrR2)
  246. It("hit route2 that not exist", func() {
  247. base.RunTestCase(base.HttpTestCase{
  248. Object: base.APISIXExpect(),
  249. Method: http.MethodGet,
  250. Path: "/hello2",
  251. Headers: map[string]string{"Host": "bar.com"},
  252. ExpectStatus: http.StatusNotFound,
  253. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  254. })
  255. })
  256. It("create route2 with uri and host to test whether the uri parsing is correct", func() {
  257. base.RunTestCase(base.HttpTestCase{
  258. Object: base.ManagerApiExpect(),
  259. Method: http.MethodPut,
  260. Path: "/apisix/admin/routes/r2",
  261. Body: `{
  262. "name": "aaaa2",
  263. "labels": {
  264. "build":"16",
  265. "env":"production",
  266. "version":"v2"
  267. },
  268. "plugins": {
  269. "limit-count": {
  270. "count": 2,
  271. "time_window": 60,
  272. "rejected_code": 503,
  273. "key": "remote_addr",
  274. "policy": "local"
  275. }
  276. },
  277. "status": 1,
  278. "uri": "/hello2",
  279. "host": "*.bar.com",
  280. "methods": ["GET", "POST"],
  281. "upstream": {
  282. "nodes": {
  283. "` + base.UpstreamIp + `:1980": 1
  284. },
  285. "type": "roundrobin"
  286. }
  287. }`,
  288. Headers: map[string]string{"Authorization": base.GetToken()},
  289. ExpectStatus: http.StatusOK,
  290. })
  291. })
  292. It("export route2", func() {
  293. base.RunTestCase(base.HttpTestCase{
  294. Object: base.ManagerApiExpect(),
  295. Method: http.MethodGet,
  296. Path: "/apisix/admin/export/routes/r2",
  297. Headers: map[string]string{"Authorization": base.GetToken()},
  298. ExpectStatus: http.StatusOK,
  299. ExpectBody: `{"components":{},"info":{"title":"RoutesExport","version":"3.0.0"},"openapi":"3.0.0","paths":{` + exportStrR2 + "}}",
  300. })
  301. })
  302. It("export route and route2", func() {
  303. base.RunTestCase(base.HttpTestCase{
  304. Object: base.ManagerApiExpect(),
  305. Method: http.MethodGet,
  306. Path: "/apisix/admin/export/routes/r1,r2",
  307. Headers: map[string]string{"Authorization": base.GetToken()},
  308. ExpectStatus: http.StatusOK,
  309. ExpectBody: `{"components":{},"info":{"title":"RoutesExport","version":"3.0.0"},"openapi":"3.0.0","paths":{` + exportStrR2 + "," + exportStrR1 + "}}",
  310. })
  311. })
  312. It("use the exportall interface to export all routes", func() {
  313. base.RunTestCase(base.HttpTestCase{
  314. Object: base.ManagerApiExpect(),
  315. Method: http.MethodGet,
  316. Path: "/apisix/admin/export/routes",
  317. Headers: map[string]string{"Authorization": base.GetToken()},
  318. ExpectStatus: http.StatusOK,
  319. ExpectBody: `{"components":{},"info":{"title":"RoutesExport","version":"3.0.0"},"openapi":"3.0.0","paths":{` + exportStrR2 + "," + exportStrR1 + "}}",
  320. })
  321. })
  322. It("delete the route just created", func() {
  323. base.RunTestCase(base.HttpTestCase{
  324. Object: base.ManagerApiExpect(),
  325. Method: http.MethodDelete,
  326. Path: "/apisix/admin/routes/r1",
  327. Headers: map[string]string{"Authorization": base.GetToken()},
  328. ExpectStatus: http.StatusOK,
  329. })
  330. })
  331. It("hit the route just deleted", func() {
  332. base.RunTestCase(base.HttpTestCase{
  333. Object: base.APISIXExpect(),
  334. Method: http.MethodGet,
  335. Path: "/hello_",
  336. Headers: map[string]string{"Host": "bar.com"},
  337. ExpectStatus: http.StatusNotFound,
  338. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  339. Sleep: base.SleepTime,
  340. })
  341. })
  342. It("delete the route2 just created", func() {
  343. base.RunTestCase(base.HttpTestCase{
  344. Object: base.ManagerApiExpect(),
  345. Method: http.MethodDelete,
  346. Path: "/apisix/admin/routes/r2",
  347. Headers: map[string]string{"Authorization": base.GetToken()},
  348. ExpectStatus: http.StatusOK,
  349. })
  350. })
  351. It("hit the route2 just deleted", func() {
  352. base.RunTestCase(base.HttpTestCase{
  353. Object: base.APISIXExpect(),
  354. Method: http.MethodGet,
  355. Path: "/hello2",
  356. Headers: map[string]string{"Host": "bar.com"},
  357. ExpectStatus: http.StatusNotFound,
  358. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  359. Sleep: base.SleepTime,
  360. })
  361. })
  362. // 4.Create a service that contains complete data and use the service_ id create route
  363. serviceStrS1 := `
  364. "name": "testservice",
  365. "desc": "testservice_desc",
  366. "upstream": {
  367. "nodes": [{
  368. "host": "` + base.UpstreamIp + `",
  369. "port": 1980,
  370. "weight": 1
  371. }],
  372. "type": "roundrobin"
  373. },
  374. "plugins": {
  375. "limit-count": {
  376. "count": 100,
  377. "key": "remote_addr",
  378. "policy": "local",
  379. "rejected_code": 503,
  380. "time_window": 60
  381. }
  382. },
  383. "labels": {
  384. "build": "16",
  385. "env": "production",
  386. "version": "v2"
  387. },
  388. "enable_websocket": true
  389. `
  390. serviceStrS1 = replaceStr(serviceStrS1)
  391. exportStrR3 := `
  392. "components": {},
  393. "info": {
  394. "title": "RoutesExport",
  395. "version": "3.0.0"
  396. },
  397. "openapi": "3.0.0",
  398. "paths": {
  399. "/hello3": {
  400. "get": {
  401. "operationId": "route3GET",
  402. "requestBody": {},
  403. "responses": {
  404. "default": {
  405. "description": ""
  406. }
  407. },
  408. "x-apisix-enable_websocket": false,
  409. "x-apisix-id":"r3",
  410. "x-apisix-labels": {
  411. "build": "16",
  412. "env": "production",
  413. "version": "v2"
  414. },
  415. "x-apisix-plugins": {
  416. "limit-count": {
  417. "count": 100,
  418. "key": "remote_addr",
  419. "policy": "local",
  420. "rejected_code": 503,
  421. "time_window": 60
  422. }
  423. },
  424. "x-apisix-priority": 0,
  425. "x-apisix-status": 1,
  426. "x-apisix-upstream": {
  427. "nodes": [{
  428. "host": "` + base.UpstreamIp + `",
  429. "port": 1980,
  430. "weight": 1
  431. }],
  432. "type": "roundrobin"
  433. }
  434. }
  435. }
  436. }`
  437. exportStrR3 = replaceStr(exportStrR3)
  438. It("create service with all options", func() {
  439. base.RunTestCase(base.HttpTestCase{
  440. Object: base.ManagerApiExpect(),
  441. Method: http.MethodPut,
  442. Path: "/apisix/admin/services/s1",
  443. Headers: map[string]string{"Authorization": base.GetToken()},
  444. Body: `{
  445. "name": "testservice",
  446. "desc": "testservice_desc",
  447. "labels": {
  448. "build":"16",
  449. "env":"production",
  450. "version":"v2"
  451. },
  452. "enable_websocket":true,
  453. "plugins": {
  454. "limit-count": {
  455. "count": 100,
  456. "time_window": 60,
  457. "policy": "local",
  458. "rejected_code": 503,
  459. "key": "remote_addr"
  460. }
  461. },
  462. "upstream": {
  463. "type": "roundrobin",
  464. "create_time":1602883670,
  465. "update_time":1602893670,
  466. "nodes": [{
  467. "host": "` + base.UpstreamIp + `",
  468. "port": 1980,
  469. "weight": 1
  470. }]
  471. }
  472. }`,
  473. ExpectStatus: http.StatusOK,
  474. })
  475. })
  476. It("get the service s1", func() {
  477. base.RunTestCase(base.HttpTestCase{
  478. Object: base.ManagerApiExpect(),
  479. Method: http.MethodGet,
  480. Path: "/apisix/admin/services/s1",
  481. Headers: map[string]string{"Authorization": base.GetToken()},
  482. ExpectCode: http.StatusOK,
  483. ExpectBody: serviceStrS1,
  484. })
  485. })
  486. It("create route3 using the service id just created", func() {
  487. base.RunTestCase(base.HttpTestCase{
  488. Object: base.ManagerApiExpect(),
  489. Method: http.MethodPut,
  490. Path: "/apisix/admin/routes/r3",
  491. Body: `{
  492. "name": "route3",
  493. "methods": ["GET"],
  494. "uri": "/hello3",
  495. "service_id": "s1"
  496. }`,
  497. Headers: map[string]string{"Authorization": base.GetToken()},
  498. ExpectCode: http.StatusOK,
  499. Sleep: base.SleepTime,
  500. })
  501. })
  502. It("export route3", func() {
  503. base.RunTestCase(base.HttpTestCase{
  504. Object: base.ManagerApiExpect(),
  505. Method: http.MethodGet,
  506. Path: "/apisix/admin/export/routes/r3",
  507. Headers: map[string]string{"Authorization": base.GetToken()},
  508. ExpectCode: http.StatusOK,
  509. ExpectBody: exportStrR3,
  510. })
  511. })
  512. It("delete the route3 just created", func() {
  513. base.RunTestCase(base.HttpTestCase{
  514. Object: base.ManagerApiExpect(),
  515. Method: http.MethodDelete,
  516. Path: "/apisix/admin/routes/r3",
  517. Headers: map[string]string{"Authorization": base.GetToken()},
  518. ExpectCode: http.StatusOK,
  519. })
  520. })
  521. It("hit the route3 just deleted", func() {
  522. base.RunTestCase(base.HttpTestCase{
  523. Object: base.APISIXExpect(),
  524. Method: http.MethodGet,
  525. Path: "/hello3",
  526. ExpectStatus: http.StatusNotFound,
  527. Sleep: base.SleepTime,
  528. })
  529. })
  530. It("delete the service1", func() {
  531. base.RunTestCase(base.HttpTestCase{
  532. Object: base.ManagerApiExpect(),
  533. Method: http.MethodDelete,
  534. Path: "/apisix/admin/services/s1",
  535. Headers: map[string]string{"Authorization": base.GetToken()},
  536. ExpectStatus: http.StatusOK,
  537. Sleep: base.SleepTime,
  538. })
  539. })
  540. // 5.Create a service containing plugin and a route containing plugin to test the fusion of exported data
  541. // This test case tests the creation of a complete service.
  542. // And create a complete route, export rules as route upstream data for high priority, direct use.
  543. // However, if the data in the service plugins does not exist in the route, it will be fused and exported.
  544. // If it exists, the data in route will be used first
  545. serviceStrS2 := `
  546. "name": "testservice",
  547. "desc": "testservice_desc",
  548. "upstream": {
  549. "nodes": [{
  550. "host": "` + base.UpstreamIp + `",
  551. "port": 1980,
  552. "weight": 1
  553. }],
  554. "type": "roundrobin"
  555. },
  556. "plugins": {
  557. "limit-count": {
  558. "count": 100,
  559. "key": "remote_addr",
  560. "policy": "local",
  561. "rejected_code": 503,
  562. "time_window": 60
  563. }
  564. },
  565. "labels": {
  566. "build": "16",
  567. "env": "production",
  568. "version": "v2"
  569. },
  570. "enable_websocket": true`
  571. serviceStrS2 = replaceStr(serviceStrS2)
  572. exportStrR4 := `
  573. "components": {},
  574. "info": {
  575. "title": "RoutesExport",
  576. "version": "3.0.0"
  577. },
  578. "openapi": "3.0.0",
  579. "paths": {
  580. "/hello": {
  581. "get": {
  582. "operationId": "route4GET",
  583. "requestBody": {},
  584. "responses": {
  585. "default": {
  586. "description": ""
  587. }
  588. },
  589. "security": [],
  590. "x-apisix-enable_websocket": false,
  591. "x-apisix-id":"r4",
  592. "x-apisix-labels": {
  593. "build": "16",
  594. "env": "production",
  595. "version": "v2"
  596. },
  597. "x-apisix-plugins": {
  598. "limit-count": {
  599. "count": 100,
  600. "key": "remote_addr",
  601. "policy": "local",
  602. "rejected_code": 503,
  603. "time_window": 60
  604. },
  605. "prometheus": {
  606. "disable": false
  607. }
  608. },
  609. "x-apisix-priority": 0,
  610. "x-apisix-status": 1,
  611. "x-apisix-upstream": {
  612. "nodes": [{
  613. "host": "` + base.UpstreamIp + `",
  614. "port": 1980,
  615. "weight": 1
  616. }],
  617. "type": "roundrobin"
  618. }
  619. }
  620. }
  621. }`
  622. exportStrR4 = replaceStr(exportStrR4)
  623. It("create service with all options", func() {
  624. base.RunTestCase(base.HttpTestCase{
  625. Object: base.ManagerApiExpect(),
  626. Method: http.MethodPut,
  627. Path: "/apisix/admin/services/s2",
  628. Headers: map[string]string{"Authorization": base.GetToken()},
  629. Body: `{
  630. "name": "testservice",
  631. "desc": "testservice_desc",
  632. "labels": {
  633. "build":"16",
  634. "env":"production",
  635. "version":"v2"
  636. },
  637. "enable_websocket":true,
  638. "plugins": {
  639. "limit-count": {
  640. "count": 100,
  641. "time_window": 60,
  642. "rejected_code": 503,
  643. "key": "remote_addr",
  644. "policy": "local"
  645. }
  646. },
  647. "upstream": {
  648. "type": "roundrobin",
  649. "create_time":1602883670,
  650. "update_time":1602893670,
  651. "nodes": [{
  652. "host": "` + base.UpstreamIp + `",
  653. "port": 1980,
  654. "weight": 1
  655. }]
  656. }
  657. }`,
  658. ExpectStatus: http.StatusOK,
  659. })
  660. })
  661. It("get the service s2", func() {
  662. base.RunTestCase(base.HttpTestCase{
  663. Object: base.ManagerApiExpect(),
  664. Method: http.MethodGet,
  665. Path: "/apisix/admin/services/s2",
  666. Headers: map[string]string{"Authorization": base.GetToken()},
  667. ExpectStatus: http.StatusOK,
  668. ExpectBody: serviceStrS2,
  669. })
  670. })
  671. It("Create Route4 and test the priority merging function of upstream, label and plugin when both service and route are included", func() {
  672. base.RunTestCase(base.HttpTestCase{
  673. Object: base.ManagerApiExpect(),
  674. Method: http.MethodPut,
  675. Path: "/apisix/admin/routes/r4",
  676. Body: `{
  677. "name": "route4",
  678. "methods": ["GET"],
  679. "uri": "/hello",
  680. "service_id": "s2",
  681. "enable_websocket":false,
  682. "plugins": {
  683. "prometheus": {
  684. "disable": false
  685. }
  686. },
  687. "upstream": {
  688. "type": "roundrobin",
  689. "nodes": [{
  690. "host": "` + base.UpstreamIp + `",
  691. "port": 1980,
  692. "weight": 1
  693. }]
  694. }
  695. }`,
  696. Headers: map[string]string{"Authorization": base.GetToken()},
  697. ExpectStatus: http.StatusOK,
  698. Sleep: base.SleepTime,
  699. })
  700. })
  701. It("export route4", func() {
  702. base.RunTestCase(base.HttpTestCase{
  703. Object: base.ManagerApiExpect(),
  704. Method: http.MethodGet,
  705. Path: "/apisix/admin/export/routes/r4",
  706. Headers: map[string]string{"Authorization": base.GetToken()},
  707. ExpectStatus: http.StatusOK,
  708. ExpectBody: exportStrR4,
  709. })
  710. })
  711. It("delete the route4 just created", func() {
  712. base.RunTestCase(base.HttpTestCase{
  713. Object: base.ManagerApiExpect(),
  714. Method: http.MethodDelete,
  715. Path: "/apisix/admin/routes/r4",
  716. Headers: map[string]string{"Authorization": base.GetToken()},
  717. ExpectStatus: http.StatusOK,
  718. })
  719. })
  720. It("hit the route4 just deleted", func() {
  721. base.RunTestCase(base.HttpTestCase{
  722. Object: base.APISIXExpect(),
  723. Method: http.MethodGet,
  724. Path: "/hello",
  725. ExpectStatus: http.StatusNotFound,
  726. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  727. })
  728. })
  729. It("delete the service2", func() {
  730. base.RunTestCase(base.HttpTestCase{
  731. Object: base.ManagerApiExpect(),
  732. Method: http.MethodDelete,
  733. Path: "/apisix/admin/services/s2",
  734. Headers: map[string]string{"Authorization": base.GetToken()},
  735. ExpectStatus: http.StatusOK,
  736. Sleep: base.SleepTime,
  737. })
  738. })
  739. // 6.Create a service according to the upstream ID and a route according to the service ID.
  740. // The test export also contains the upstream.
  741. // Use the upstream of route.
  742. serviceStrS3 := `
  743. "name": "testservice",
  744. "desc": "testservice_desc",
  745. "upstream_id": "1",
  746. "plugins": {
  747. "limit-count": {
  748. "count": 100,
  749. "key": "remote_addr",
  750. "policy": "local",
  751. "rejected_code": 503,
  752. "time_window": 60
  753. }
  754. },
  755. "labels": {
  756. "build": "16",
  757. "env": "production",
  758. "version": "v2"
  759. },
  760. "enable_websocket": true`
  761. serviceStrS3 = replaceStr(serviceStrS3)
  762. exportStrR5 := `
  763. "components": {},
  764. "info": {
  765. "title": "RoutesExport",
  766. "version": "3.0.0"
  767. },
  768. "openapi": "3.0.0",
  769. "paths": {
  770. "/hello5": {
  771. "get": {
  772. "operationId": "route5GET",
  773. "requestBody": {},
  774. "responses": {
  775. "default": {
  776. "description": ""
  777. }
  778. },
  779. "security": [],
  780. "x-apisix-enable_websocket": false,
  781. "x-apisix-id":"r5",
  782. "x-apisix-labels": {
  783. "build": "16",
  784. "env": "production",
  785. "version": "v2"
  786. },
  787. "x-apisix-plugins": {
  788. "limit-count": {
  789. "count": 100,
  790. "key": "remote_addr",
  791. "policy": "local",
  792. "rejected_code": 503,
  793. "time_window": 60
  794. },
  795. "prometheus": {
  796. "disable": false
  797. }
  798. },
  799. "x-apisix-priority": 0,
  800. "x-apisix-status": 1,
  801. "x-apisix-upstream": {
  802. "nodes": [{
  803. "host": "` + base.UpstreamIp + `",
  804. "port": 1981,
  805. "weight": 1
  806. }],
  807. "type": "roundrobin"
  808. }
  809. }
  810. }
  811. }`
  812. exportStrR5 = replaceStr(exportStrR5)
  813. It("create upstream", func() {
  814. base.RunTestCase(base.HttpTestCase{
  815. Object: base.ManagerApiExpect(),
  816. Method: http.MethodPut,
  817. Path: "/apisix/admin/upstreams/1",
  818. Headers: map[string]string{"Authorization": base.GetToken()},
  819. Body: `{
  820. "nodes": [
  821. {
  822. "host": "` + base.UpstreamIp + `",
  823. "port": 1980,
  824. "weight": 1
  825. }
  826. ],
  827. "type": "roundrobin"
  828. }`,
  829. ExpectStatus: http.StatusOK,
  830. })
  831. })
  832. It("create service with upstream id", func() {
  833. base.RunTestCase(base.HttpTestCase{
  834. Object: base.ManagerApiExpect(),
  835. Method: http.MethodPut,
  836. Path: "/apisix/admin/services/s3",
  837. Headers: map[string]string{"Authorization": base.GetToken()},
  838. Body: `{
  839. "name": "testservice",
  840. "desc": "testservice_desc",
  841. "labels": {
  842. "build":"16",
  843. "env":"production",
  844. "version":"v2"
  845. },
  846. "enable_websocket":true,
  847. "plugins": {
  848. "limit-count": {
  849. "count": 100,
  850. "time_window": 60,
  851. "rejected_code": 503,
  852. "key": "remote_addr",
  853. "policy": "local"
  854. }
  855. },
  856. "upstream_id": "1"
  857. }`,
  858. ExpectStatus: http.StatusOK,
  859. })
  860. })
  861. It("get the service s3", func() {
  862. base.RunTestCase(base.HttpTestCase{
  863. Object: base.ManagerApiExpect(),
  864. Method: http.MethodGet,
  865. Path: "/apisix/admin/services/s3",
  866. Headers: map[string]string{"Authorization": base.GetToken()},
  867. ExpectStatus: http.StatusOK,
  868. ExpectBody: serviceStrS3,
  869. })
  870. })
  871. It("Create a route5 with the id of the service3 created with upstream id", func() {
  872. base.RunTestCase(base.HttpTestCase{
  873. Object: base.ManagerApiExpect(),
  874. Method: http.MethodPut,
  875. Path: "/apisix/admin/routes/r5",
  876. Body: `{
  877. "name": "route5",
  878. "methods": ["GET"],
  879. "uri": "/hello5",
  880. "service_id": "s3",
  881. "enable_websocket":false,
  882. "plugins": {
  883. "prometheus": {
  884. "disable": false
  885. }
  886. },
  887. "upstream": {
  888. "type": "roundrobin",
  889. "nodes": [{
  890. "host": "` + base.UpstreamIp + `",
  891. "port": 1981,
  892. "weight": 1
  893. }]
  894. }
  895. }`,
  896. Headers: map[string]string{"Authorization": base.GetToken()},
  897. ExpectStatus: http.StatusOK,
  898. Sleep: base.SleepTime,
  899. })
  900. })
  901. It("export route5", func() {
  902. base.RunTestCase(base.HttpTestCase{
  903. Object: base.ManagerApiExpect(),
  904. Method: http.MethodGet,
  905. Path: "/apisix/admin/export/routes/r5",
  906. Headers: map[string]string{"Authorization": base.GetToken()},
  907. ExpectStatus: http.StatusOK,
  908. ExpectBody: exportStrR5,
  909. })
  910. })
  911. It("delete the route5 just created", func() {
  912. base.RunTestCase(base.HttpTestCase{
  913. Object: base.ManagerApiExpect(),
  914. Method: http.MethodDelete,
  915. Path: "/apisix/admin/routes/r5",
  916. Headers: map[string]string{"Authorization": base.GetToken()},
  917. ExpectStatus: http.StatusOK,
  918. })
  919. })
  920. It("hit the route5 just deleted", func() {
  921. base.RunTestCase(base.HttpTestCase{
  922. Object: base.APISIXExpect(),
  923. Method: http.MethodGet,
  924. Path: "/hello5",
  925. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  926. Sleep: base.SleepTime,
  927. })
  928. })
  929. It("delete the service3", func() {
  930. base.RunTestCase(base.HttpTestCase{
  931. Object: base.ManagerApiExpect(),
  932. Method: http.MethodDelete,
  933. Path: "/apisix/admin/services/s3",
  934. Headers: map[string]string{"Authorization": base.GetToken()},
  935. ExpectStatus: http.StatusOK,
  936. Sleep: base.SleepTime,
  937. })
  938. })
  939. It("remove upstream", func() {
  940. base.RunTestCase(base.HttpTestCase{
  941. Object: base.ManagerApiExpect(),
  942. Method: http.MethodDelete,
  943. Path: "/apisix/admin/upstreams/1",
  944. Headers: map[string]string{"Authorization": base.GetToken()},
  945. ExpectStatus: http.StatusOK,
  946. })
  947. })
  948. // 8.Create and export route according to upstream ID
  949. exportStrR8 := `
  950. "components": {},
  951. "info": {
  952. "title": "RoutesExport",
  953. "version": "3.0.0"
  954. },
  955. "openapi": "3.0.0",
  956. "paths": {
  957. "/hello": {
  958. "get": {
  959. "operationId": "route8GET",
  960. "requestBody": {},
  961. "responses": {
  962. "default": {
  963. "description": ""
  964. }
  965. },
  966. "security": [],
  967. "x-apisix-enable_websocket": false,
  968. "x-apisix-id":"r8",
  969. "x-apisix-plugins": {
  970. "prometheus": {
  971. "disable": false
  972. }
  973. },
  974. "x-apisix-priority": 0,
  975. "x-apisix-status": 1,
  976. "x-apisix-upstream": {
  977. "id": "3"
  978. `
  979. exportStrR8 = replaceStr(exportStrR8)
  980. It("create upstream3", func() {
  981. base.RunTestCase(base.HttpTestCase{
  982. Object: base.ManagerApiExpect(),
  983. Method: http.MethodPut,
  984. Path: "/apisix/admin/upstreams/3",
  985. Body: `{
  986. "nodes": [
  987. {
  988. "host": "` + base.UpstreamIp + `",
  989. "port": 1980,
  990. "weight": 1
  991. }
  992. ],
  993. "type": "roundrobin"
  994. }`,
  995. Headers: map[string]string{"Authorization": base.GetToken()},
  996. ExpectStatus: http.StatusOK,
  997. })
  998. })
  999. It("Create a route8 using upstream id", func() {
  1000. base.RunTestCase(base.HttpTestCase{
  1001. Object: base.ManagerApiExpect(),
  1002. Method: http.MethodPut,
  1003. Path: "/apisix/admin/routes/r8",
  1004. Body: `{
  1005. "name": "route8",
  1006. "methods": ["GET"],
  1007. "uri": "/hello",
  1008. "enable_websocket":false,
  1009. "plugins": {
  1010. "prometheus": {
  1011. "disable": false
  1012. }
  1013. },
  1014. "upstream_id": "3"
  1015. }`,
  1016. Headers: map[string]string{"Authorization": base.GetToken()},
  1017. ExpectStatus: http.StatusOK,
  1018. Sleep: base.SleepTime,
  1019. })
  1020. })
  1021. It("export route8", func() {
  1022. base.RunTestCase(base.HttpTestCase{
  1023. Object: base.ManagerApiExpect(),
  1024. Method: http.MethodGet,
  1025. Path: "/apisix/admin/export/routes/r8",
  1026. Headers: map[string]string{"Authorization": base.GetToken()},
  1027. ExpectStatus: http.StatusOK,
  1028. ExpectBody: exportStrR8,
  1029. })
  1030. })
  1031. It("delete the route8 just created", func() {
  1032. base.RunTestCase(base.HttpTestCase{
  1033. Object: base.ManagerApiExpect(),
  1034. Method: http.MethodDelete,
  1035. Path: "/apisix/admin/routes/r8",
  1036. Headers: map[string]string{"Authorization": base.GetToken()},
  1037. ExpectStatus: http.StatusOK,
  1038. })
  1039. })
  1040. It("hit the route8 just deleted", func() {
  1041. base.RunTestCase(base.HttpTestCase{
  1042. Object: base.APISIXExpect(),
  1043. Method: http.MethodGet,
  1044. Path: "/hello",
  1045. ExpectStatus: http.StatusNotFound,
  1046. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  1047. Sleep: base.SleepTime,
  1048. })
  1049. })
  1050. It("remove upstream3", func() {
  1051. base.RunTestCase(base.HttpTestCase{
  1052. Object: base.ManagerApiExpect(),
  1053. Method: http.MethodDelete,
  1054. Path: "/apisix/admin/upstreams/3",
  1055. Headers: map[string]string{"Authorization": base.GetToken()},
  1056. ExpectStatus: http.StatusOK,
  1057. })
  1058. })
  1059. // 9.Create service according to upstream1 ID
  1060. // Create route according to upstream2 ID and service ID
  1061. // Export route
  1062. serviceStrS4 := `
  1063. "name": "testservice",
  1064. "desc": "testservice_desc",
  1065. "upstream_id": "4",
  1066. "enable_websocket": true`
  1067. serviceStrS4 = replaceStr(serviceStrS4)
  1068. exportStrR9 := `
  1069. "components": {},
  1070. "info": {
  1071. "title": "RoutesExport",
  1072. "version": "3.0.0"
  1073. },
  1074. "openapi": "3.0.0",
  1075. "paths": {
  1076. "/hello": {
  1077. "get": {
  1078. "operationId": "route_allGET",
  1079. "requestBody": {},
  1080. "responses": {
  1081. "default": {
  1082. "description": ""
  1083. }
  1084. },
  1085. "security": [],
  1086. "summary": "所有",
  1087. "x-apisix-enable_websocket": false,
  1088. "x-apisix-id":"r9",
  1089. "x-apisix-labels": {
  1090. "API_VERSION": "v1",
  1091. "test": "1"
  1092. },
  1093. "x-apisix-plugins": {
  1094. "prometheus": {
  1095. "disable": false
  1096. }
  1097. },
  1098. "x-apisix-priority": 0,
  1099. "x-apisix-status": 1,
  1100. "x-apisix-upstream": {
  1101. "id": "5"
  1102. `
  1103. exportStrR9 = replaceStr(exportStrR9)
  1104. It("create upstream4", func() {
  1105. base.RunTestCase(base.HttpTestCase{
  1106. Object: base.ManagerApiExpect(),
  1107. Method: http.MethodPut,
  1108. Path: "/apisix/admin/upstreams/4",
  1109. Body: `{
  1110. "nodes": [
  1111. {
  1112. "host": "` + base.UpstreamIp + `",
  1113. "port": 1980,
  1114. "weight": 1
  1115. }
  1116. ],
  1117. "type": "roundrobin"
  1118. }`,
  1119. Headers: map[string]string{"Authorization": base.GetToken()},
  1120. ExpectStatus: http.StatusOK,
  1121. })
  1122. })
  1123. It("create upstream5", func() {
  1124. base.RunTestCase(base.HttpTestCase{
  1125. Object: base.ManagerApiExpect(),
  1126. Method: http.MethodPut,
  1127. Path: "/apisix/admin/upstreams/5",
  1128. Body: `{
  1129. "name": "upstream5",
  1130. "nodes": [
  1131. {
  1132. "host": "` + base.UpstreamIp + `",
  1133. "port": 1981,
  1134. "weight": 1
  1135. }
  1136. ],
  1137. "type": "roundrobin"
  1138. }`,
  1139. Headers: map[string]string{"Authorization": base.GetToken()},
  1140. ExpectStatus: http.StatusOK,
  1141. })
  1142. })
  1143. It("create service", func() {
  1144. base.RunTestCase(base.HttpTestCase{
  1145. Object: base.ManagerApiExpect(),
  1146. Method: http.MethodPut,
  1147. Path: "/apisix/admin/services/s4",
  1148. Body: `{
  1149. "name": "testservice",
  1150. "desc": "testservice_desc",
  1151. "enable_websocket":true,
  1152. "upstream_id": "4"
  1153. }`,
  1154. Headers: map[string]string{"Authorization": base.GetToken()},
  1155. ExpectStatus: http.StatusOK,
  1156. })
  1157. })
  1158. It("get the service s4", func() {
  1159. base.RunTestCase(base.HttpTestCase{
  1160. Object: base.ManagerApiExpect(),
  1161. Method: http.MethodGet,
  1162. Path: "/apisix/admin/services/s4",
  1163. Headers: map[string]string{"Authorization": base.GetToken()},
  1164. ExpectStatus: http.StatusOK,
  1165. ExpectBody: serviceStrS4,
  1166. })
  1167. })
  1168. It("Create a route9 using upstream id and service id", func() {
  1169. base.RunTestCase(base.HttpTestCase{
  1170. Object: base.ManagerApiExpect(),
  1171. Method: http.MethodPut,
  1172. Path: "/apisix/admin/routes/r9",
  1173. Body: `{
  1174. "name": "route_all",
  1175. "desc": "所有",
  1176. "status": 1,
  1177. "methods": ["GET"],
  1178. "priority": 0,
  1179. "service_id": "s4",
  1180. "labels": {
  1181. "test": "1",
  1182. "API_VERSION": "v1"
  1183. },
  1184. "vars": [
  1185. ["arg_name", "==", "test"]
  1186. ],
  1187. "uri": "/hello",
  1188. "enable_websocket":false,
  1189. "plugins": {
  1190. "prometheus": {
  1191. "disable": false
  1192. }
  1193. },
  1194. "upstream_id": "5"
  1195. }`,
  1196. Headers: map[string]string{"Authorization": base.GetToken()},
  1197. ExpectStatus: http.StatusOK,
  1198. Sleep: base.SleepTime,
  1199. })
  1200. })
  1201. It("export route9", func() {
  1202. base.RunTestCase(base.HttpTestCase{
  1203. Object: base.ManagerApiExpect(),
  1204. Method: http.MethodGet,
  1205. Path: "/apisix/admin/export/routes/r9",
  1206. Headers: map[string]string{"Authorization": base.GetToken()},
  1207. ExpectStatus: http.StatusOK,
  1208. ExpectBody: exportStrR9,
  1209. })
  1210. })
  1211. It("delete the route9 just created", func() {
  1212. base.RunTestCase(base.HttpTestCase{
  1213. Object: base.ManagerApiExpect(),
  1214. Method: http.MethodDelete,
  1215. Path: "/apisix/admin/routes/r9",
  1216. Headers: map[string]string{"Authorization": base.GetToken()},
  1217. ExpectStatus: http.StatusOK,
  1218. })
  1219. })
  1220. It("hit the route9 just deleted", func() {
  1221. base.RunTestCase(base.HttpTestCase{
  1222. Object: base.APISIXExpect(),
  1223. Method: http.MethodGet,
  1224. Path: "/hello",
  1225. ExpectStatus: http.StatusNotFound,
  1226. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  1227. Sleep: base.SleepTime,
  1228. })
  1229. })
  1230. It("delete the service4", func() {
  1231. base.RunTestCase(base.HttpTestCase{
  1232. Object: base.ManagerApiExpect(),
  1233. Method: http.MethodDelete,
  1234. Path: "/apisix/admin/services/s4",
  1235. Headers: map[string]string{"Authorization": base.GetToken()},
  1236. ExpectStatus: http.StatusOK,
  1237. Sleep: base.SleepTime,
  1238. })
  1239. })
  1240. It("remove upstream4", func() {
  1241. base.RunTestCase(base.HttpTestCase{
  1242. Object: base.ManagerApiExpect(),
  1243. Method: http.MethodDelete,
  1244. Path: "/apisix/admin/upstreams/4",
  1245. Headers: map[string]string{"Authorization": base.GetToken()},
  1246. ExpectStatus: http.StatusOK,
  1247. })
  1248. })
  1249. It("remove upstream5", func() {
  1250. base.RunTestCase(base.HttpTestCase{
  1251. Object: base.ManagerApiExpect(),
  1252. Method: http.MethodDelete,
  1253. Path: "/apisix/admin/upstreams/5",
  1254. Headers: map[string]string{"Authorization": base.GetToken()},
  1255. ExpectStatus: http.StatusOK,
  1256. })
  1257. })
  1258. // 10.Creating route10 using service ID does not contain upstream data
  1259. serviceStrS5 := `
  1260. "name": "testservice",
  1261. "desc": "testservice_desc",
  1262. "upstream_id": "6",
  1263. "enable_websocket": true`
  1264. serviceStrS5 = replaceStr(serviceStrS5)
  1265. exportStrR10 := `
  1266. "components": {},
  1267. "info": {
  1268. "title": "RoutesExport",
  1269. "version": "3.0.0"
  1270. },
  1271. "openapi": "3.0.0",
  1272. "paths": {
  1273. "/hello": {
  1274. "get": {
  1275. "operationId": "route_allGET",
  1276. "requestBody": {},
  1277. "responses": {
  1278. "default": {
  1279. "description": ""
  1280. }
  1281. },
  1282. "security": [],
  1283. "summary": "所有",
  1284. "x-apisix-enable_websocket": false,
  1285. "x-apisix-id":"r10",
  1286. "x-apisix-labels": {
  1287. "API_VERSION": "v1",
  1288. "test": "1"
  1289. },
  1290. "x-apisix-plugins": {
  1291. "prometheus": {
  1292. "disable": false
  1293. }
  1294. },
  1295. "x-apisix-priority": 0,
  1296. "x-apisix-status": 1,
  1297. "x-apisix-upstream": {
  1298. "id": "6"
  1299. `
  1300. exportStrR10 = replaceStr(exportStrR10)
  1301. It("create upstream6", func() {
  1302. base.RunTestCase(base.HttpTestCase{
  1303. Object: base.ManagerApiExpect(),
  1304. Method: http.MethodPut,
  1305. Path: "/apisix/admin/upstreams/6",
  1306. Body: `{
  1307. "nodes": [
  1308. {
  1309. "host": "` + base.UpstreamIp + `",
  1310. "port": 1980,
  1311. "weight": 1
  1312. }
  1313. ],
  1314. "type": "roundrobin"
  1315. }`,
  1316. Headers: map[string]string{"Authorization": base.GetToken()},
  1317. ExpectStatus: http.StatusOK,
  1318. })
  1319. })
  1320. It("create service", func() {
  1321. base.RunTestCase(base.HttpTestCase{
  1322. Object: base.ManagerApiExpect(),
  1323. Method: http.MethodPut,
  1324. Path: "/apisix/admin/services/s5",
  1325. Body: `{
  1326. "name": "testservice",
  1327. "desc": "testservice_desc",
  1328. "enable_websocket":true,
  1329. "upstream_id": "6"
  1330. }`,
  1331. Headers: map[string]string{"Authorization": base.GetToken()},
  1332. ExpectStatus: http.StatusOK,
  1333. })
  1334. })
  1335. It("get the service s5", func() {
  1336. base.RunTestCase(base.HttpTestCase{
  1337. Object: base.ManagerApiExpect(),
  1338. Method: http.MethodGet,
  1339. Path: "/apisix/admin/services/s5",
  1340. Headers: map[string]string{"Authorization": base.GetToken()},
  1341. ExpectStatus: http.StatusOK,
  1342. ExpectBody: serviceStrS5,
  1343. })
  1344. })
  1345. It("Creating route10 using service ID does not contain upstream data", func() {
  1346. base.RunTestCase(base.HttpTestCase{
  1347. Object: base.ManagerApiExpect(),
  1348. Method: http.MethodPut,
  1349. Path: "/apisix/admin/routes/r10",
  1350. Body: `{
  1351. "name": "route_all",
  1352. "desc": "所有",
  1353. "status": 1,
  1354. "methods": ["GET"],
  1355. "priority": 0,
  1356. "service_id": "s5",
  1357. "labels": {
  1358. "test": "1",
  1359. "API_VERSION": "v1"
  1360. },
  1361. "vars": [
  1362. ["arg_name", "==", "test"]
  1363. ],
  1364. "uri": "/hello",
  1365. "enable_websocket":false,
  1366. "plugins": {
  1367. "prometheus": {
  1368. "disable": false
  1369. }
  1370. }
  1371. }`,
  1372. Headers: map[string]string{"Authorization": base.GetToken()},
  1373. ExpectStatus: http.StatusOK,
  1374. Sleep: base.SleepTime,
  1375. })
  1376. })
  1377. It("export route10", func() {
  1378. base.RunTestCase(base.HttpTestCase{
  1379. Object: base.ManagerApiExpect(),
  1380. Method: http.MethodGet,
  1381. Path: "/apisix/admin/export/routes/r10",
  1382. Headers: map[string]string{"Authorization": base.GetToken()},
  1383. ExpectStatus: http.StatusOK,
  1384. ExpectBody: exportStrR10,
  1385. })
  1386. })
  1387. It("delete the route10 just created", func() {
  1388. base.RunTestCase(base.HttpTestCase{
  1389. Object: base.ManagerApiExpect(),
  1390. Method: http.MethodDelete,
  1391. Path: "/apisix/admin/routes/r10",
  1392. Headers: map[string]string{"Authorization": base.GetToken()},
  1393. ExpectStatus: http.StatusOK,
  1394. })
  1395. })
  1396. It("hit the route10 just deleted", func() {
  1397. base.RunTestCase(base.HttpTestCase{
  1398. Object: base.APISIXExpect(),
  1399. Method: http.MethodGet,
  1400. Path: "/hello",
  1401. ExpectStatus: http.StatusNotFound,
  1402. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  1403. Sleep: base.SleepTime,
  1404. })
  1405. })
  1406. It("delete the service5", func() {
  1407. base.RunTestCase(base.HttpTestCase{
  1408. Object: base.ManagerApiExpect(),
  1409. Method: http.MethodDelete,
  1410. Path: "/apisix/admin/services/s5",
  1411. Headers: map[string]string{"Authorization": base.GetToken()},
  1412. ExpectStatus: http.StatusOK,
  1413. Sleep: base.SleepTime,
  1414. })
  1415. })
  1416. It("remove upstream6", func() {
  1417. base.RunTestCase(base.HttpTestCase{
  1418. Object: base.ManagerApiExpect(),
  1419. Method: http.MethodDelete,
  1420. Path: "/apisix/admin/upstreams/6",
  1421. Headers: map[string]string{"Authorization": base.GetToken()},
  1422. ExpectStatus: http.StatusOK,
  1423. })
  1424. })
  1425. })
  1426. Context("test export route with jwt plugin", func() {
  1427. It("make sure the route is not created", func() {
  1428. base.RunTestCase(base.HttpTestCase{
  1429. Object: base.APISIXExpect(),
  1430. Method: http.MethodGet,
  1431. Path: "/hello",
  1432. Headers: map[string]string{"Authorization": base.GetToken()},
  1433. ExpectStatus: http.StatusNotFound,
  1434. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  1435. })
  1436. })
  1437. It("create route", func() {
  1438. base.RunTestCase(base.HttpTestCase{
  1439. Object: base.ManagerApiExpect(),
  1440. Method: http.MethodPut,
  1441. Path: "/apisix/admin/routes/r1",
  1442. Body: `{
  1443. "name": "route1",
  1444. "uri": "/hello",
  1445. "plugins": {
  1446. "jwt-auth": {}
  1447. },
  1448. "upstream": {
  1449. "type": "roundrobin",
  1450. "nodes": [{
  1451. "host": "` + base.UpstreamIp + `",
  1452. "port": 1980,
  1453. "weight": 1
  1454. }]
  1455. }
  1456. }`,
  1457. Headers: map[string]string{"Authorization": base.GetToken()},
  1458. ExpectStatus: http.StatusOK,
  1459. ExpectBody: `"code":0`,
  1460. })
  1461. })
  1462. It("make sure the consumer is not created", func() {
  1463. base.RunTestCase(base.HttpTestCase{
  1464. Object: base.ManagerApiExpect(),
  1465. Method: http.MethodGet,
  1466. Path: "/apisix/admin/consumers/jack",
  1467. Headers: map[string]string{"Authorization": base.GetToken()},
  1468. ExpectStatus: http.StatusNotFound,
  1469. })
  1470. })
  1471. It("create consumer", func() {
  1472. base.RunTestCase(base.HttpTestCase{
  1473. Object: base.ManagerApiExpect(),
  1474. Method: http.MethodPut,
  1475. Path: "/apisix/admin/consumers",
  1476. Body: `{
  1477. "username": "jack",
  1478. "plugins": {
  1479. "jwt-auth": {
  1480. "key": "user-key",
  1481. "secret": "my-secret-key",
  1482. "algorithm": "HS256"
  1483. }
  1484. },
  1485. "desc": "test description"
  1486. }`,
  1487. Headers: map[string]string{"Authorization": base.GetToken()},
  1488. ExpectStatus: http.StatusOK,
  1489. })
  1490. })
  1491. It("create public api for JWT sign", func() {
  1492. base.RunTestCase(base.HttpTestCase{
  1493. Object: base.ManagerApiExpect(),
  1494. Method: http.MethodPut,
  1495. Path: "/apisix/admin/routes/jwt-sign",
  1496. Body: `{
  1497. "name": "jwt-auth",
  1498. "uri": "/apisix/plugin/jwt/sign",
  1499. "plugins": {
  1500. "public-api": {}
  1501. },
  1502. "upstream": {
  1503. "type": "roundrobin",
  1504. "nodes": [{
  1505. "host": "` + base.UpstreamIp + `",
  1506. "port": 1980,
  1507. "weight": 1
  1508. }]
  1509. }
  1510. }`,
  1511. Headers: map[string]string{"Authorization": base.GetToken()},
  1512. ExpectStatus: http.StatusOK,
  1513. ExpectBody: `"code":0`,
  1514. })
  1515. })
  1516. jwtToken := ""
  1517. exportStrJWT := ""
  1518. It("sign jwt token", func() {
  1519. time.Sleep(base.SleepTime)
  1520. // sign jwt token
  1521. body, status, err := base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=user-key", nil)
  1522. assert.Nil(GinkgoT(), err)
  1523. assert.Equal(GinkgoT(), http.StatusOK, status)
  1524. jwtToken = string(body)
  1525. // sign jwt token with not exists key
  1526. _, status, err = base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=not-exist-key", nil)
  1527. assert.Nil(GinkgoT(), err)
  1528. assert.Equal(GinkgoT(), http.StatusNotFound, status)
  1529. exportStrJWT = `
  1530. "components": {
  1531. "securitySchemes": {
  1532. "bearerAuth": {
  1533. "bearerFormat": "JWT",
  1534. "scheme": "bearer",
  1535. "type": "http"
  1536. }
  1537. }
  1538. },
  1539. "info": {
  1540. "title": "RoutesExport",
  1541. "version": "3.0.0"
  1542. },
  1543. "openapi": "3.0.0",
  1544. `
  1545. exportStrJWT = replaceStr(exportStrJWT)
  1546. // verify token and clean test data
  1547. })
  1548. It("verify route with correct jwt token", func() {
  1549. base.RunTestCase(base.HttpTestCase{
  1550. Object: base.APISIXExpect(),
  1551. Method: http.MethodGet,
  1552. Path: "/hello",
  1553. Headers: map[string]string{"Authorization": jwtToken},
  1554. ExpectStatus: http.StatusOK,
  1555. ExpectBody: "hello world",
  1556. Sleep: base.SleepTime,
  1557. })
  1558. })
  1559. It("export route", func() {
  1560. base.RunTestCase(base.HttpTestCase{
  1561. Object: base.ManagerApiExpect(),
  1562. Method: http.MethodGet,
  1563. Path: "/apisix/admin/export/routes/r1",
  1564. Headers: map[string]string{"Authorization": base.GetToken()},
  1565. ExpectStatus: http.StatusOK,
  1566. ExpectBody: exportStrJWT,
  1567. })
  1568. })
  1569. It("delete consumer", func() {
  1570. base.RunTestCase(base.HttpTestCase{
  1571. Object: base.ManagerApiExpect(),
  1572. Method: http.MethodDelete,
  1573. Path: "/apisix/admin/consumers/jack",
  1574. Headers: map[string]string{"Authorization": base.GetToken()},
  1575. ExpectStatus: http.StatusOK,
  1576. })
  1577. })
  1578. It("verify route with the jwt token from just deleted consumer", func() {
  1579. base.RunTestCase(base.HttpTestCase{
  1580. Object: base.APISIXExpect(),
  1581. Method: http.MethodGet,
  1582. Path: "/hello",
  1583. Headers: map[string]string{"Authorization": jwtToken},
  1584. ExpectStatus: http.StatusUnauthorized,
  1585. ExpectBody: `{"message":"Missing related consumer"}`,
  1586. Sleep: base.SleepTime,
  1587. })
  1588. })
  1589. It("delete route", func() {
  1590. base.RunTestCase(base.HttpTestCase{
  1591. Object: base.ManagerApiExpect(),
  1592. Method: http.MethodDelete,
  1593. Path: "/apisix/admin/routes/r1",
  1594. Headers: map[string]string{"Authorization": base.GetToken()},
  1595. ExpectStatus: http.StatusOK,
  1596. })
  1597. })
  1598. It("verify the deleted route", func() {
  1599. base.RunTestCase(base.HttpTestCase{
  1600. Object: base.APISIXExpect(),
  1601. Method: http.MethodGet,
  1602. Path: "/hello",
  1603. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  1604. ExpectStatus: http.StatusNotFound,
  1605. Sleep: base.SleepTime,
  1606. })
  1607. })
  1608. exportStrJWTNoAlgorithm := `
  1609. "components": {
  1610. "securitySchemes": {
  1611. "bearerAuth": {
  1612. "bearerFormat": "JWT",
  1613. "scheme": "bearer",
  1614. "type": "http"
  1615. }
  1616. }
  1617. },
  1618. "info": {
  1619. "title": "RoutesExport",
  1620. "version": "3.0.0"
  1621. },
  1622. "openapi": "3.0.0",
  1623. `
  1624. exportStrJWTNoAlgorithm = replaceStr(exportStrJWTNoAlgorithm)
  1625. It("create consumer with jwt (no algorithm)", func() {
  1626. base.RunTestCase(base.HttpTestCase{
  1627. Object: base.ManagerApiExpect(),
  1628. Method: http.MethodPut,
  1629. Path: "/apisix/admin/consumers",
  1630. Body: `{
  1631. "username":"consumer_1",
  1632. "desc": "test description",
  1633. "plugins":{
  1634. "jwt-auth":{
  1635. "exp":86400,
  1636. "key":"user-key",
  1637. "secret":"my-secret-key"
  1638. }
  1639. }
  1640. }`,
  1641. Headers: map[string]string{"Authorization": base.GetToken()},
  1642. ExpectBody: `"code":0`,
  1643. ExpectStatus: http.StatusOK,
  1644. Sleep: base.SleepTime,
  1645. })
  1646. })
  1647. It("get the consumer", func() {
  1648. base.RunTestCase(base.HttpTestCase{
  1649. Object: base.ManagerApiExpect(),
  1650. Method: http.MethodGet,
  1651. Path: "/apisix/admin/consumers/consumer_1",
  1652. Headers: map[string]string{"Authorization": base.GetToken()},
  1653. ExpectStatus: http.StatusOK,
  1654. ExpectBody: `"username":"consumer_1"`,
  1655. Sleep: base.SleepTime,
  1656. })
  1657. })
  1658. It("create the route", func() {
  1659. base.RunTestCase(base.HttpTestCase{
  1660. Object: base.ManagerApiExpect(),
  1661. Method: http.MethodPut,
  1662. Path: "/apisix/admin/routes/r1",
  1663. Body: `{
  1664. "name": "route1",
  1665. "uri": "/hello",
  1666. "plugins": {
  1667. "jwt-auth": {}
  1668. },
  1669. "upstream": {
  1670. "type": "roundrobin",
  1671. "nodes": [{
  1672. "host": "` + base.UpstreamIp + `",
  1673. "port": 1980,
  1674. "weight": 1
  1675. }]
  1676. }
  1677. }`,
  1678. Headers: map[string]string{"Authorization": base.GetToken()},
  1679. ExpectStatus: http.StatusOK,
  1680. Sleep: base.SleepTime,
  1681. })
  1682. })
  1683. jwttoken := ""
  1684. It("sign jwt token", func() {
  1685. // sign jwt token
  1686. body, status, err := base.HttpGet(base.APISIXHost+"/apisix/plugin/jwt/sign?key=user-key", nil)
  1687. assert.Nil(GinkgoT(), err)
  1688. assert.Equal(GinkgoT(), http.StatusOK, status)
  1689. jwttoken = string(body)
  1690. })
  1691. It("hit route with jwt token", func() {
  1692. base.RunTestCase(base.HttpTestCase{
  1693. Object: base.APISIXExpect(),
  1694. Method: http.MethodGet,
  1695. Path: "/hello",
  1696. Headers: map[string]string{"Authorization": jwttoken},
  1697. ExpectStatus: http.StatusOK,
  1698. ExpectBody: "hello world",
  1699. Sleep: base.SleepTime,
  1700. })
  1701. })
  1702. It("export route", func() {
  1703. base.RunTestCase(base.HttpTestCase{
  1704. Object: base.ManagerApiExpect(),
  1705. Method: http.MethodGet,
  1706. Path: "/apisix/admin/export/routes/r1",
  1707. Headers: map[string]string{"Authorization": base.GetToken()},
  1708. ExpectStatus: http.StatusOK,
  1709. ExpectBody: exportStrJWTNoAlgorithm,
  1710. })
  1711. })
  1712. It("delete consumer", func() {
  1713. base.RunTestCase(base.HttpTestCase{
  1714. Object: base.ManagerApiExpect(),
  1715. Method: http.MethodDelete,
  1716. Path: "/apisix/admin/consumers/consumer_1",
  1717. Headers: map[string]string{"Authorization": base.GetToken()},
  1718. ExpectStatus: http.StatusOK,
  1719. ExpectBody: `"code":0`,
  1720. })
  1721. })
  1722. It("after delete consumer verify it again", func() {
  1723. base.RunTestCase(base.HttpTestCase{
  1724. Object: base.ManagerApiExpect(),
  1725. Method: http.MethodGet,
  1726. Path: "/apisix/admin/consumers/jack",
  1727. Headers: map[string]string{"Authorization": base.GetToken()},
  1728. ExpectStatus: http.StatusNotFound,
  1729. Sleep: base.SleepTime,
  1730. })
  1731. })
  1732. It("delete the route", func() {
  1733. base.RunTestCase(base.HttpTestCase{
  1734. Object: base.ManagerApiExpect(),
  1735. Method: http.MethodDelete,
  1736. Path: "/apisix/admin/routes/r1",
  1737. Headers: map[string]string{"Authorization": base.GetToken()},
  1738. ExpectStatus: http.StatusOK,
  1739. })
  1740. })
  1741. })
  1742. Context("test export route with auth plugin", func() {
  1743. It("make sure the route is not created", func() {
  1744. base.RunTestCase(base.HttpTestCase{
  1745. Object: base.APISIXExpect(),
  1746. Method: http.MethodGet,
  1747. Path: "/hello",
  1748. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  1749. ExpectStatus: http.StatusNotFound,
  1750. })
  1751. })
  1752. It("create route", func() {
  1753. base.RunTestCase(base.HttpTestCase{
  1754. Object: base.ManagerApiExpect(),
  1755. Method: http.MethodPut,
  1756. Path: "/apisix/admin/routes/r1",
  1757. Body: `{
  1758. "name": "route1",
  1759. "uri": "/hello",
  1760. "plugins": {
  1761. "key-auth": {},
  1762. "basic-auth": {}
  1763. },
  1764. "upstream": {
  1765. "type": "roundrobin",
  1766. "nodes": [{
  1767. "host": "` + base.UpstreamIp + `",
  1768. "port": 1980,
  1769. "weight": 1
  1770. }]
  1771. }
  1772. }`,
  1773. Headers: map[string]string{"Authorization": base.GetToken()},
  1774. ExpectBody: `"code":0`,
  1775. ExpectStatus: http.StatusOK,
  1776. })
  1777. })
  1778. It("make sure the consumer is not created", func() {
  1779. base.RunTestCase(base.HttpTestCase{
  1780. Object: base.ManagerApiExpect(),
  1781. Method: http.MethodGet,
  1782. Path: "/apisix/admin/consumers/jack",
  1783. Headers: map[string]string{"Authorization": base.GetToken()},
  1784. ExpectStatus: http.StatusNotFound,
  1785. })
  1786. })
  1787. It("create consumer", func() {
  1788. base.RunTestCase(base.HttpTestCase{
  1789. Object: base.ManagerApiExpect(),
  1790. Method: http.MethodPut,
  1791. Path: "/apisix/admin/consumers",
  1792. Body: `{
  1793. "username": "jack",
  1794. "plugins": {
  1795. "key-auth": {
  1796. "key": "auth-one"
  1797. },
  1798. "basic-auth": {
  1799. "username": "jack",
  1800. "password": "123456"
  1801. }
  1802. },
  1803. "desc": "test description"
  1804. }`,
  1805. Headers: map[string]string{"Authorization": base.GetToken()},
  1806. ExpectStatus: http.StatusOK,
  1807. })
  1808. })
  1809. time.Sleep(base.SleepTime)
  1810. exportStrAuth := `
  1811. "components": {
  1812. "securitySchemes": {
  1813. "api_key": {
  1814. "in": "header",
  1815. "name": "X-XSRF-TOKEN",
  1816. "type": "apiKey"
  1817. },
  1818. "basicAuth": {
  1819. "in": "header",
  1820. "name": "basicAuth",
  1821. "type": "basicAuth"
  1822. }
  1823. }
  1824. },
  1825. "info": {
  1826. "title": "RoutesExport",
  1827. "version": "3.0.0"
  1828. },
  1829. "openapi": "3.0.0"`
  1830. time.Sleep(base.SleepTime)
  1831. exportStrAuth = replaceStr(exportStrAuth)
  1832. It("verify route with correct basic-auth and key-auth token", func() {
  1833. base.RunTestCase(base.HttpTestCase{
  1834. Object: base.APISIXExpect(),
  1835. Method: http.MethodGet,
  1836. Path: "/hello",
  1837. Headers: map[string]string{"Authorization": "Basic amFjazoxMjM0NTYKIA==", "apikey": "auth-one"},
  1838. ExpectStatus: http.StatusOK,
  1839. ExpectBody: "hello world",
  1840. Sleep: base.SleepTime,
  1841. })
  1842. })
  1843. It("export route", func() {
  1844. base.RunTestCase(base.HttpTestCase{
  1845. Object: base.ManagerApiExpect(),
  1846. Method: http.MethodGet,
  1847. Path: "/apisix/admin/export/routes/r1",
  1848. Headers: map[string]string{"Authorization": base.GetToken()},
  1849. ExpectStatus: http.StatusOK,
  1850. ExpectBody: exportStrAuth,
  1851. })
  1852. })
  1853. It("delete consumer", func() {
  1854. base.RunTestCase(base.HttpTestCase{
  1855. Object: base.ManagerApiExpect(),
  1856. Method: http.MethodDelete,
  1857. Path: "/apisix/admin/consumers/jack",
  1858. Headers: map[string]string{"Authorization": base.GetToken()},
  1859. ExpectStatus: http.StatusOK,
  1860. })
  1861. })
  1862. It("verify route with the basic-auth and key-auth token from just deleted consumer", func() {
  1863. base.RunTestCase(base.HttpTestCase{
  1864. Object: base.APISIXExpect(),
  1865. Method: http.MethodGet,
  1866. Path: "/hello",
  1867. Headers: map[string]string{"Authorization": "Basic amFjazoxMjM0NTYKIA==", "apikey": "auth-one"},
  1868. ExpectStatus: http.StatusUnauthorized,
  1869. ExpectBody: `{"message":"Missing related consumer"}`,
  1870. Sleep: base.SleepTime,
  1871. })
  1872. })
  1873. It("delete route", func() {
  1874. base.RunTestCase(base.HttpTestCase{
  1875. Object: base.ManagerApiExpect(),
  1876. Method: http.MethodDelete,
  1877. Path: "/apisix/admin/routes/r1",
  1878. Headers: map[string]string{"Authorization": base.GetToken()},
  1879. ExpectStatus: http.StatusOK,
  1880. })
  1881. })
  1882. It("verify the deleted route", func() {
  1883. base.RunTestCase(base.HttpTestCase{
  1884. Object: base.APISIXExpect(),
  1885. Method: http.MethodGet,
  1886. Path: "/hello",
  1887. ExpectStatus: http.StatusNotFound,
  1888. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  1889. Sleep: base.SleepTime,
  1890. })
  1891. })
  1892. })
  1893. Context("test route export label", func() {
  1894. // 10.Create a service with label data and a route with label data, and export the route.
  1895. // Label is the original data of the route
  1896. serviceStrS1 := `
  1897. "name": "testservice",
  1898. "desc": "testservice_desc",
  1899. "labels": {
  1900. "build": "10"
  1901. },
  1902. "enable_websocket": true`
  1903. serviceStrS1 = replaceStr(serviceStrS1)
  1904. exportStrR1 := `
  1905. "components": {},
  1906. "info": {
  1907. "title": "RoutesExport",
  1908. "version": "3.0.0"
  1909. },
  1910. "openapi": "3.0.0",
  1911. "paths": {
  1912. "/hello": {
  1913. "get": {
  1914. "operationId": "route_allGET",
  1915. "requestBody": {},
  1916. "responses": {
  1917. "default": {
  1918. "description": ""
  1919. }
  1920. },
  1921. "summary": "所有",
  1922. "x-apisix-enable_websocket": false,
  1923. "x-apisix-id":"r1",
  1924. "x-apisix-labels": {
  1925. "build": "16",
  1926. "env": "production",
  1927. "version": "v2"
  1928. },
  1929. "x-apisix-priority": 0,
  1930. "x-apisix-status": 1,
  1931. "x-apisix-vars":[
  1932. [
  1933. "arg_name",
  1934. "==",
  1935. "test"
  1936. ]
  1937. ]
  1938. }`
  1939. exportStrR1 = replaceStr(exportStrR1)
  1940. It("create service", func() {
  1941. base.RunTestCase(base.HttpTestCase{
  1942. Object: base.ManagerApiExpect(),
  1943. Method: http.MethodPut,
  1944. Path: "/apisix/admin/services/s1",
  1945. Headers: map[string]string{"Authorization": base.GetToken()},
  1946. Body: `{
  1947. "name": "testservice",
  1948. "desc": "testservice_desc",
  1949. "enable_websocket":true,
  1950. "labels": {
  1951. "build": "10"
  1952. }
  1953. }`,
  1954. ExpectStatus: http.StatusOK,
  1955. })
  1956. })
  1957. It("get the service s1", func() {
  1958. base.RunTestCase(base.HttpTestCase{
  1959. Object: base.ManagerApiExpect(),
  1960. Method: http.MethodGet,
  1961. Path: "/apisix/admin/services/s1",
  1962. Headers: map[string]string{"Authorization": base.GetToken()},
  1963. ExpectCode: http.StatusOK,
  1964. ExpectBody: serviceStrS1,
  1965. })
  1966. })
  1967. It("Create a service with label data and a route with label data", func() {
  1968. base.RunTestCase(base.HttpTestCase{
  1969. Object: base.ManagerApiExpect(),
  1970. Method: http.MethodPut,
  1971. Path: "/apisix/admin/routes/r1",
  1972. Body: `{
  1973. "name": "route_all",
  1974. "desc": "所有",
  1975. "status": 1,
  1976. "methods": ["GET"],
  1977. "service_id": "s1",
  1978. "labels": {
  1979. "build": "16",
  1980. "env": "production",
  1981. "version": "v2"
  1982. },
  1983. "vars": [
  1984. ["arg_name", "==", "test"]
  1985. ],
  1986. "uri": "/hello"
  1987. }`,
  1988. Headers: map[string]string{"Authorization": base.GetToken()},
  1989. ExpectCode: http.StatusOK,
  1990. Sleep: base.SleepTime,
  1991. })
  1992. })
  1993. It("export route1", func() {
  1994. base.RunTestCase(base.HttpTestCase{
  1995. Object: base.ManagerApiExpect(),
  1996. Method: http.MethodGet,
  1997. Path: "/apisix/admin/export/routes/r1",
  1998. Headers: map[string]string{"Authorization": base.GetToken()},
  1999. ExpectCode: http.StatusOK,
  2000. ExpectBody: exportStrR1,
  2001. })
  2002. })
  2003. It("delete the route1 just created", func() {
  2004. base.RunTestCase(base.HttpTestCase{
  2005. Object: base.ManagerApiExpect(),
  2006. Method: http.MethodDelete,
  2007. Path: "/apisix/admin/routes/r1",
  2008. Headers: map[string]string{"Authorization": base.GetToken()},
  2009. ExpectCode: http.StatusOK,
  2010. })
  2011. })
  2012. It("hit the route1 just deleted", func() {
  2013. base.RunTestCase(base.HttpTestCase{
  2014. Object: base.APISIXExpect(),
  2015. Method: http.MethodGet,
  2016. Path: "/hello",
  2017. ExpectStatus: http.StatusNotFound,
  2018. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  2019. Sleep: base.SleepTime,
  2020. })
  2021. })
  2022. It("delete the service1", func() {
  2023. base.RunTestCase(base.HttpTestCase{
  2024. Object: base.ManagerApiExpect(),
  2025. Method: http.MethodDelete,
  2026. Path: "/apisix/admin/services/s1",
  2027. Headers: map[string]string{"Authorization": base.GetToken()},
  2028. ExpectStatus: http.StatusOK,
  2029. Sleep: base.SleepTime,
  2030. })
  2031. })
  2032. // 11.Create a service with label data and a route without label data, and export the route.
  2033. // Label is the data of the service
  2034. serviceStrS2 := `
  2035. "name": "testservice",
  2036. "desc": "testservice_desc",
  2037. "labels": {
  2038. "build": "16",
  2039. "env": "production",
  2040. "version": "v2"
  2041. },
  2042. "enable_websocket": true`
  2043. serviceStrS2 = replaceStr(serviceStrS2)
  2044. exportStrR2 := `
  2045. "components": {},
  2046. "info": {
  2047. "title": "RoutesExport",
  2048. "version": "3.0.0"
  2049. },
  2050. "openapi": "3.0.0",
  2051. "paths": {
  2052. "/hello": {
  2053. "get": {
  2054. "operationId": "route_allGET",
  2055. "requestBody": {},
  2056. "responses": {
  2057. "default": {
  2058. "description": ""
  2059. }
  2060. },
  2061. "summary": "所有",
  2062. "x-apisix-enable_websocket": false,
  2063. "x-apisix-id":"r2",
  2064. "x-apisix-labels": {
  2065. "build": "16",
  2066. "env": "production",
  2067. "version": "v2"
  2068. },
  2069. "x-apisix-priority": 0,
  2070. "x-apisix-status": 1,
  2071. "x-apisix-vars":[
  2072. [
  2073. "arg_name",
  2074. "==",
  2075. "test"
  2076. ]
  2077. ]
  2078. }`
  2079. exportStrR2 = replaceStr(exportStrR2)
  2080. It("create service", func() {
  2081. base.RunTestCase(base.HttpTestCase{
  2082. Object: base.ManagerApiExpect(),
  2083. Method: http.MethodPut,
  2084. Path: "/apisix/admin/services/s2",
  2085. Headers: map[string]string{"Authorization": base.GetToken()},
  2086. Body: `{
  2087. "name": "testservice",
  2088. "desc": "testservice_desc",
  2089. "enable_websocket":true,
  2090. "labels": {
  2091. "build": "16",
  2092. "env": "production",
  2093. "version": "v2"
  2094. }
  2095. }`,
  2096. ExpectStatus: http.StatusOK,
  2097. })
  2098. })
  2099. It("get the service s2", func() {
  2100. base.RunTestCase(base.HttpTestCase{
  2101. Object: base.ManagerApiExpect(),
  2102. Method: http.MethodGet,
  2103. Path: "/apisix/admin/services/s2",
  2104. Headers: map[string]string{"Authorization": base.GetToken()},
  2105. ExpectBody: serviceStrS2,
  2106. ExpectStatus: http.StatusOK,
  2107. })
  2108. })
  2109. It("Create a service with label data and a route without label data", func() {
  2110. base.RunTestCase(base.HttpTestCase{
  2111. Object: base.ManagerApiExpect(),
  2112. Method: http.MethodPut,
  2113. Path: "/apisix/admin/routes/r2",
  2114. Body: `{
  2115. "name": "route_all",
  2116. "desc": "所有",
  2117. "status": 1,
  2118. "methods": ["GET"],
  2119. "service_id": "s2",
  2120. "vars": [
  2121. ["arg_name", "==", "test"]
  2122. ],
  2123. "uri": "/hello"
  2124. }`,
  2125. Headers: map[string]string{"Authorization": base.GetToken()},
  2126. ExpectStatus: http.StatusOK,
  2127. Sleep: base.SleepTime,
  2128. })
  2129. })
  2130. It("export route2", func() {
  2131. base.RunTestCase(base.HttpTestCase{
  2132. Object: base.ManagerApiExpect(),
  2133. Method: http.MethodGet,
  2134. Path: "/apisix/admin/export/routes/r2",
  2135. Headers: map[string]string{"Authorization": base.GetToken()},
  2136. ExpectBody: exportStrR2,
  2137. ExpectStatus: http.StatusOK,
  2138. })
  2139. })
  2140. It("delete the route2 just created", func() {
  2141. base.RunTestCase(base.HttpTestCase{
  2142. Object: base.ManagerApiExpect(),
  2143. Method: http.MethodDelete,
  2144. Path: "/apisix/admin/routes/r2",
  2145. Headers: map[string]string{"Authorization": base.GetToken()},
  2146. ExpectStatus: http.StatusOK,
  2147. })
  2148. })
  2149. It("hit the route2 just deleted", func() {
  2150. base.RunTestCase(base.HttpTestCase{
  2151. Object: base.APISIXExpect(),
  2152. Method: http.MethodGet,
  2153. Path: "/hello",
  2154. ExpectStatus: http.StatusNotFound,
  2155. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  2156. Sleep: base.SleepTime,
  2157. })
  2158. })
  2159. It("delete the service2", func() {
  2160. base.RunTestCase(base.HttpTestCase{
  2161. Object: base.ManagerApiExpect(),
  2162. Method: http.MethodDelete,
  2163. Path: "/apisix/admin/services/s2",
  2164. Headers: map[string]string{"Authorization": base.GetToken()},
  2165. ExpectStatus: http.StatusOK,
  2166. Sleep: base.SleepTime,
  2167. })
  2168. })
  2169. })
  2170. Context("test route export request validation", func() {
  2171. // 12.Test export route request_ validation data correctness
  2172. exportStrR1 := `
  2173. "components": {},
  2174. "info": {
  2175. "title": "RoutesExport",
  2176. "version": "3.0.0"
  2177. },
  2178. "openapi": "3.0.0",
  2179. "paths": {
  2180. "/test-test": {
  2181. "get": {
  2182. "operationId": "route_allGET",
  2183. "parameters": [{
  2184. "in": "header",
  2185. "name": "test",
  2186. "schema": {
  2187. "type": "string"
  2188. }
  2189. }],
  2190. "requestBody": {
  2191. "content": {
  2192. "*/*": {
  2193. "schema": {
  2194. "properties": {
  2195. "boolean_payload": {
  2196. "type": "boolean"
  2197. },
  2198. "required_payload": {
  2199. "type": "string"
  2200. }
  2201. },
  2202. "required": ["required_payload"],
  2203. "type": "object"
  2204. }
  2205. }
  2206. }
  2207. },
  2208. "responses": {
  2209. "default": {
  2210. "description": ""
  2211. }
  2212. },
  2213. "security": [],
  2214. "summary": "所有",
  2215. "x-apisix-enable_websocket": false,
  2216. "x-apisix-hosts": ["test.com"],
  2217. "x-apisix-id":"r1",
  2218. "x-apisix-priority": 0,
  2219. "x-apisix-status": 1
  2220. }
  2221. }
  2222. }`
  2223. exportStrR1 = replaceStr(exportStrR1)
  2224. It("Create a route containing request_ validation data", func() {
  2225. base.RunTestCase(base.HttpTestCase{
  2226. Object: base.ManagerApiExpect(),
  2227. Method: http.MethodPut,
  2228. Path: "/apisix/admin/routes/r1",
  2229. Body: `{
  2230. "uris": ["/test-test"],
  2231. "name": "route_all",
  2232. "desc": "所有",
  2233. "methods": ["GET"],
  2234. "hosts": ["test.com"],
  2235. "plugins": {
  2236. "request-validation": {
  2237. "body_schema": {
  2238. "properties": {
  2239. "boolean_payload": {
  2240. "type": "boolean"
  2241. },
  2242. "required_payload": {
  2243. "type": "string"
  2244. }
  2245. },
  2246. "required": ["required_payload"],
  2247. "type": "object"
  2248. },
  2249. "disable": false,
  2250. "header_schema": {
  2251. "properties": {
  2252. "test": {
  2253. "enum": "test-enum",
  2254. "type": "string"
  2255. }
  2256. },
  2257. "type": "string"
  2258. }
  2259. }
  2260. },
  2261. "status": 1
  2262. }`,
  2263. Headers: map[string]string{"Authorization": base.GetToken()},
  2264. ExpectStatus: http.StatusOK,
  2265. Sleep: base.SleepTime,
  2266. })
  2267. })
  2268. It("export route1", func() {
  2269. base.RunTestCase(base.HttpTestCase{
  2270. Object: base.ManagerApiExpect(),
  2271. Method: http.MethodGet,
  2272. Path: "/apisix/admin/export/routes/r1",
  2273. Headers: map[string]string{"Authorization": base.GetToken()},
  2274. ExpectStatus: http.StatusOK,
  2275. ExpectBody: exportStrR1,
  2276. })
  2277. })
  2278. It("delete the route1 just created", func() {
  2279. base.RunTestCase(base.HttpTestCase{
  2280. Object: base.ManagerApiExpect(),
  2281. Method: http.MethodDelete,
  2282. Path: "/apisix/admin/routes/r1",
  2283. Headers: map[string]string{"Authorization": base.GetToken()},
  2284. ExpectStatus: http.StatusOK,
  2285. })
  2286. })
  2287. It("hit the route1 just deleted", func() {
  2288. base.RunTestCase(base.HttpTestCase{
  2289. Object: base.APISIXExpect(),
  2290. Method: http.MethodGet,
  2291. Path: "/hello",
  2292. ExpectStatus: http.StatusNotFound,
  2293. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  2294. Sleep: base.SleepTime,
  2295. })
  2296. })
  2297. })
  2298. Context("test route export equal uri", func() {
  2299. // 13.Add suffix when testing the same URI export
  2300. exportStrAll := `
  2301. "components": {},
  2302. "info": {
  2303. "title": "RoutesExport",
  2304. "version": "3.0.0"
  2305. },
  2306. "openapi": "3.0.0",
  2307. "paths": {
  2308. "/test-test": {
  2309. "get": {
  2310. "operationId": "route_allGET",
  2311. "requestBody": {},
  2312. "responses": {
  2313. "default": {
  2314. "description": ""
  2315. }
  2316. },
  2317. "summary": "所有",
  2318. "x-apisix-enable_websocket": false,
  2319. "x-apisix-hosts": ["test.com"],
  2320. "x-apisix-id":"r1",
  2321. "x-apisix-priority": 0,
  2322. "x-apisix-status": 1,
  2323. "x-apisix-upstream": {
  2324. "nodes": {
  2325. "` + base.UpstreamIp + `:1980": 1
  2326. },
  2327. "type": "roundrobin"
  2328. }
  2329. }
  2330. },
  2331. "/test-test-APISIX-REPEAT-URI-2": {
  2332. "get": {
  2333. "operationId": "route_all2GET",
  2334. "requestBody": {},
  2335. "responses": {
  2336. "default": {
  2337. "description": ""
  2338. }
  2339. },
  2340. "summary": "所有1",
  2341. "x-apisix-enable_websocket": false,
  2342. "x-apisix-hosts": ["test.com"],
  2343. "x-apisix-id":"r2",
  2344. "x-apisix-priority": 0,
  2345. "x-apisix-status": 1,
  2346. "x-apisix-upstream": {
  2347. "nodes": {
  2348. "` + base.UpstreamIp + `:1980": 1
  2349. },
  2350. "type": "roundrobin"
  2351. }
  2352. }
  2353. },
  2354. "/test-test-APISIX-REPEAT-URI-3": {
  2355. "get": {
  2356. "operationId": "route_all3GET",
  2357. "requestBody": {},
  2358. "responses": {
  2359. "default": {
  2360. "description": ""
  2361. }
  2362. },
  2363. "summary": "所有2",
  2364. "x-apisix-enable_websocket": false,
  2365. "x-apisix-hosts": ["test.com"],
  2366. "x-apisix-id":"r3",
  2367. "x-apisix-priority": 0,
  2368. "x-apisix-status": 1,
  2369. "x-apisix-upstream": {
  2370. "nodes": {
  2371. "` + base.UpstreamIp + `:1981": 1
  2372. },
  2373. "type": "roundrobin"
  2374. }
  2375. }
  2376. }
  2377. }`
  2378. exportStrAll = replaceStr(exportStrAll)
  2379. It("Create a route", func() {
  2380. base.RunTestCase(base.HttpTestCase{
  2381. Object: base.ManagerApiExpect(),
  2382. Method: http.MethodPut,
  2383. Path: "/apisix/admin/routes/r1",
  2384. Body: `{
  2385. "uris": ["/test-test"],
  2386. "name": "route_all",
  2387. "desc": "所有",
  2388. "methods": ["GET"],
  2389. "hosts": ["test.com"],
  2390. "status": 1,
  2391. "upstream": {
  2392. "nodes": {
  2393. "` + base.UpstreamIp + `:1980": 1
  2394. },
  2395. "type": "roundrobin"
  2396. }
  2397. }`,
  2398. Headers: map[string]string{"Authorization": base.GetToken()},
  2399. ExpectStatus: http.StatusOK,
  2400. Sleep: base.SleepTime,
  2401. })
  2402. })
  2403. It("Create a route2", func() {
  2404. base.RunTestCase(base.HttpTestCase{
  2405. Object: base.ManagerApiExpect(),
  2406. Method: http.MethodPut,
  2407. Path: "/apisix/admin/routes/r2",
  2408. Body: `{
  2409. "uris": ["/test-test"],
  2410. "name": "route_all2",
  2411. "desc": "所有1",
  2412. "methods": ["GET"],
  2413. "hosts": ["test.com"],
  2414. "status": 1,
  2415. "upstream": {
  2416. "nodes": {
  2417. "` + base.UpstreamIp + `:1980": 1
  2418. },
  2419. "type": "roundrobin"
  2420. }
  2421. }`,
  2422. Headers: map[string]string{"Authorization": base.GetToken()},
  2423. ExpectStatus: http.StatusOK,
  2424. Sleep: base.SleepTime,
  2425. })
  2426. })
  2427. It("Create a route3", func() {
  2428. base.RunTestCase(base.HttpTestCase{
  2429. Object: base.ManagerApiExpect(),
  2430. Method: http.MethodPut,
  2431. Path: "/apisix/admin/routes/r3",
  2432. Body: `{
  2433. "uris": ["/test-test"],
  2434. "name": "route_all3",
  2435. "desc": "所有2",
  2436. "methods": ["GET"],
  2437. "hosts": ["test.com"],
  2438. "status": 1,
  2439. "upstream": {
  2440. "nodes": {
  2441. "` + base.UpstreamIp + `:1981": 1
  2442. },
  2443. "type": "roundrobin"
  2444. }
  2445. }`,
  2446. Headers: map[string]string{"Authorization": base.GetToken()},
  2447. ExpectStatus: http.StatusOK,
  2448. Sleep: base.SleepTime,
  2449. })
  2450. })
  2451. It("use the exportall interface to export all routes", func() {
  2452. base.RunTestCase(base.HttpTestCase{
  2453. Object: base.ManagerApiExpect(),
  2454. Method: http.MethodGet,
  2455. Path: "/apisix/admin/export/routes/r1,r2,r3",
  2456. Headers: map[string]string{"Authorization": base.GetToken()},
  2457. ExpectStatus: http.StatusOK,
  2458. ExpectBody: exportStrAll,
  2459. })
  2460. })
  2461. It("delete the route1 just created", func() {
  2462. base.RunTestCase(base.HttpTestCase{
  2463. Object: base.ManagerApiExpect(),
  2464. Method: http.MethodDelete,
  2465. Path: "/apisix/admin/routes/r1",
  2466. Headers: map[string]string{"Authorization": base.GetToken()},
  2467. ExpectStatus: http.StatusOK,
  2468. })
  2469. })
  2470. It("hit the route1 just deleted", func() {
  2471. base.RunTestCase(base.HttpTestCase{
  2472. Object: base.APISIXExpect(),
  2473. Method: http.MethodGet,
  2474. Path: "/hello",
  2475. Headers: map[string]string{"Authorization": base.GetToken()},
  2476. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  2477. Sleep: base.SleepTime,
  2478. })
  2479. })
  2480. It("delete the route2 just created", func() {
  2481. base.RunTestCase(base.HttpTestCase{
  2482. Object: base.ManagerApiExpect(),
  2483. Method: http.MethodDelete,
  2484. Path: "/apisix/admin/routes/r2",
  2485. Headers: map[string]string{"Authorization": base.GetToken()},
  2486. ExpectStatus: http.StatusOK,
  2487. })
  2488. })
  2489. It("hit the route2 just deleted", func() {
  2490. base.RunTestCase(base.HttpTestCase{
  2491. Object: base.APISIXExpect(),
  2492. Method: http.MethodGet,
  2493. Path: "/hello",
  2494. ExpectStatus: http.StatusNotFound,
  2495. ExpectBody: `{"error_msg":"404 Route Not Found"}`,
  2496. Sleep: base.SleepTime,
  2497. })
  2498. })
  2499. })
  2500. })
  2501. func replaceStr(str string) string {
  2502. str = strings.Replace(str, "\n", "", -1)
  2503. str = strings.Replace(str, "\t", "", -1)
  2504. str = strings.Replace(str, " ", "", -1)
  2505. return str
  2506. }