123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- import Im from "immutable"
- import curl from "core/curlify"
- import win from "core/window"
- describe("curlify", function () {
- it("prints a curl statement with custom content-type", function () {
- let req = {
- url: "http://example.com",
- method: "POST",
- body: {
- id: 0,
- name: "doggie",
- status: "available"
- },
- headers: {
- Accept: "application/json",
- "content-type": "application/json"
- }
- }
- let curlified = curl(Im.fromJS(req))
- 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\"}")
- })
- it("does add a empty data param if no request body given", function () {
- let req = {
- url: "http://example.com",
- method: "POST",
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -d \"\"")
- })
- it("does not change the case of header in curl", function () {
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: {
- "conTenT Type": "application/Moar"
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"conTenT Type: application/Moar\" -d \"\"")
- })
- it("prints a curl statement with an array of query params", function () {
- let req = {
- url: "http://swaggerhub.com/v1/one?name=john|smith",
- method: "GET"
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\"")
- })
- it("prints a curl statement with an array of query params and auth", function () {
- let req = {
- url: "http://swaggerhub.com/v1/one?name=john|smith",
- method: "GET",
- headers: {
- authorization: "Basic Zm9vOmJhcg=="
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"authorization: Basic Zm9vOmJhcg==\"")
- })
- it("prints a curl statement with html", function () {
- let req = {
- url: "http://swaggerhub.com/v1/one?name=john|smith",
- method: "GET",
- headers: {
- accept: "application/json"
- },
- body: {
- description: "<b>Test</b>"
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X GET \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"accept: application/json\" -d {\"description\":\"<b>Test</b>\"}")
- })
- it("handles post body with html", function () {
- let req = {
- url: "http://swaggerhub.com/v1/one?name=john|smith",
- method: "POST",
- headers: {
- accept: "application/json"
- },
- body: {
- description: "<b>Test</b>"
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://swaggerhub.com/v1/one?name=john|smith\" -H \"accept: application/json\" -d {\"description\":\"<b>Test</b>\"}")
- })
- it("handles post body with special chars", function () {
- let req = {
- url: "http://swaggerhub.com/v1/one?name=john|smith",
- method: "POST",
- body: {
- description: "@prefix nif:<http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .\n" +
- "@prefix itsrdf: <http://www.w3.org/2005/11/its/rdf#> ."
- }
- }
- let curlified = curl(Im.fromJS(req))
- 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#> .\"}")
- })
- it("handles delete form with parameters", function () {
- let req = {
- url: "http://example.com",
- method: "DELETE",
- headers: {
- accept: "application/x-www-form-urlencoded"
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X DELETE \"http://example.com\" -H \"accept: application/x-www-form-urlencoded\"")
- })
- it("should print a curl with formData", function () {
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: { "content-type": "multipart/form-data" },
- body: {
- id: "123",
- name: "Sahar"
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"name=Sahar\"")
- })
- it("should print a curl with formData that extracts array representation with hashIdx", function () {
- // Note: hashIdx = `_**[]${counter}`
- // Usage of hashIdx is an internal SwaggerUI method to convert formData array into something curlify can handle
- const req = {
- url: "http://example.com",
- method: "POST",
- headers: { "content-type": "multipart/form-data" },
- body: {
- id: "123",
- "fruits[]_**[]1": "apple",
- "fruits[]_**[]2": "banana",
- "fruits[]_**[]3": "grape"
- }
- }
- let curlified = curl(Im.fromJS(req))
- 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\"")
- })
- it("should print a curl with formData and file", function () {
- let file = new win.File([""], "file.txt", { type: "text/plain" })
- // file.name = "file.txt"
- // file.type = "text/plain"
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: { "content-type": "multipart/form-data" },
- body: {
- id: "123",
- file
- }
- }
- let curlified = curl(Im.fromJS(req))
- 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\"")
- })
- it("should print a curl without form data type if type is unknown", function () {
- let file = new win.File([""], "file.txt", { type: "" })
- // file.name = "file.txt"
- // file.type = ""
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: { "content-type": "multipart/form-data" },
- body: {
- id: "123",
- file
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"file=@file.txt\"")
- })
- it("prints a curl post statement from an object", function () {
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: {
- accept: "application/json"
- },
- body: {
- id: 10101
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d {\"id\":10101}")
- })
- it("prints a curl post statement from a string containing a single quote", function () {
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: {
- accept: "application/json"
- },
- body: "{\"id\":\"foo'bar\"}"
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d \"{\\\"id\\\":\\\"foo'bar\\\"}\"")
- })
- describe("given multiple entries with file", function () {
- describe("and with leading custom header", function () {
- it("should print a proper curl -F", function () {
- let file = new win.File([""], "file.txt", { type: "text/plain" })
- // file.name = "file.txt"
- // file.type = "text/plain"
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: {
- "x-custom-name": "multipart/form-data",
- "content-type": "multipart/form-data"
- },
- body: {
- id: "123",
- file
- }
- }
- let curlified = curl(Im.fromJS(req))
- 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\"")
- })
- })
- describe("and with trailing custom header; e.g. from requestInterceptor appending req.headers", function () {
- it("should print a proper curl -F", function () {
- let file = new win.File([""], "file.txt", { type: "text/plain" })
- // file.name = "file.txt"
- // file.type = "text/plain"
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: {
- "content-type": "multipart/form-data",
- "x-custom-name": "any-value"
- },
- body: {
- id: "123",
- file
- }
- }
- let curlified = curl(Im.fromJS(req))
- 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\"")
- })
- })
- })
- describe("POST when header value is 'multipart/form-data' but header name is not 'content-type'", function () {
- it("shoud print a proper curl as -d <data>, when file type is provided", function () {
- let file = new win.File([""], "file.txt", { type: "text/plain" })
- // file.name = "file.txt"
- // file.type = "text/plain"
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: { "x-custom-name": "multipart/form-data" },
- body: {
- id: "123",
- file
- }
- }
- let curlified = curl(Im.fromJS(req))
- 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\"}}")
- })
- it("shoud print a proper curl as -d <data>, no file type provided", function () {
- let file = new win.File([""], "file.txt")
- // file.name = "file.txt"
- // file.type = "text/plain"
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: { "x-custom-name": "multipart/form-data" },
- body: {
- id: "123",
- file
- }
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"x-custom-name: multipart/form-data\" -d {\"id\":\"123\",\"file\":{\"name\":\"file.txt\"}}")
- })
- })
- it("should escape dollar signs in headers and request body", function () {
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: { "X-DOLLAR": "token/123$" },
- body: "CREATE ($props)"
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"X-DOLLAR: token/123\\$\" -d \"CREATE (\\$props)\"")
- })
- it("should escape multiple dollar signs", function () {
- let req = {
- url: "http://example.com",
- method: "POST",
- headers: { },
- body: "RETURN $x + $y"
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -X POST \"http://example.com\" -d \"RETURN \\$x + \\$y\"")
- })
- it("should include curlOptions from the request in the curl command", function () {
- let req = {
- url: "http://example.com",
- method: "GET",
- headers: { "X-DOLLAR": "token/123$" },
- curlOptions: ["-g"]
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -g -X GET \"http://example.com\" -H \"X-DOLLAR: token/123\\$\"")
- })
- it("should include multiple curlOptions from the request in the curl command", function () {
- let req = {
- url: "http://example.com",
- method: "GET",
- headers: { "X-DOLLAR": "token/123$" },
- curlOptions: ["-g", "--limit-rate 20k"]
- }
- let curlified = curl(Im.fromJS(req))
- expect(curlified).toEqual("curl -g --limit-rate 20k -X GET \"http://example.com\" -H \"X-DOLLAR: token/123\\$\"")
- })
- })
|