Files
gerrit/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-rest-api_test.html
Viktar Donich e5a2f5c5cd Introduce plugin.restApi(), deprecate other REST helper methods
Implement REST-related methods on Gerrit (get, post, etc).

REST-related methods on plugin now take plugin URL space into account,
to match GWT UI plugin JS API.

Example:

``` js
Gerrit.install(plugin => {
  // deprecated:
  plugin.get('/foo', json => {
    // work work
  });
  Gerrit.post('/bar', {bar: 'space'}, json => {
    // post succeeds
  });

  // recommended:
  const pluginRestApi = plugin.restApi(plugin.url());
  plugin.get('/foo').then(json => {
    // work work
  });
  plugin.restApi().post('/bar', {bar: 'space'}).then(json => {
    // post succeeds
  });
});
```

Change-Id: I6f537507d76bddec1cac9159cebe1b720ab5caf8
2017-10-30 13:25:31 -07:00

125 lines
4.1 KiB
HTML

<!DOCTYPE html>
<!--
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-plugin-rest-api</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-js-api-interface.html"/>
<script>
suite('gr-plugin-rest-api tests', () => {
let instance;
let sandbox;
let getResponseObjectStub;
let sendStub;
setup(() => {
sandbox = sinon.sandbox.create();
getResponseObjectStub = sandbox.stub().returns(Promise.resolve());
sendStub = sandbox.stub().returns(Promise.resolve({status: 200}));
stub('gr-rest-api-interface', {
getAccount() {
return Promise.resolve({name: 'Judy Hopps'});
},
getResponseObject: getResponseObjectStub,
send(...args) {
return sendStub(...args);
},
});
Gerrit._setPluginsCount(1);
Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js');
instance = new GrPluginRestApi();
});
teardown(() => {
sandbox.restore();
});
test('fetch', () => {
const payload = {foo: 'foo'};
return instance.fetch('HTTP_METHOD', '/url', payload).then(r => {
assert.isTrue(sendStub.calledWith('HTTP_METHOD', '/url', payload));
assert.equal(r.status, 200);
assert.isFalse(getResponseObjectStub.called);
});
});
test('send', () => {
const payload = {foo: 'foo'};
const response = {bar: 'bar'};
getResponseObjectStub.returns(Promise.resolve(response));
return instance.send('HTTP_METHOD', '/url', payload).then(r => {
assert.isTrue(sendStub.calledWith('HTTP_METHOD', '/url', payload));
assert.strictEqual(r, response);
});
});
test('get', () => {
const response = {foo: 'foo'};
getResponseObjectStub.returns(Promise.resolve(response));
return instance.get('/url').then(r => {
assert.isTrue(sendStub.calledWith('GET', '/url'));
assert.strictEqual(r, response);
});
});
test('post', () => {
const payload = {foo: 'foo'};
const response = {bar: 'bar'};
getResponseObjectStub.returns(Promise.resolve(response));
return instance.post('/url', payload).then(r => {
assert.isTrue(sendStub.calledWith('POST', '/url', payload));
assert.strictEqual(r, response);
});
});
test('put', () => {
const payload = {foo: 'foo'};
const response = {bar: 'bar'};
getResponseObjectStub.returns(Promise.resolve(response));
return instance.put('/url', payload).then(r => {
assert.isTrue(sendStub.calledWith('PUT', '/url', payload));
assert.strictEqual(r, response);
});
});
test('delete works', () => {
const response = {status: 204};
sendStub.returns(Promise.resolve(response));
return instance.delete('/url').then(r => {
assert.isTrue(sendStub.calledWith('DELETE', '/url'));
assert.strictEqual(r, response);
});
});
test('delete fails', () => {
sendStub.returns(Promise.resolve(
{status: 400, text() { return Promise.resolve('text'); }}));
return instance.delete('/url').then(r => {
throw new Error('Should not resolve');
}).catch(err => {
assert.isTrue(sendStub.calledWith('DELETE', '/url'));
assert.equal('text', err);
});
});
});
</script>