route_online_debug_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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_online_debug_test
  18. import (
  19. "encoding/json"
  20. "io/ioutil"
  21. "net/http"
  22. "path/filepath"
  23. "time"
  24. . "github.com/onsi/ginkgo/v2"
  25. . "github.com/onsi/gomega"
  26. "github.com/tidwall/gjson"
  27. "github.com/apisix/manager-api/test/e2e/base"
  28. )
  29. var upstream map[string]interface{} = map[string]interface{}{
  30. "type": "roundrobin",
  31. "nodes": []map[string]interface{}{
  32. {
  33. "host": base.UpstreamIp,
  34. "port": 1980,
  35. "weight": 1,
  36. },
  37. },
  38. }
  39. var _ = Describe("Route_Online_Debug_Route_Not_Exist", func() {
  40. DescribeTable("Route_Online_Debug_Route_Not_Exist",
  41. func(tc base.HttpTestCase) {
  42. base.RunTestCase(tc)
  43. },
  44. Entry("hit route that not exist", base.HttpTestCase{
  45. Object: base.APISIXExpect(),
  46. Method: http.MethodGet,
  47. Path: "/hello_",
  48. ExpectStatus: http.StatusNotFound,
  49. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  50. }),
  51. Entry("online debug route with query params", base.HttpTestCase{
  52. Object: base.ManagerApiExpect(),
  53. Method: http.MethodPost,
  54. Path: "/apisix/admin/debug-request-forwarding",
  55. Headers: map[string]string{
  56. "Authorization": base.GetToken(),
  57. "online_debug_url": base.APISIXInternalUrl + `/hello_`,
  58. "online_debug_request_protocol": "http",
  59. "online_debug_method": "GET",
  60. "Content-Type": "multipart/form-data",
  61. "online_debug_header_params": `{"test":["test1"]}`,
  62. },
  63. ExpectStatus: http.StatusOK,
  64. ExpectBody: `{"code":0,"message":"","data":{"code":404,"header":{"Connection":["keep-alive"],"Content-Type":["text/plain; charset=utf-8"]`,
  65. Sleep: base.SleepTime,
  66. }),
  67. )
  68. })
  69. var _ = Describe("Route_Online_Debug_Route_With_Query_Params", func() {
  70. It("hit route that not exist", func() {
  71. base.RunTestCase(base.HttpTestCase{
  72. Object: base.APISIXExpect(),
  73. Method: http.MethodGet,
  74. Path: "/hello",
  75. ExpectStatus: http.StatusNotFound,
  76. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  77. })
  78. })
  79. It("create route with query params", func() {
  80. var routeBody map[string]interface{} = map[string]interface{}{
  81. "name": "route1",
  82. "uri": "/hello",
  83. "methods": []string{"GET"},
  84. "vars": []interface{}{
  85. []string{"arg_name", "==", "aaa"},
  86. },
  87. "upstream": upstream,
  88. }
  89. _routeBody, err := json.Marshal(routeBody)
  90. Expect(err).To(BeNil())
  91. base.RunTestCase(base.HttpTestCase{
  92. Object: base.ManagerApiExpect(),
  93. Method: http.MethodPut,
  94. Path: "/apisix/admin/routes/r1",
  95. Body: string(_routeBody),
  96. Headers: map[string]string{"Authorization": base.GetToken()},
  97. ExpectStatus: http.StatusOK,
  98. })
  99. })
  100. It("online debug route with query params", func() {
  101. base.RunTestCase(base.HttpTestCase{
  102. Object: base.ManagerApiExpect(),
  103. Method: http.MethodPost,
  104. Path: "/apisix/admin/debug-request-forwarding",
  105. Headers: map[string]string{
  106. "Authorization": base.GetToken(),
  107. "online_debug_url": base.APISIXInternalUrl + `/hello?name=aaa`,
  108. "online_debug_request_protocol": "http",
  109. "online_debug_method": "GET",
  110. "Content-Type": "multipart/form-data",
  111. "online_debug_header_params": `{"test":["test1"]}`,
  112. },
  113. ExpectStatus: http.StatusOK,
  114. ExpectBody: `{"code":0,"message":"","data":{"code":200,"header":{"Connection":["keep-alive"],"Content-Type":["application/octet-stream"],`,
  115. Sleep: base.SleepTime,
  116. })
  117. })
  118. It("delete the route just created", func() {
  119. base.RunTestCase(base.HttpTestCase{
  120. Object: base.ManagerApiExpect(),
  121. Method: http.MethodDelete,
  122. Path: "/apisix/admin/routes/r1",
  123. Headers: map[string]string{"Authorization": base.GetToken()},
  124. ExpectStatus: http.StatusOK,
  125. })
  126. })
  127. It("hit the route just deleted", func() {
  128. base.RunTestCase(base.HttpTestCase{
  129. Object: base.APISIXExpect(),
  130. Method: http.MethodGet,
  131. Path: "/hello",
  132. ExpectStatus: http.StatusNotFound,
  133. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  134. Sleep: base.SleepTime,
  135. })
  136. })
  137. })
  138. var _ = Describe("Route_Online_Debug_Route_With_Header_Params", func() {
  139. It("hit route that not exist", func() {
  140. base.RunTestCase(base.HttpTestCase{
  141. Object: base.APISIXExpect(),
  142. Method: http.MethodGet,
  143. Path: "/hello",
  144. ExpectStatus: http.StatusNotFound,
  145. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  146. })
  147. })
  148. It("create route with header params", func() {
  149. var routeBody map[string]interface{} = map[string]interface{}{
  150. "name": "route1",
  151. "uri": "/hello",
  152. "methods": []string{"GET"},
  153. "vars": []interface{}{
  154. []string{"http_version", "==", "v2"},
  155. },
  156. "upstream": upstream,
  157. }
  158. _reqRouteBody, err := json.Marshal(routeBody)
  159. Expect(err).To(BeNil())
  160. base.RunTestCase(base.HttpTestCase{
  161. Object: base.ManagerApiExpect(),
  162. Method: http.MethodPut,
  163. Path: "/apisix/admin/routes/r1",
  164. Body: string(_reqRouteBody),
  165. Headers: map[string]string{"Authorization": base.GetToken()},
  166. ExpectStatus: http.StatusOK,
  167. Sleep: base.SleepTime,
  168. })
  169. })
  170. It("online debug route with header params", func() {
  171. base.RunTestCase(base.HttpTestCase{
  172. Object: base.ManagerApiExpect(),
  173. Method: http.MethodPost,
  174. Path: "/apisix/admin/debug-request-forwarding",
  175. Headers: map[string]string{
  176. "Authorization": base.GetToken(),
  177. "online_debug_url": base.APISIXInternalUrl + `/hello`,
  178. "online_debug_request_protocol": "http",
  179. "online_debug_method": "GET",
  180. "Content-Type": "multipart/form-data",
  181. "online_debug_header_params": `{"test":["test1"],"version":["v2"]}`,
  182. },
  183. ExpectStatus: http.StatusOK,
  184. ExpectBody: `{"code":0,"message":"","data":{"code":200,"header":{"Connection":["keep-alive"],"Content-Type":["application/octet-stream"],`,
  185. Sleep: base.SleepTime,
  186. })
  187. })
  188. It("online debug route with header params(add Content-type to header params to create route)", func() {
  189. var routeBody map[string]interface{} = map[string]interface{}{
  190. "name": "route2",
  191. "status": 1,
  192. "uri": "/hello_",
  193. "methods": []string{"GET"},
  194. "upstream": upstream,
  195. }
  196. _reqRouteBody, err := json.Marshal(routeBody)
  197. Expect(err).To(BeNil())
  198. base.RunTestCase(base.HttpTestCase{
  199. Object: base.ManagerApiExpect(),
  200. Method: http.MethodPost,
  201. Path: "/apisix/admin/debug-request-forwarding",
  202. Body: string(_reqRouteBody),
  203. Headers: map[string]string{
  204. "Authorization": base.GetToken(),
  205. "online_debug_url": base.ManagerAPIHost + `/apisix/admin/routes/r2`,
  206. "online_debug_request_protocol": "http",
  207. "online_debug_method": http.MethodPut,
  208. "Content-Type": "text/plain;charset=UTF-8",
  209. "online_debug_header_params": `{"Content-type":["application/json"],"Authorization":["` + base.GetToken() + `"]}`,
  210. },
  211. ExpectStatus: http.StatusOK,
  212. ExpectBody: `{"code":0,"message":"","data":{"code":200,"header":{"Access-Control-Allow-Credentials":["true"],"Access-Control-Allow-Headers":["Authorization"],"Access-Control-Allow-Methods":["*"],"Access-Control-Allow-Origin":["*"],"Content-Length":["296"],"Content-Type":["application/json"]`,
  213. Sleep: base.SleepTime,
  214. })
  215. })
  216. It("hit the route (r2)", func() {
  217. base.RunTestCase(base.HttpTestCase{
  218. Object: base.APISIXExpect(),
  219. Method: http.MethodGet,
  220. Path: "/hello_",
  221. ExpectStatus: http.StatusOK,
  222. ExpectBody: "hello world\n",
  223. Sleep: base.SleepTime,
  224. })
  225. })
  226. It("delete the route just created (r1)", func() {
  227. base.RunTestCase(base.HttpTestCase{
  228. Object: base.ManagerApiExpect(),
  229. Method: http.MethodDelete,
  230. Path: "/apisix/admin/routes/r1",
  231. Headers: map[string]string{"Authorization": base.GetToken()},
  232. ExpectStatus: http.StatusOK,
  233. })
  234. })
  235. It("delete the route just created (r2)", func() {
  236. base.RunTestCase(base.HttpTestCase{
  237. Object: base.ManagerApiExpect(),
  238. Method: http.MethodDelete,
  239. Path: "/apisix/admin/routes/r2",
  240. Headers: map[string]string{"Authorization": base.GetToken()},
  241. ExpectStatus: http.StatusOK,
  242. })
  243. })
  244. It("hit the route just deleted (r1)", func() {
  245. base.RunTestCase(base.HttpTestCase{
  246. Object: base.APISIXExpect(),
  247. Method: http.MethodGet,
  248. Path: "/hello",
  249. ExpectStatus: http.StatusNotFound,
  250. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  251. Sleep: base.SleepTime,
  252. })
  253. })
  254. It("hit the route just deleted (r2)", func() {
  255. base.RunTestCase(base.HttpTestCase{
  256. Object: base.APISIXExpect(),
  257. Method: http.MethodGet,
  258. Path: "/hello_",
  259. ExpectStatus: http.StatusNotFound,
  260. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  261. Sleep: base.SleepTime,
  262. })
  263. })
  264. })
  265. var _ = Describe("Route_Online_Debug_Route_With_Body_Params", func() {
  266. It("hit route that not exist", func() {
  267. base.RunTestCase(base.HttpTestCase{
  268. Object: base.APISIXExpect(),
  269. Method: http.MethodGet,
  270. Path: "/hello",
  271. ExpectStatus: http.StatusNotFound,
  272. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  273. })
  274. })
  275. It("create route with method POST", func() {
  276. var routeBody map[string]interface{} = map[string]interface{}{
  277. "name": "route1",
  278. "uri": "/hello",
  279. "methods": []string{"POST"},
  280. "upstream": upstream,
  281. }
  282. _reqRouteBody, err := json.Marshal(routeBody)
  283. Expect(err).To(BeNil())
  284. base.RunTestCase(base.HttpTestCase{
  285. Object: base.ManagerApiExpect(),
  286. Method: http.MethodPut,
  287. Path: "/apisix/admin/routes/r1",
  288. Body: string(_reqRouteBody),
  289. Headers: map[string]string{"Authorization": base.GetToken()},
  290. ExpectStatus: http.StatusOK,
  291. Sleep: base.SleepTime,
  292. })
  293. })
  294. It("online debug route with body params", func() {
  295. base.RunTestCase(base.HttpTestCase{
  296. Object: base.ManagerApiExpect(),
  297. Method: http.MethodPost,
  298. Path: "/apisix/admin/debug-request-forwarding",
  299. Body: `{
  300. "name": "test",
  301. "desc": "online debug route with body params"
  302. }`,
  303. Headers: map[string]string{
  304. "Authorization": base.GetToken(),
  305. "online_debug_url": base.APISIXInternalUrl + `/hello`,
  306. "online_debug_request_protocol": "http",
  307. "online_debug_method": http.MethodPost,
  308. "Content-Type": "application/json",
  309. "online_debug_header_params": `{"test":["test1"]}`,
  310. },
  311. ExpectStatus: http.StatusOK,
  312. ExpectBody: `{"code":0,"message":"","data":{"code":200,"header":{"Connection":["keep-alive"],"Content-Type":["application/octet-stream"]`,
  313. Sleep: base.SleepTime,
  314. })
  315. })
  316. It("delete the route just created", func() {
  317. base.RunTestCase(base.HttpTestCase{
  318. Object: base.ManagerApiExpect(),
  319. Method: http.MethodDelete,
  320. Path: "/apisix/admin/routes/r1",
  321. Headers: map[string]string{"Authorization": base.GetToken()},
  322. ExpectStatus: http.StatusOK,
  323. })
  324. })
  325. It("hit the route just deleted", func() {
  326. base.RunTestCase(base.HttpTestCase{
  327. Object: base.APISIXExpect(),
  328. Method: http.MethodGet,
  329. Path: "/hello",
  330. ExpectStatus: http.StatusNotFound,
  331. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  332. Sleep: base.SleepTime,
  333. })
  334. })
  335. })
  336. var _ = Describe("Route_Online_Debug_Route_With_Basic_Auth", func() {
  337. It("hit route that not exist", func() {
  338. base.RunTestCase(base.HttpTestCase{
  339. Object: base.APISIXExpect(),
  340. Method: http.MethodGet,
  341. Path: "/hello",
  342. ExpectStatus: http.StatusNotFound,
  343. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  344. })
  345. })
  346. It("create route enable basic-auth plugin", func() {
  347. var routeBody map[string]interface{} = map[string]interface{}{
  348. "name": "route1",
  349. "uri": "/hello",
  350. "plugins": map[string]interface{}{
  351. "basic-auth": map[string]interface{}{},
  352. },
  353. "methods": []string{"GET"},
  354. "upstream": upstream,
  355. }
  356. _reqRouteBody, err := json.Marshal(routeBody)
  357. Expect(err).To(BeNil())
  358. base.RunTestCase(base.HttpTestCase{
  359. Object: base.ManagerApiExpect(),
  360. Method: http.MethodPut,
  361. Path: "/apisix/admin/routes/r1",
  362. Body: string(_reqRouteBody),
  363. Headers: map[string]string{"Authorization": base.GetToken()},
  364. ExpectStatus: http.StatusOK,
  365. Sleep: base.SleepTime,
  366. })
  367. })
  368. It("make sure the consumer is not created", func() {
  369. base.RunTestCase(base.HttpTestCase{
  370. Object: base.ManagerApiExpect(),
  371. Method: http.MethodGet,
  372. Path: "/apisix/admin/consumers/jack",
  373. Headers: map[string]string{"Authorization": base.GetToken()},
  374. ExpectStatus: http.StatusNotFound,
  375. })
  376. })
  377. It("create consumer", func() {
  378. base.RunTestCase(base.HttpTestCase{
  379. Object: base.ManagerApiExpect(),
  380. Method: http.MethodPut,
  381. Path: "/apisix/admin/consumers",
  382. Body: `{
  383. "username": "jack",
  384. "plugins": {
  385. "basic-auth": {
  386. "disable": false,
  387. "username": "jack",
  388. "password": "123456"
  389. }
  390. },
  391. "desc": "test description"
  392. }`,
  393. Headers: map[string]string{"Authorization": base.GetToken()},
  394. ExpectStatus: http.StatusOK,
  395. Sleep: base.SleepTime,
  396. })
  397. })
  398. It("online debug with basic-auth", func() {
  399. base.RunTestCase(base.HttpTestCase{
  400. Object: base.ManagerApiExpect(),
  401. Method: http.MethodPost,
  402. Path: "/apisix/admin/debug-request-forwarding",
  403. Headers: map[string]string{
  404. "Authorization": base.GetToken(),
  405. "online_debug_url": base.APISIXInternalUrl + `/hello`,
  406. "online_debug_request_protocol": "http",
  407. "online_debug_method": "GET",
  408. "Content-Type": "multipart/form-data",
  409. "online_debug_header_params": `{"test":["test1"],"Authorization": ["Basic amFjazoxMjM0NTYKIA=="]}`,
  410. },
  411. ExpectStatus: http.StatusOK,
  412. ExpectBody: `{"code":0,"message":"","data":{"code":200,"header":{"Connection":["keep-alive"],"Content-Type":["application/octet-stream"]`,
  413. })
  414. })
  415. It("online debug without basic-auth", func() {
  416. base.RunTestCase(base.HttpTestCase{
  417. Object: base.ManagerApiExpect(),
  418. Method: http.MethodPost,
  419. Path: "/apisix/admin/debug-request-forwarding",
  420. Headers: map[string]string{
  421. "Authorization": base.GetToken(),
  422. "online_debug_url": base.APISIXInternalUrl + `/hello`,
  423. "online_debug_request_protocol": "http",
  424. "online_debug_method": "GET",
  425. "Content-Type": "multipart/form-data",
  426. "online_debug_header_params": `{"test":["test1"]}`,
  427. },
  428. ExpectStatus: http.StatusOK,
  429. ExpectBody: `{"code":0,"message":"","data":{"code":401,"header":{"Connection":["keep-alive"],"Content-Type":["text/plain; charset=utf-8"],`,
  430. })
  431. })
  432. It("delete the route just created", func() {
  433. base.RunTestCase(base.HttpTestCase{
  434. Object: base.ManagerApiExpect(),
  435. Method: http.MethodDelete,
  436. Path: "/apisix/admin/routes/r1",
  437. Headers: map[string]string{"Authorization": base.GetToken()},
  438. ExpectStatus: http.StatusOK,
  439. })
  440. })
  441. It("hit the route just deleted", func() {
  442. base.RunTestCase(base.HttpTestCase{
  443. Object: base.APISIXExpect(),
  444. Method: http.MethodGet,
  445. Path: "/hello",
  446. ExpectStatus: http.StatusNotFound,
  447. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  448. Sleep: base.SleepTime,
  449. })
  450. })
  451. It("delete consumer", func() {
  452. base.RunTestCase(base.HttpTestCase{
  453. Object: base.ManagerApiExpect(),
  454. Method: http.MethodDelete,
  455. Path: "/apisix/admin/consumers/jack",
  456. Headers: map[string]string{"Authorization": base.GetToken()},
  457. ExpectStatus: http.StatusOK,
  458. })
  459. })
  460. })
  461. var _ = Describe("Route_Online_Debug_Route_With_Key_Auth", func() {
  462. It("hit route that not exist", func() {
  463. base.RunTestCase(base.HttpTestCase{
  464. Object: base.APISIXExpect(),
  465. Method: http.MethodGet,
  466. Path: "/hello",
  467. ExpectStatus: http.StatusNotFound,
  468. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  469. })
  470. })
  471. It("create route enable key-auth plugin", func() {
  472. var routeBody map[string]interface{} = map[string]interface{}{
  473. "name": "route1",
  474. "uri": "/hello",
  475. "plugins": map[string]interface{}{
  476. "key-auth": map[string]interface{}{},
  477. },
  478. "methods": []string{"GET"},
  479. "upstream": upstream,
  480. }
  481. _reqRouteBody, err := json.Marshal(routeBody)
  482. Expect(err).To(BeNil())
  483. base.RunTestCase(base.HttpTestCase{
  484. Object: base.ManagerApiExpect(),
  485. Method: http.MethodPut,
  486. Path: "/apisix/admin/routes/r1",
  487. Body: string(_reqRouteBody),
  488. Headers: map[string]string{"Authorization": base.GetToken()},
  489. ExpectStatus: http.StatusOK,
  490. Sleep: base.SleepTime,
  491. })
  492. })
  493. It("make sure the consumer is not created", func() {
  494. base.RunTestCase(base.HttpTestCase{
  495. Object: base.ManagerApiExpect(),
  496. Method: http.MethodGet,
  497. Path: "/apisix/admin/consumers/jack",
  498. Headers: map[string]string{"Authorization": base.GetToken()},
  499. ExpectStatus: http.StatusNotFound,
  500. })
  501. })
  502. It("create consumer", func() {
  503. base.RunTestCase(base.HttpTestCase{
  504. Object: base.ManagerApiExpect(),
  505. Method: http.MethodPut,
  506. Path: "/apisix/admin/consumers",
  507. Body: `{
  508. "username": "jack",
  509. "plugins": {
  510. "key-auth": {
  511. "key": "user-key"
  512. }
  513. },
  514. "desc": "test description"
  515. }`,
  516. Headers: map[string]string{"Authorization": base.GetToken()},
  517. ExpectStatus: http.StatusOK,
  518. })
  519. })
  520. It("online debug with key-auth", func() {
  521. base.RunTestCase(base.HttpTestCase{
  522. Object: base.ManagerApiExpect(),
  523. Method: http.MethodPost,
  524. Path: "/apisix/admin/debug-request-forwarding",
  525. Headers: map[string]string{
  526. "Authorization": base.GetToken(),
  527. "online_debug_url": base.APISIXInternalUrl + `/hello`,
  528. "online_debug_request_protocol": "http",
  529. "online_debug_method": "GET",
  530. "Content-Type": "multipart/form-data",
  531. "online_debug_header_params": `{"test":["test1"],"apikey":["user-key"]}`,
  532. },
  533. ExpectStatus: http.StatusOK,
  534. ExpectBody: `{"code":0,"message":"","data":{"code":200,"header":{"Connection":["keep-alive"],"Content-Type":["application/octet-stream"],`,
  535. })
  536. })
  537. It("online debug without key-auth", func() {
  538. base.RunTestCase(base.HttpTestCase{
  539. Object: base.ManagerApiExpect(),
  540. Method: http.MethodPost,
  541. Path: "/apisix/admin/debug-request-forwarding",
  542. Headers: map[string]string{
  543. "Authorization": base.GetToken(),
  544. "online_debug_url": base.APISIXInternalUrl + `/hello`,
  545. "online_debug_request_protocol": "http",
  546. "online_debug_method": "GET",
  547. "Content-Type": "multipart/form-data",
  548. "online_debug_header_params": `{"test":["test1"]}`,
  549. },
  550. ExpectStatus: http.StatusOK,
  551. ExpectBody: `{"code":0,"message":"","data":{"code":401,"header":{"Connection":["keep-alive"],"Content-Type":["text/plain; charset=utf-8"]`,
  552. })
  553. })
  554. It("delete the route just created", func() {
  555. base.RunTestCase(base.HttpTestCase{
  556. Object: base.ManagerApiExpect(),
  557. Method: http.MethodDelete,
  558. Path: "/apisix/admin/routes/r1",
  559. Headers: map[string]string{"Authorization": base.GetToken()},
  560. ExpectStatus: http.StatusOK,
  561. })
  562. })
  563. It("hit the route just deleted", func() {
  564. base.RunTestCase(base.HttpTestCase{
  565. Object: base.APISIXExpect(),
  566. Method: http.MethodGet,
  567. Path: "/hello",
  568. ExpectStatus: http.StatusNotFound,
  569. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  570. Sleep: base.SleepTime,
  571. })
  572. })
  573. It("delete consumer", func() {
  574. base.RunTestCase(base.HttpTestCase{
  575. Object: base.ManagerApiExpect(),
  576. Method: http.MethodDelete,
  577. Path: "/apisix/admin/consumers/jack",
  578. Headers: map[string]string{"Authorization": base.GetToken()},
  579. ExpectStatus: http.StatusOK,
  580. })
  581. })
  582. })
  583. var _ = Describe("Route_Online_Debug_Route_With_JWT_Auth", func() {
  584. It("hit route that not exist", func() {
  585. base.RunTestCase(base.HttpTestCase{
  586. Object: base.APISIXExpect(),
  587. Method: http.MethodGet,
  588. Path: "/hello",
  589. ExpectStatus: http.StatusNotFound,
  590. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  591. })
  592. })
  593. It("create route enable jwt-auth plugin", func() {
  594. var routeBody map[string]interface{} = map[string]interface{}{
  595. "name": "route1",
  596. "uri": "/hello",
  597. "plugins": map[string]interface{}{
  598. "jwt-auth": map[string]interface{}{},
  599. },
  600. "methods": []string{"GET"},
  601. "upstream": upstream,
  602. }
  603. _reqRouteBody, err := json.Marshal(routeBody)
  604. Expect(err).To(BeNil())
  605. base.RunTestCase(base.HttpTestCase{
  606. Object: base.ManagerApiExpect(),
  607. Method: http.MethodPut,
  608. Path: "/apisix/admin/routes/r1",
  609. Body: string(_reqRouteBody),
  610. Headers: map[string]string{"Authorization": base.GetToken()},
  611. ExpectStatus: http.StatusOK,
  612. Sleep: base.SleepTime,
  613. })
  614. })
  615. It("make sure the consumer is not created", func() {
  616. base.RunTestCase(base.HttpTestCase{
  617. Object: base.ManagerApiExpect(),
  618. Method: http.MethodGet,
  619. Path: "/apisix/admin/consumers/jack",
  620. Headers: map[string]string{"Authorization": base.GetToken()},
  621. ExpectStatus: http.StatusNotFound,
  622. })
  623. })
  624. It("create consumer", func() {
  625. base.RunTestCase(base.HttpTestCase{
  626. Object: base.ManagerApiExpect(),
  627. Method: http.MethodPut,
  628. Path: "/apisix/admin/consumers",
  629. Body: `{
  630. "username": "jack",
  631. "plugins": {
  632. "jwt-auth": {
  633. "key": "user-key",
  634. "secret": "my-secret-key",
  635. "algorithm": "HS256"
  636. }
  637. },
  638. "desc": "test description"
  639. }`,
  640. Headers: map[string]string{"Authorization": base.GetToken()},
  641. ExpectStatus: http.StatusOK,
  642. })
  643. })
  644. It("online debug with JWT-auth", func() {
  645. jsonStr := `{"test":["test1"]}`
  646. var _headerParams map[string]interface{}
  647. err := json.Unmarshal([]byte(jsonStr), &_headerParams)
  648. Expect(err).To(BeNil())
  649. jwtToken := base.GetJwtToken("user-key")
  650. l := []string{jwtToken}
  651. _headerParams["Authorization"] = l
  652. headerParams, err := json.Marshal(_headerParams)
  653. Expect(err).To(BeNil())
  654. base.RunTestCase(base.HttpTestCase{
  655. Object: base.ManagerApiExpect(),
  656. Method: http.MethodPost,
  657. Path: "/apisix/admin/debug-request-forwarding",
  658. Headers: map[string]string{
  659. "Authorization": base.GetToken(),
  660. "online_debug_url": base.APISIXInternalUrl + `/hello`,
  661. "online_debug_request_protocol": "http",
  662. "online_debug_method": "GET",
  663. "Content-Type": "multipart/form-data",
  664. "online_debug_header_params": string(headerParams),
  665. },
  666. ExpectStatus: http.StatusOK,
  667. ExpectBody: `{"code":0,"message":"","data":{"code":200,"header":{"Connection":["keep-alive"],"Content-Type":["application/octet-stream"],`,
  668. })
  669. })
  670. It("online debug without JWT-auth", func() {
  671. base.RunTestCase(base.HttpTestCase{
  672. Object: base.ManagerApiExpect(),
  673. Method: http.MethodPost,
  674. Path: "/apisix/admin/debug-request-forwarding",
  675. Headers: map[string]string{
  676. "Authorization": base.GetToken(),
  677. "online_debug_url": base.APISIXInternalUrl + `/hello`,
  678. "online_debug_request_protocol": "http",
  679. "online_debug_method": "GET",
  680. "Content-Type": "multipart/form-data",
  681. "online_debug_header_params": `{"test":["test1"]}`,
  682. },
  683. ExpectStatus: http.StatusOK,
  684. ExpectBody: `{"code":0,"message":"","data":{"code":401,"header":{"Connection":["keep-alive"],"Content-Type":["text/plain; charset=utf-8"],`,
  685. })
  686. })
  687. It("delete the route just created", func() {
  688. base.RunTestCase(base.HttpTestCase{
  689. Object: base.ManagerApiExpect(),
  690. Method: http.MethodDelete,
  691. Path: "/apisix/admin/routes/r1",
  692. Headers: map[string]string{"Authorization": base.GetToken()},
  693. ExpectStatus: http.StatusOK,
  694. })
  695. })
  696. It("hit the route just deleted", func() {
  697. base.RunTestCase(base.HttpTestCase{
  698. Object: base.APISIXExpect(),
  699. Method: http.MethodGet,
  700. Path: "/hello",
  701. ExpectStatus: http.StatusNotFound,
  702. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  703. Sleep: base.SleepTime,
  704. })
  705. })
  706. It("delete consumer", func() {
  707. base.RunTestCase(base.HttpTestCase{
  708. Object: base.ManagerApiExpect(),
  709. Method: http.MethodDelete,
  710. Path: "/apisix/admin/consumers/jack",
  711. Headers: map[string]string{"Authorization": base.GetToken()},
  712. ExpectStatus: http.StatusOK,
  713. })
  714. })
  715. })
  716. var _ = Describe("Route_Online_Debug_Route_With_Files", func() {
  717. It("hit route that not exist", func() {
  718. base.RunTestCase(base.HttpTestCase{
  719. Object: base.APISIXExpect(),
  720. Method: http.MethodGet,
  721. Path: "/hello",
  722. ExpectStatus: http.StatusNotFound,
  723. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  724. })
  725. })
  726. It("create route enable basic-auth plugin", func() {
  727. var routeBody map[string]interface{} = map[string]interface{}{
  728. "name": "route1",
  729. "uri": "/hello_",
  730. "methods": []string{"POST"},
  731. "upstream": upstream,
  732. }
  733. _reqRouteBody, err := json.Marshal(routeBody)
  734. Expect(err).To(BeNil())
  735. base.RunTestCase(base.HttpTestCase{
  736. Object: base.ManagerApiExpect(),
  737. Method: http.MethodPut,
  738. Path: "/apisix/admin/routes/r2",
  739. Body: string(_reqRouteBody),
  740. Headers: map[string]string{"Authorization": base.GetToken()},
  741. ExpectStatus: http.StatusOK,
  742. Sleep: base.SleepTime,
  743. })
  744. })
  745. It("online debug with file", func() {
  746. path, err := filepath.Abs("../../testdata/import/default.yaml")
  747. Expect(err).To(BeNil())
  748. files := []base.UploadFile{
  749. {Name: "file", Filepath: path},
  750. }
  751. headers := map[string]string{}
  752. jsonStr := `{"test":["test1"]}`
  753. var _headerParams map[string]interface{}
  754. err = json.Unmarshal([]byte(jsonStr), &_headerParams)
  755. Expect(err).To(BeNil())
  756. l := []string{base.GetToken()}
  757. _headerParams["Authorization"] = l
  758. headerParams, err := json.Marshal(_headerParams)
  759. Expect(err).To(BeNil())
  760. basePath := base.ManagerAPIHost + "/apisix/admin/debug-request-forwarding"
  761. requestBody, requestContentType, err := base.GetReader(headers, "multipart/form-data", files)
  762. Expect(err).To(BeNil())
  763. httpRequest, err := http.NewRequest(http.MethodPost, basePath, requestBody)
  764. Expect(err).To(BeNil())
  765. httpRequest.Header.Add("Content-Type", requestContentType)
  766. httpRequest.Header.Add("Authorization", base.GetToken())
  767. httpRequest.Header.Add("online_debug_request_protocol", "http")
  768. httpRequest.Header.Add("online_debug_url", base.ManagerAPIHost+`/apisix/admin/import/routes`)
  769. httpRequest.Header.Add("online_debug_method", http.MethodPost)
  770. httpRequest.Header.Add("online_debug_header_params", string(headerParams))
  771. client := &http.Client{}
  772. resp, err := client.Do(httpRequest)
  773. Expect(err).To(BeNil())
  774. defer resp.Body.Close()
  775. respBody, err := ioutil.ReadAll(resp.Body)
  776. Expect(err).To(BeNil())
  777. realBody := gjson.Get(string(respBody), "data")
  778. // todo get successful result and compare
  779. Expect(realBody.String()).Should(ContainSubstring(`"data":{"paths":1,"routes":1}`))
  780. })
  781. It("verify the route just imported and delete data", func() {
  782. time.Sleep(time.Duration(500) * time.Millisecond)
  783. request, _ := http.NewRequest("GET", base.ManagerAPIHost+"/apisix/admin/routes", nil)
  784. request.Header.Add("Authorization", base.GetToken())
  785. resp, err := http.DefaultClient.Do(request)
  786. Expect(err).To(BeNil())
  787. defer resp.Body.Close()
  788. respBody, _ := ioutil.ReadAll(resp.Body)
  789. list := gjson.Get(string(respBody), "data.rows").Value().([]interface{})
  790. var tests []base.HttpTestCase
  791. for _, item := range list {
  792. route := item.(map[string]interface{})
  793. tc := base.HttpTestCase{
  794. Desc: "route patch for update status(online)",
  795. Object: base.ManagerApiExpect(),
  796. Method: http.MethodPatch,
  797. Path: "/apisix/admin/routes/" + route["id"].(string),
  798. Body: `{"status":1}`,
  799. Headers: map[string]string{"Authorization": base.GetToken()},
  800. ExpectStatus: http.StatusOK,
  801. Sleep: base.SleepTime,
  802. }
  803. tests = append(tests, tc)
  804. }
  805. // verify route
  806. tests = append(tests, base.HttpTestCase{
  807. Desc: "verify the route just imported",
  808. Object: base.APISIXExpect(),
  809. Method: http.MethodGet,
  810. Path: "/hello",
  811. ExpectStatus: http.StatusOK,
  812. ExpectBody: "hello world",
  813. Sleep: base.SleepTime,
  814. })
  815. // delete test data
  816. for _, item := range list {
  817. route := item.(map[string]interface{})
  818. tc := base.HttpTestCase{
  819. Desc: "delete route",
  820. Object: base.ManagerApiExpect(),
  821. Method: http.MethodDelete,
  822. Path: "/apisix/admin/routes/" + route["id"].(string),
  823. Headers: map[string]string{"Authorization": base.GetToken()},
  824. ExpectStatus: http.StatusOK,
  825. }
  826. tests = append(tests, tc)
  827. }
  828. for _, tc := range tests {
  829. base.RunTestCase(tc)
  830. }
  831. })
  832. It("hit the route just deleted", func() {
  833. base.RunTestCase(base.HttpTestCase{
  834. Object: base.APISIXExpect(),
  835. Method: http.MethodGet,
  836. Path: "/hello",
  837. ExpectStatus: http.StatusNotFound,
  838. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  839. Sleep: base.SleepTime,
  840. })
  841. })
  842. It("hit the route just deleted", func() {
  843. base.RunTestCase(base.HttpTestCase{
  844. Object: base.APISIXExpect(),
  845. Method: http.MethodGet,
  846. Path: "/hello_",
  847. ExpectStatus: http.StatusNotFound,
  848. ExpectBody: "{\"error_msg\":\"404 Route Not Found\"}\n",
  849. Sleep: base.SleepTime,
  850. })
  851. })
  852. })