diff --git a/src/app/components/rest/controller.js b/src/app/components/rest/controller.js index 4b7b1605..251ba6fb 100644 --- a/src/app/components/rest/controller.js +++ b/src/app/components/rest/controller.js @@ -81,10 +81,11 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', if (path.substring(0, 1) !== '/') { path = '/' + path; } - var body = JSON.stringify($scope.editor.getValue(), undefined, 1); var curl = 'curl -X' + method + ' \'' + $scope.host + path + '\''; if (['POST', 'PUT'].indexOf(method) >= 0) { - curl += ' -d \'' + body + '\''; + var body = JSON.stringify($scope.editor.getValue(), undefined, 1); + var escapedBody = body.replace(/'/g, '\'\\\'\''); + curl += ' -d \'' + escapedBody + '\''; } ClipboardService.copy( curl, diff --git a/tests/components/rest/controller.tests.js b/tests/components/rest/controller.tests.js index df60a9d9..0e0a8007 100644 --- a/tests/components/rest/controller.tests.js +++ b/tests/components/rest/controller.tests.js @@ -10,9 +10,10 @@ describe('RestController', function() { this.AlertService = $injector.get('AlertService'); this.ModalService = $injector.get('ModalService'); this.AceEditorService = $injector.get('AceEditorService'); + this.ClipboardService = $injector.get('ClipboardService'); this.createController = function() { return $controller('RestController', - {$scope: this.scope}, this.$http, this.$window, this.RestDataService, this.AlertService, this.ModalService, this.AceEditorService); + {$scope: this.scope}, this.$http, this.$window, this.RestDataService, this.AlertService, this.ModalService, this.AceEditorService, this.ClipboardService); }; this._controller = this.createController(); })); @@ -183,4 +184,21 @@ describe('RestController', function() { }); }); + describe('copyAsCURLCommand', function() { + it('copy given requests as a curl command with correct escaping', function() { + this.scope.path = "_reindex" + this.scope.host = "http://localhost:9200" + this.scope.editor = { + getValue: function() { + return { + "script": "'metricbeat-' + '1234'" + } + } + }; + spyOn(this.ClipboardService, 'copy') + this.scope.copyAsCURLCommand(); + expect(this.ClipboardService.copy).toHaveBeenCalledWith('curl -XPOST \'http://localhost:9200/_reindex\' -d \'{\n\ "script": "\'\\\'\'metricbeat-\'\\\'\' + \'\\\'\'1234\'\\\'\'"\n}\'', jasmine.any(Function), jasmine.any(Function)); + }); + }); + });