From 19028ed03314350d8c343ff71a50fa1f46dd777b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neuh=C3=A4user?= Date: Mon, 9 Nov 2015 13:52:03 +0100 Subject: [PATCH 1/3] Outputs --data in single quotes --- lib/types/operation.js | 2 +- test/request.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/types/operation.js b/lib/types/operation.js index bce47d7cb..344341121 100644 --- a/lib/types/operation.js +++ b/lib/types/operation.js @@ -876,7 +876,7 @@ Operation.prototype.asCurl = function (args1, args2) { body = obj.body; } - results.push('-d "' + body.replace(/"/g, '\\"') + '"'); + results.push('-d \'' + body.replace(/"/g, '\\"') + '\''); } return 'curl ' + (results.join(' ')) + ' "' + obj.url + '"'; diff --git a/test/request.js b/test/request.js index c75ba85a3..0bccae35a 100644 --- a/test/request.js +++ b/test/request.js @@ -303,14 +303,14 @@ describe('swagger request functions', function () { var petApi = sample.pet; var curl = petApi.addPet.asCurl({body: {id:10101}}); - expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{\\"id\\":10101}" "http://localhost:8000/v2/api/pet"'); + expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{\\"id\\":10101}\' "http://localhost:8000/v2/api/pet"'); }); it('prints a curl post statement from a string', function () { var petApi = sample.pet; var curl = petApi.addPet.asCurl({body: '{"id":10101}'}); - expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{\\"id\\":10101}" "http://localhost:8000/v2/api/pet"'); + expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{\\"id\\":10101}\' "http://localhost:8000/v2/api/pet"'); }); it('gets an server side 404, and verifies that the content-type in the response is correct, and different than one in the request', function (done) { @@ -404,4 +404,4 @@ describe('swagger basePath override functions', function () { sample.clientAuthorizations.authz = {}; done(); }); -}); \ No newline at end of file +}); From c1064643511ff8153f93b4d9279fe848a408c15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neuh=C3=A4user?= Date: Mon, 16 Nov 2015 11:18:28 +0100 Subject: [PATCH 2/3] Fixes escaping issue --- lib/types/operation.js | 2 +- test/request.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/types/operation.js b/lib/types/operation.js index 344341121..3e076490a 100644 --- a/lib/types/operation.js +++ b/lib/types/operation.js @@ -876,7 +876,7 @@ Operation.prototype.asCurl = function (args1, args2) { body = obj.body; } - results.push('-d \'' + body.replace(/"/g, '\\"') + '\''); + results.push('-d \'' + body.replace(/\'/g, '\\u0027') + '\''); } return 'curl ' + (results.join(' ')) + ' "' + obj.url + '"'; diff --git a/test/request.js b/test/request.js index 0bccae35a..b90c28e1f 100644 --- a/test/request.js +++ b/test/request.js @@ -303,14 +303,21 @@ describe('swagger request functions', function () { var petApi = sample.pet; var curl = petApi.addPet.asCurl({body: {id:10101}}); - expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{\\"id\\":10101}\' "http://localhost:8000/v2/api/pet"'); + expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{"id":10101}\' "http://localhost:8000/v2/api/pet"'); }); it('prints a curl post statement from a string', function () { var petApi = sample.pet; var curl = petApi.addPet.asCurl({body: '{"id":10101}'}); - expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{\\"id\\":10101}\' "http://localhost:8000/v2/api/pet"'); + expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{"id":10101}\' "http://localhost:8000/v2/api/pet"'); + }); + + it('prints a curl post statement from a string containing a single quote', function () { + var petApi = sample.pet; + var curl = petApi.addPet.asCurl({body: '{"id":"foo\'bar"}'}); + + expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{"id":"foo\\u0027bar"}\' "http://localhost:8000/v2/api/pet"'); }); it('gets an server side 404, and verifies that the content-type in the response is correct, and different than one in the request', function (done) { From 3d24399ee3bcdd4f4c962296818add2fa56b9698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neuh=C3=A4user?= Date: Mon, 16 Nov 2015 11:33:54 +0100 Subject: [PATCH 3/3] Outputs --header in single quotes --- lib/types/operation.js | 4 ++-- test/compat/request.js | 2 +- test/help.js | 6 +++--- test/request.js | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/types/operation.js b/lib/types/operation.js index 3e076490a..8672e7419 100644 --- a/lib/types/operation.js +++ b/lib/types/operation.js @@ -861,9 +861,9 @@ Operation.prototype.asCurl = function (args1, args2) { for (key in obj.headers) { var value = obj.headers[key]; if(typeof value === 'string'){ - value = value.replace(/"/g, '\\"'); + value = value.replace(/\'/g, '\\u0027'); } - results.push('--header "' + key + ': ' + value + '"'); + results.push('--header \'' + key + ': ' + value + '\''); } } diff --git a/test/compat/request.js b/test/compat/request.js index f3e74ae7e..a99bf1766 100644 --- a/test/compat/request.js +++ b/test/compat/request.js @@ -34,7 +34,7 @@ describe('1.2 request operations', function () { it('shows curl syntax', function () { var curl = sample.pet.getPetById.asCurl({petId: 1}); - expect(curl).toBe('curl -X GET --header "Accept: application/json" "http://localhost:8001/v1/api/pet/1"'); + expect(curl).toBe('curl -X GET --header \'Accept: application/json\' "http://localhost:8001/v1/api/pet/1"'); }); it('generate a get request', function () { diff --git a/test/help.js b/test/help.js index bcc7dd310..ecbebd235 100644 --- a/test/help.js +++ b/test/help.js @@ -35,7 +35,7 @@ describe('help options', function () { new auth.SwaggerAuthorizations()); var curl = op.asCurl({}); - expect(curl).toBe('curl -X GET --header "Accept: application/json" "http://localhost/path"'); + expect(curl).toBe('curl -X GET --header \'Accept: application/json\' "http://localhost/path"'); }); it('prints a curl statement with headers', function () { @@ -65,7 +65,7 @@ describe('help options', function () { Authorization: 'Oauth:"test"' }); - expect(curl).toBe('curl -X GET --header "Accept: application/json" --header "name: tony" --header "age: 42" --header "Authorization: Oauth:\\"test\\"" "http://localhost/path"'); + expect(curl).toBe('curl -X GET --header \'Accept: application/json\' --header \'name: tony\' --header \'age: 42\' --header \'Authorization: Oauth:"test"\' "http://localhost/path"'); }); it('prints a curl statement with custom content-type', function () { @@ -75,6 +75,6 @@ describe('help options', function () { responseContentType: 'application/xml' }); - expect(curl).toBe('curl -X GET --header "Accept: application/xml" "http://localhost/path"'); + expect(curl).toBe('curl -X GET --header \'Accept: application/xml\' "http://localhost/path"'); }); }); diff --git a/test/request.js b/test/request.js index b90c28e1f..521d402a0 100644 --- a/test/request.js +++ b/test/request.js @@ -303,21 +303,21 @@ describe('swagger request functions', function () { var petApi = sample.pet; var curl = petApi.addPet.asCurl({body: {id:10101}}); - expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{"id":10101}\' "http://localhost:8000/v2/api/pet"'); + expect(curl).toBe('curl -X POST --header \'Content-Type: application/json\' --header \'Accept: application/json\' -d \'{"id":10101}\' "http://localhost:8000/v2/api/pet"'); }); it('prints a curl post statement from a string', function () { var petApi = sample.pet; var curl = petApi.addPet.asCurl({body: '{"id":10101}'}); - expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{"id":10101}\' "http://localhost:8000/v2/api/pet"'); + expect(curl).toBe('curl -X POST --header \'Content-Type: application/json\' --header \'Accept: application/json\' -d \'{"id":10101}\' "http://localhost:8000/v2/api/pet"'); }); it('prints a curl post statement from a string containing a single quote', function () { var petApi = sample.pet; var curl = petApi.addPet.asCurl({body: '{"id":"foo\'bar"}'}); - expect(curl).toBe('curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d \'{"id":"foo\\u0027bar"}\' "http://localhost:8000/v2/api/pet"'); + expect(curl).toBe('curl -X POST --header \'Content-Type: application/json\' --header \'Accept: application/json\' -d \'{"id":"foo\\u0027bar"}\' "http://localhost:8000/v2/api/pet"'); }); it('gets an server side 404, and verifies that the content-type in the response is correct, and different than one in the request', function (done) {