Skip to content

Commit e93ab99

Browse files
committed
include content-type on copied curl requests
closes #426
1 parent abb8ab7 commit e93ab99

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

public/js/app.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -1748,8 +1748,31 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',
17481748
if (path.substring(0, 1) !== '/') {
17491749
path = '/' + path;
17501750
}
1751-
var body = JSON.stringify($scope.editor.getValue(), undefined, 1);
1752-
var curl = 'curl -X' + method + ' \'' + $scope.host + path + '\'';
1751+
1752+
var matchesAPI = function(path, api) {
1753+
return path.indexOf(api) === (path.length - api.length);
1754+
};
1755+
1756+
var contentType = 'application/json';
1757+
var body = '';
1758+
1759+
try {
1760+
if (matchesAPI(path, '_bulk') || matchesAPI(path, '_msearch')) {
1761+
contentType = 'application/x-ndjson';
1762+
body = $scope.editor.getStringValue().split('\n').map(function(line) {
1763+
return line === '' ? '\n' : JSON.stringify(JSON.parse(line));
1764+
}).join('\n');
1765+
} else {
1766+
body = JSON.stringify($scope.editor.getValue(), undefined, 1);
1767+
}
1768+
} catch (e) {
1769+
AlertService.error('Unexpected content format for [' + path + ']');
1770+
return;
1771+
}
1772+
1773+
var curl = 'curl';
1774+
curl += ' -H \'Content-type: ' + contentType + '\'';
1775+
curl += ' -X' + method + ' \'' + $scope.host + path + '\'';
17531776
if (['POST', 'PUT'].indexOf(method) >= 0) {
17541777
curl += ' -d \'' + body + '\'';
17551778
}

src/app/components/rest/controller.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,31 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',
8181
if (path.substring(0, 1) !== '/') {
8282
path = '/' + path;
8383
}
84-
var body = JSON.stringify($scope.editor.getValue(), undefined, 1);
85-
var curl = 'curl -X' + method + ' \'' + $scope.host + path + '\'';
84+
85+
var matchesAPI = function(path, api) {
86+
return path.indexOf(api) === (path.length - api.length);
87+
};
88+
89+
var contentType = 'application/json';
90+
var body = '';
91+
92+
try {
93+
if (matchesAPI(path, '_bulk') || matchesAPI(path, '_msearch')) {
94+
contentType = 'application/x-ndjson';
95+
body = $scope.editor.getStringValue().split('\n').map(function(line) {
96+
return line === '' ? '\n' : JSON.stringify(JSON.parse(line));
97+
}).join('\n');
98+
} else {
99+
body = JSON.stringify($scope.editor.getValue(), undefined, 1);
100+
}
101+
} catch (e) {
102+
AlertService.error('Unexpected content format for [' + path + ']');
103+
return;
104+
}
105+
106+
var curl = 'curl';
107+
curl += ' -H \'Content-type: ' + contentType + '\'';
108+
curl += ' -X' + method + ' \'' + $scope.host + path + '\'';
86109
if (['POST', 'PUT'].indexOf(method) >= 0) {
87110
curl += ' -d \'' + body + '\'';
88111
}

tests/components/rest/controller.tests.js

+71-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ describe('RestController', function() {
1010
this.AlertService = $injector.get('AlertService');
1111
this.ModalService = $injector.get('ModalService');
1212
this.AceEditorService = $injector.get('AceEditorService');
13+
this.ClipboardService = $injector.get('ClipboardService');
1314
this.createController = function() {
1415
return $controller('RestController',
15-
{$scope: this.scope}, this.$http, this.$window, this.RestDataService, this.AlertService, this.ModalService, this.AceEditorService);
16+
{$scope: this.scope},
17+
this.$http,
18+
this.$window,
19+
this.RestDataService,
20+
this.AlertService,
21+
this.ModalService,
22+
this.AceEditorService,
23+
this.ClipboardService
24+
);
1625
};
1726
this._controller = this.createController();
1827
}));
@@ -129,7 +138,7 @@ describe('RestController', function() {
129138
it('loads all possible autocompletion options', function() {
130139
this.scope.indices = [];
131140
this.scope.updateOptions("");
132-
expect(this.scope.options).toEqual(['_msearch', '_search', '_suggest']);
141+
expect(this.scope.options).toEqual(['_msearch', '_search']);
133142
});
134143
it('skip autocompletion if indices is absent', function() {
135144
this.scope.indices = undefined;
@@ -183,4 +192,64 @@ describe('RestController', function() {
183192
});
184193
});
185194

195+
describe('copyAsCURLCommand', function() {
196+
it('copy json call to clipboard', function() {
197+
this.scope.path = 'wot/_search';
198+
this.scope.method = 'GET';
199+
this.scope.host = 'http://localhost:9200';
200+
this.scope.editor = { getValue: function(){} };
201+
spyOn(this.scope.editor, 'getValue').and.returnValue({'k': 'v'});
202+
spyOn(this.ClipboardService, 'copy');
203+
this.scope.copyAsCURLCommand();
204+
expect(this.scope.editor.getValue).toHaveBeenCalled();
205+
expect(this.ClipboardService.copy).toHaveBeenCalledWith(
206+
'curl -H \'Content-type: application/json\' -XGET \'http://localhost:9200/wot/_search\'',
207+
jasmine.any(Function),
208+
jasmine.any(Function)
209+
);
210+
});
211+
212+
it('copy x-ndjson call to clipboard for _bulk', function() {
213+
this.scope.path = 'wot/_bulk';
214+
this.scope.method = 'POST';
215+
this.scope.host = 'http://localhost:9200';
216+
this.scope.editor = { getStringValue: function(){} };
217+
var body = '{"header": ""}\n{"query": "query"}\n{"header": ""}\n{"query": "query"}\n';
218+
spyOn(this.scope.editor, 'getStringValue').and.returnValue(body);
219+
spyOn(this.ClipboardService, 'copy');
220+
this.scope.copyAsCURLCommand();
221+
expect(this.scope.editor.getStringValue).toHaveBeenCalled();
222+
expect(this.ClipboardService.copy).toHaveBeenCalledWith(
223+
'curl -H \'Content-type: application/x-ndjson\' -XPOST \'http://localhost:9200/wot/_bulk\' -d \'{"header":""}\n' +
224+
'{"query":"query"}\n' +
225+
'{"header":""}\n' +
226+
'{"query":"query"}\n' +
227+
'\n\'',
228+
jasmine.any(Function),
229+
jasmine.any(Function)
230+
);
231+
});
232+
233+
it('copy x-ndjson call to clipboard for _msearch', function() {
234+
this.scope.path = 'wot/_msearch';
235+
this.scope.method = 'POST';
236+
this.scope.host = 'http://localhost:9200';
237+
this.scope.editor = { getStringValue: function(){} };
238+
var body = '{"header": ""}\n{"query": "query"}\n{"header": ""}\n{"query": "query"}\n';
239+
spyOn(this.scope.editor, 'getStringValue').and.returnValue(body);
240+
spyOn(this.ClipboardService, 'copy');
241+
this.scope.copyAsCURLCommand();
242+
expect(this.scope.editor.getStringValue).toHaveBeenCalled();
243+
expect(this.ClipboardService.copy).toHaveBeenCalledWith(
244+
'curl -H \'Content-type: application/x-ndjson\' -XPOST \'http://localhost:9200/wot/_msearch\' -d \'{"header":""}\n' +
245+
'{"query":"query"}\n' +
246+
'{"header":""}\n' +
247+
'{"query":"query"}\n' +
248+
'\n\'',
249+
jasmine.any(Function),
250+
jasmine.any(Function)
251+
);
252+
});
253+
});
254+
186255
});

0 commit comments

Comments
 (0)