You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

199 lines
6.3 KiB

  1. import expect from "expect"
  2. import Im from "immutable"
  3. import curl from "core/curlify"
  4. import win from "core/window"
  5. describe("curlify", function() {
  6. it("prints a curl statement with custom content-type", function() {
  7. var req = {
  8. url: "http://example.com",
  9. method: "POST",
  10. body: {
  11. id: 0,
  12. name: "doggie",
  13. status: "available"
  14. },
  15. headers: {
  16. Accept: "application/json",
  17. "content-type": "application/json"
  18. }
  19. }
  20. let curlified = curl(Im.fromJS(req))
  21. 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\"}")
  22. })
  23. it("does not change the case of header in curl", function() {
  24. var req = {
  25. url: "http://example.com",
  26. method: "POST",
  27. headers: {
  28. "conTenT Type": "application/Moar"
  29. }
  30. }
  31. let curlified = curl(Im.fromJS(req))
  32. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"conTenT Type: application/Moar\"")
  33. })
  34. it("prints a curl statement with an array of query params", function() {
  35. var req = {
  36. url: "http://swaggerhub.com/v1/one?name=john|smith",
  37. method: "GET"
  38. }
  39. let curlified = curl(Im.fromJS(req))
  40. expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\"")
  41. })
  42. it("prints a curl statement with an array of query params and auth", function() {
  43. var req = {
  44. url: "http://swaggerhub.com/v1/one?name=john|smith",
  45. method: "GET",
  46. headers: {
  47. authorization: "Basic Zm9vOmJhcg=="
  48. }
  49. }
  50. let curlified = curl(Im.fromJS(req))
  51. expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"authorization: Basic Zm9vOmJhcg==\"")
  52. })
  53. it("prints a curl statement with html", function() {
  54. var req = {
  55. url: "http://swaggerhub.com/v1/one?name=john|smith",
  56. method: "GET",
  57. headers: {
  58. accept: "application/json"
  59. },
  60. body: {
  61. description: "<b>Test</b>"
  62. }
  63. }
  64. let curlified = curl(Im.fromJS(req))
  65. expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"accept: application/json\" -d {\"description\":\"<b>Test</b>\"}")
  66. })
  67. it("handles post body with html", function() {
  68. var req = {
  69. url: "http://swaggerhub.com/v1/one?name=john|smith",
  70. method: "POST",
  71. headers: {
  72. accept: "application/json"
  73. },
  74. body: {
  75. description: "<b>Test</b>"
  76. }
  77. }
  78. let curlified = curl(Im.fromJS(req))
  79. expect(curlified).toEqual("curl -X POST \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"accept: application/json\" -d {\"description\":\"<b>Test</b>\"}")
  80. })
  81. it("handles post body with special chars", function() {
  82. var req = {
  83. url: "http://swaggerhub.com/v1/one?name=john|smith",
  84. method: "POST",
  85. body: {
  86. description: "@prefix nif:<http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .\n" +
  87. "@prefix itsrdf: <http://www.w3.org/2005/11/its/rdf#> ."
  88. }
  89. }
  90. let curlified = curl(Im.fromJS(req))
  91. 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#> .\"}")
  92. })
  93. it("handles delete form with parameters", function() {
  94. var req = {
  95. url: "http://example.com",
  96. method: "DELETE",
  97. headers: {
  98. accept: "application/x-www-form-urlencoded"
  99. }
  100. }
  101. let curlified = curl(Im.fromJS(req))
  102. expect(curlified).toEqual("curl -X DELETE \"http://example.com\" -H \"accept: application/x-www-form-urlencoded\"")
  103. })
  104. it("should print a curl with formData", function() {
  105. var req = {
  106. url: "http://example.com",
  107. method: "POST",
  108. headers: { "content-type": "multipart/form-data" },
  109. body: {
  110. id: "123",
  111. name: "Sahar"
  112. }
  113. }
  114. let curlified = curl(Im.fromJS(req))
  115. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"name=Sahar\"")
  116. })
  117. it("should print a curl with formData and file", function() {
  118. var file = new win.File()
  119. file.name = "file.txt"
  120. file.type = "text/plain"
  121. var req = {
  122. url: "http://example.com",
  123. method: "POST",
  124. headers: { "content-type": "multipart/form-data" },
  125. body: {
  126. id: "123",
  127. file
  128. }
  129. }
  130. let curlified = curl(Im.fromJS(req))
  131. 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\"")
  132. })
  133. it("prints a curl post statement from an object", function() {
  134. var req = {
  135. url: "http://example.com",
  136. method: "POST",
  137. headers: {
  138. accept: "application/json"
  139. },
  140. body: {
  141. id: 10101
  142. }
  143. }
  144. let curlified = curl(Im.fromJS(req))
  145. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d {\"id\":10101}")
  146. })
  147. it("prints a curl post statement from a string containing a single quote", function() {
  148. var req = {
  149. url: "http://example.com",
  150. method: "POST",
  151. headers: {
  152. accept: "application/json"
  153. },
  154. body: "{\"id\":\"foo'bar\"}"
  155. }
  156. let curlified = curl(Im.fromJS(req))
  157. expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d \"{\\\"id\\\":\\\"foo'bar\\\"}\"")
  158. })
  159. })