curlify.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import Im from "immutable"
  2. import curl from "core/curlify"
  3. import win from "core/window"
  4. describe("curlify", function () {
  5. it("prints a curl statement with custom content-type", function () {
  6. let req = {
  7. url: "http://example.com",
  8. method: "POST",
  9. body: {
  10. id: 0,
  11. name: "doggie",
  12. status: "available"
  13. },
  14. headers: {
  15. Accept: "application/json",
  16. "content-type": "application/json"
  17. }
  18. }
  19. let curlified = curl(Im.fromJS(req))
  20. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"Accept: application/json\" -H \"content-type: application/json\" -d {\"id\":0,\"name\":\"doggie\",\"status\":\"available\"}")
  21. })
  22. it("does add a empty data param if no request body given", function () {
  23. let req = {
  24. url: "http://example.com",
  25. method: "POST",
  26. }
  27. let curlified = curl(Im.fromJS(req))
  28. expect(curlified).toEqual("curl -X POST \"http://example.com\" -d \"\"")
  29. })
  30. it("does not change the case of header in curl", function () {
  31. let req = {
  32. url: "http://example.com",
  33. method: "POST",
  34. headers: {
  35. "conTenT Type": "application/Moar"
  36. }
  37. }
  38. let curlified = curl(Im.fromJS(req))
  39. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"conTenT Type: application/Moar\" -d \"\"")
  40. })
  41. it("prints a curl statement with an array of query params", function () {
  42. let req = {
  43. url: "http://swaggerhub.com/v1/one?name=john|smith",
  44. method: "GET"
  45. }
  46. let curlified = curl(Im.fromJS(req))
  47. expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\"")
  48. })
  49. it("prints a curl statement with an array of query params and auth", function () {
  50. let req = {
  51. url: "http://swaggerhub.com/v1/one?name=john|smith",
  52. method: "GET",
  53. headers: {
  54. authorization: "Basic Zm9vOmJhcg=="
  55. }
  56. }
  57. let curlified = curl(Im.fromJS(req))
  58. expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"authorization: Basic Zm9vOmJhcg==\"")
  59. })
  60. it("prints a curl statement with html", function () {
  61. let req = {
  62. url: "http://swaggerhub.com/v1/one?name=john|smith",
  63. method: "GET",
  64. headers: {
  65. accept: "application/json"
  66. },
  67. body: {
  68. description: "<b>Test</b>"
  69. }
  70. }
  71. let curlified = curl(Im.fromJS(req))
  72. expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"accept: application/json\" -d {\"description\":\"<b>Test</b>\"}")
  73. })
  74. it("handles post body with html", function () {
  75. let req = {
  76. url: "http://swaggerhub.com/v1/one?name=john|smith",
  77. method: "POST",
  78. headers: {
  79. accept: "application/json"
  80. },
  81. body: {
  82. description: "<b>Test</b>"
  83. }
  84. }
  85. let curlified = curl(Im.fromJS(req))
  86. expect(curlified).toEqual("curl -X POST \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"accept: application/json\" -d {\"description\":\"<b>Test</b>\"}")
  87. })
  88. it("handles post body with special chars", function () {
  89. let req = {
  90. url: "http://swaggerhub.com/v1/one?name=john|smith",
  91. method: "POST",
  92. body: {
  93. description: "@prefix nif:<http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .\n" +
  94. "@prefix itsrdf: <http://www.w3.org/2005/11/its/rdf#> ."
  95. }
  96. }
  97. let curlified = curl(Im.fromJS(req))
  98. expect(curlified).toEqual("curl -X POST \"http://swaggerhub.com/v1/one?name=john|smith\" -d {\"description\":\"@prefix nif:<http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .@prefix itsrdf: <http://www.w3.org/2005/11/its/rdf#> .\"}")
  99. })
  100. it("handles delete form with parameters", function () {
  101. let req = {
  102. url: "http://example.com",
  103. method: "DELETE",
  104. headers: {
  105. accept: "application/x-www-form-urlencoded"
  106. }
  107. }
  108. let curlified = curl(Im.fromJS(req))
  109. expect(curlified).toEqual("curl -X DELETE \"http://example.com\" -H \"accept: application/x-www-form-urlencoded\"")
  110. })
  111. it("should print a curl with formData", function () {
  112. let req = {
  113. url: "http://example.com",
  114. method: "POST",
  115. headers: { "content-type": "multipart/form-data" },
  116. body: {
  117. id: "123",
  118. name: "Sahar"
  119. }
  120. }
  121. let curlified = curl(Im.fromJS(req))
  122. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"name=Sahar\"")
  123. })
  124. it("should print a curl with formData that extracts array representation with hashIdx", function () {
  125. // Note: hashIdx = `_**[]${counter}`
  126. // Usage of hashIdx is an internal SwaggerUI method to convert formData array into something curlify can handle
  127. const req = {
  128. url: "http://example.com",
  129. method: "POST",
  130. headers: { "content-type": "multipart/form-data" },
  131. body: {
  132. id: "123",
  133. "fruits[]_**[]1": "apple",
  134. "fruits[]_**[]2": "banana",
  135. "fruits[]_**[]3": "grape"
  136. }
  137. }
  138. let curlified = curl(Im.fromJS(req))
  139. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"fruits[]=apple\" -F \"fruits[]=banana\" -F \"fruits[]=grape\"")
  140. })
  141. it("should print a curl with formData and file", function () {
  142. let file = new win.File([""], "file.txt", { type: "text/plain" })
  143. // file.name = "file.txt"
  144. // file.type = "text/plain"
  145. let req = {
  146. url: "http://example.com",
  147. method: "POST",
  148. headers: { "content-type": "multipart/form-data" },
  149. body: {
  150. id: "123",
  151. file
  152. }
  153. }
  154. let curlified = curl(Im.fromJS(req))
  155. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"file=@file.txt;type=text/plain\"")
  156. })
  157. it("should print a curl without form data type if type is unknown", function () {
  158. let file = new win.File([""], "file.txt", { type: "" })
  159. // file.name = "file.txt"
  160. // file.type = ""
  161. let req = {
  162. url: "http://example.com",
  163. method: "POST",
  164. headers: { "content-type": "multipart/form-data" },
  165. body: {
  166. id: "123",
  167. file
  168. }
  169. }
  170. let curlified = curl(Im.fromJS(req))
  171. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"file=@file.txt\"")
  172. })
  173. it("prints a curl post statement from an object", function () {
  174. let req = {
  175. url: "http://example.com",
  176. method: "POST",
  177. headers: {
  178. accept: "application/json"
  179. },
  180. body: {
  181. id: 10101
  182. }
  183. }
  184. let curlified = curl(Im.fromJS(req))
  185. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d {\"id\":10101}")
  186. })
  187. it("prints a curl post statement from a string containing a single quote", function () {
  188. let req = {
  189. url: "http://example.com",
  190. method: "POST",
  191. headers: {
  192. accept: "application/json"
  193. },
  194. body: "{\"id\":\"foo'bar\"}"
  195. }
  196. let curlified = curl(Im.fromJS(req))
  197. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d \"{\\\"id\\\":\\\"foo'bar\\\"}\"")
  198. })
  199. describe("given multiple entries with file", function () {
  200. describe("and with leading custom header", function () {
  201. it("should print a proper curl -F", function () {
  202. let file = new win.File([""], "file.txt", { type: "text/plain" })
  203. // file.name = "file.txt"
  204. // file.type = "text/plain"
  205. let req = {
  206. url: "http://example.com",
  207. method: "POST",
  208. headers: {
  209. "x-custom-name": "multipart/form-data",
  210. "content-type": "multipart/form-data"
  211. },
  212. body: {
  213. id: "123",
  214. file
  215. }
  216. }
  217. let curlified = curl(Im.fromJS(req))
  218. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"x-custom-name: multipart/form-data\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"file=@file.txt;type=text/plain\"")
  219. })
  220. })
  221. describe("and with trailing custom header; e.g. from requestInterceptor appending req.headers", function () {
  222. it("should print a proper curl -F", function () {
  223. let file = new win.File([""], "file.txt", { type: "text/plain" })
  224. // file.name = "file.txt"
  225. // file.type = "text/plain"
  226. let req = {
  227. url: "http://example.com",
  228. method: "POST",
  229. headers: {
  230. "content-type": "multipart/form-data",
  231. "x-custom-name": "any-value"
  232. },
  233. body: {
  234. id: "123",
  235. file
  236. }
  237. }
  238. let curlified = curl(Im.fromJS(req))
  239. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -H \"x-custom-name: any-value\" -F \"id=123\" -F \"file=@file.txt;type=text/plain\"")
  240. })
  241. })
  242. })
  243. describe("POST when header value is 'multipart/form-data' but header name is not 'content-type'", function () {
  244. it("shoud print a proper curl as -d <data>, when file type is provided", function () {
  245. let file = new win.File([""], "file.txt", { type: "text/plain" })
  246. // file.name = "file.txt"
  247. // file.type = "text/plain"
  248. let req = {
  249. url: "http://example.com",
  250. method: "POST",
  251. headers: { "x-custom-name": "multipart/form-data" },
  252. body: {
  253. id: "123",
  254. file
  255. }
  256. }
  257. let curlified = curl(Im.fromJS(req))
  258. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"x-custom-name: multipart/form-data\" -d {\"id\":\"123\",\"file\":{\"name\":\"file.txt\",\"type\":\"text/plain\"}}")
  259. })
  260. it("shoud print a proper curl as -d <data>, no file type provided", function () {
  261. let file = new win.File([""], "file.txt")
  262. // file.name = "file.txt"
  263. // file.type = "text/plain"
  264. let req = {
  265. url: "http://example.com",
  266. method: "POST",
  267. headers: { "x-custom-name": "multipart/form-data" },
  268. body: {
  269. id: "123",
  270. file
  271. }
  272. }
  273. let curlified = curl(Im.fromJS(req))
  274. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"x-custom-name: multipart/form-data\" -d {\"id\":\"123\",\"file\":{\"name\":\"file.txt\"}}")
  275. })
  276. })
  277. it("should escape dollar signs in headers and request body", function () {
  278. let req = {
  279. url: "http://example.com",
  280. method: "POST",
  281. headers: { "X-DOLLAR": "token/123$" },
  282. body: "CREATE ($props)"
  283. }
  284. let curlified = curl(Im.fromJS(req))
  285. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"X-DOLLAR: token/123\\$\" -d \"CREATE (\\$props)\"")
  286. })
  287. it("should escape multiple dollar signs", function () {
  288. let req = {
  289. url: "http://example.com",
  290. method: "POST",
  291. headers: { },
  292. body: "RETURN $x + $y"
  293. }
  294. let curlified = curl(Im.fromJS(req))
  295. expect(curlified).toEqual("curl -X POST \"http://example.com\" -d \"RETURN \\$x + \\$y\"")
  296. })
  297. it("should include curlOptions from the request in the curl command", function () {
  298. let req = {
  299. url: "http://example.com",
  300. method: "GET",
  301. headers: { "X-DOLLAR": "token/123$" },
  302. curlOptions: ["-g"]
  303. }
  304. let curlified = curl(Im.fromJS(req))
  305. expect(curlified).toEqual("curl -g -X GET \"http://example.com\" -H \"X-DOLLAR: token/123\\$\"")
  306. })
  307. it("should include multiple curlOptions from the request in the curl command", function () {
  308. let req = {
  309. url: "http://example.com",
  310. method: "GET",
  311. headers: { "X-DOLLAR": "token/123$" },
  312. curlOptions: ["-g", "--limit-rate 20k"]
  313. }
  314. let curlified = curl(Im.fromJS(req))
  315. expect(curlified).toEqual("curl -g --limit-rate 20k -X GET \"http://example.com\" -H \"X-DOLLAR: token/123\\$\"")
  316. })
  317. })