Skip to content

Commit

Permalink
Merge pull request #1414 from bjoernricks/fast-xml-parser-encoding
Browse files Browse the repository at this point in the history
Fast xml parser encoding
  • Loading branch information
swaterkamp authored May 17, 2019
2 parents 5c1941a + af93b5e commit 188ed32
Show file tree
Hide file tree
Showing 21 changed files with 444 additions and 207 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Cleanup get_report function in gsad [#1263](https://github.com/greenbone/gsa/pull/1263)

### Fixed
- Fix xml decoding issues with fast-xml-parser [#1414](https://github.com/greenbone/gsa/pull/1414)
- Fix translation for task status and task trend tooltip [#1409](https://github.com/greenbone/gsa/pull/1409)
- Fix problems with German translation in Add Dashboard dialog, SolutionTypeGroup and SeverityClassLabel [#1412](https://github.com/greenbone/gsa/pull/1412)
- Fix some translation bugs (statusbar, about page, table header tooltips)[#1407](https://github.com/greenbone/gsa/pull/1407)
Expand Down
2 changes: 1 addition & 1 deletion gsa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"d3-shape": "^1.3.3",
"downshift": "^1.31.16",
"fast-deep-equal": "^1.1.0",
"fast-xml-parser": "^3.12.13",
"fast-xml-parser": "^3.12.16",
"glamor": "^2.20.40",
"history": "^4.7.2",
"hoist-non-react-statics": "^3.3.0",
Expand Down
4 changes: 2 additions & 2 deletions gsa/src/gmp/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class LoginCommand extends HttpCommand {
}).then(
response => new Login(response.data),
rej => {
if (rej.isError && rej.isError() && rej.xhr) {
switch (rej.xhr.status) {
if (rej.isError && rej.isError()) {
switch (rej.status) {
case 401:
rej.setMessage(_('Bad login information'));
break;
Expand Down
31 changes: 22 additions & 9 deletions gsa/src/gmp/http/__tests__/rejection.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ describe('Rejection tests', () => {

expect(rejection.reason).toEqual(Rejection.REASON_ERROR);
expect(rejection.message).toEqual('');
expect(rejection.xhr).toBeUndefined();
expect(rejection.error).toBeUndefined();
expect(rejection.stack).toBeDefined();
expect(rejection.isError()).toEqual(true);
expect(rejection.status).toBeUndefined();
});

test('should create error rejection', () => {
const xhr = {foo: 'bar'};
const xhr = {status: 123};
const error = new Error('foobar');
const rejection = new Rejection(
xhr,
Expand All @@ -42,46 +42,46 @@ describe('Rejection tests', () => {

expect(rejection.reason).toEqual(Rejection.REASON_ERROR);
expect(rejection.message).toEqual('an error');
expect(rejection.xhr).toEqual(xhr);
expect(rejection.error).toEqual(error);
expect(rejection.stack).toBeDefined();
expect(rejection.isError()).toEqual(true);
expect(rejection.status).toEqual(123);
});

test('should create unauthorized rejection', () => {
const xhr = {foo: 'bar'};
const xhr = {status: 123};
const rejection = new Rejection(xhr, Rejection.REASON_UNAUTHORIZED);

expect(rejection.reason).toEqual(Rejection.REASON_UNAUTHORIZED);
expect(rejection.message).toEqual('');
expect(rejection.xhr).toEqual(xhr);
expect(rejection.error).toBeUndefined();
expect(rejection.stack).toBeDefined();
expect(rejection.isError()).toEqual(false);
expect(rejection.status).toEqual(123);
});

test('should create cancel rejection', () => {
const xhr = {foo: 'bar'};
const xhr = {status: 123};
const rejection = new Rejection(xhr, Rejection.REASON_CANCEL, 'foo');

expect(rejection.reason).toEqual(Rejection.REASON_CANCEL);
expect(rejection.message).toEqual('foo');
expect(rejection.xhr).toEqual(xhr);
expect(rejection.error).toBeUndefined();
expect(rejection.stack).toBeDefined();
expect(rejection.isError()).toEqual(false);
expect(rejection.status).toEqual(123);
});

test('should create timeout rejection', () => {
const xhr = {foo: 'bar'};
const xhr = {status: 123};
const rejection = new Rejection(xhr, Rejection.REASON_TIMEOUT, 'foo');

expect(rejection.reason).toEqual(Rejection.REASON_TIMEOUT);
expect(rejection.message).toEqual('foo');
expect(rejection.xhr).toEqual(xhr);
expect(rejection.error).toBeUndefined();
expect(rejection.stack).toBeDefined();
expect(rejection.isError()).toEqual(false);
expect(rejection.status).toEqual(123);
});

test('should allow to change message', () => {
Expand All @@ -93,4 +93,17 @@ describe('Rejection tests', () => {

expect(rejection.message).toEqual('bar');
});

test('should allow to get plain data', () => {
const xhr = {
response: 'foo',
responseText: 'bar',
responseXML: 'ipsum',
};
const rejection = new Rejection(xhr, Rejection.REASON_ERROR, 'foo');

expect(rejection.plainData()).toEqual('foo');
expect(rejection.plainData('text')).toEqual('bar');
expect(rejection.plainData('xml')).toEqual('ipsum');
});
});
34 changes: 34 additions & 0 deletions gsa/src/gmp/http/__tests__/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright (C) 2019 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import Response from '../response';

describe('Response tests', () => {
test('should allow to get plain data', () => {
const xhr = {
response: 'foo',
responseText: 'bar',
responseXML: 'ipsum',
};
const response = new Response(xhr, {});

expect(response.plainData()).toEqual('foo');
expect(response.plainData('text')).toEqual('bar');
expect(response.plainData('xml')).toEqual('ipsum');
});
});
17 changes: 16 additions & 1 deletion gsa/src/gmp/http/rejection.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,27 @@ class Rejection {
this.name = 'Rejection';
this.message = message;
this.reason = reason;
this.xhr = xhr;
this.error = error;

this._xhr = xhr;

if (!isDefined(error)) {
error = new Error();
}

this.stack = error.stack;
}

plainData(type = '') {
if (type === 'xml') {
return this._xhr.responseXML;
}
if (type === 'text') {
return this._xhr.responseText;
}
return this._xhr.response;
}

isError() {
return this.reason === Rejection.REASON_ERROR;
}
Expand All @@ -46,6 +57,10 @@ class Rejection {
this.message = message;
return this;
}

get status() {
return isDefined(this._xhr) ? this._xhr.status : undefined;
}
}

export default Rejection;
Expand Down
4 changes: 3 additions & 1 deletion gsa/src/gmp/http/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class Response {
if (type === 'xml') {
return this._xhr.responseXML;
}
this._xhr.responseType = type;
if (type === 'text') {
return this._xhr.responseText;
}
return this._xhr.response;
}

Expand Down
137 changes: 137 additions & 0 deletions gsa/src/gmp/http/transform/__tests__/fastxml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* Copyright (C) 2019 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import transform from '../fastxml';

const createEnvelopedXml = xmlStr =>
`
<envelope>
<version>123</version>
<backend_operation>1</backend_operation>
<vendor_version>FooBar</vendor_version>
<i18n>en</i18n>
<time></time>
<timezone>UTC</timezone>
${xmlStr}
</envelope>
`;

const envelopeMeta = {
backendOperation: 1,
i18n: 'en',
time: '',
timezone: 'UTC',
version: 123,
vendorVersion: 'FooBar',
};

describe('fastxml transform tests', () => {
test('should transform xml encoded element content successfully', () => {
const xmlStr = createEnvelopedXml(
'<foo>foo&quot;&lt;&gt;&amp;&apos;&#x2F;&#x5C;</foo>',
);
const plainData = jest.fn().mockReturnValue(xmlStr);
const setData = jest.fn().mockReturnValue('foo');
const response = {
plainData,
set: setData,
};

expect(transform.success(response)).toEqual('foo');
expect(plainData).toHaveBeenCalledWith('text');
expect(setData).toHaveBeenCalledWith(
{
foo: 'foo"<>&\'/\\',
},
envelopeMeta,
);
});

test('should transform xml encoded element attribute successfully', () => {
const xmlStr = createEnvelopedXml(
'<foo bar="foo&quot;&lt;&gt;&amp;&apos;&#x2F;&#x5C;"></foo>',
);
const plainData = jest.fn().mockReturnValue(xmlStr);
const setData = jest.fn().mockReturnValue('foo');
const response = {
plainData,
set: setData,
};

expect(transform.success(response)).toEqual('foo');
expect(plainData).toHaveBeenCalledWith('text');
expect(setData).toHaveBeenCalledWith(
{
foo: {
_bar: 'foo"<>&\'/\\',
},
},
envelopeMeta,
);
});

test('should create a rejection on parser errors', () => {
const plainData = jest.fn().mockReturnValue({foo: 'bar'});
const setData = jest.fn().mockReturnValue('foo');
const response = {
plainData,
set: setData,
};

expect(() => {
transform.success(response);
}).toThrow();
});

test('should transform rejection with action_result', () => {
const xmlStr =
'<envelope><action_result><message>foo</message></action_result></envelope>';
const isError = jest.fn().mockReturnValue(true);
const setMessage = jest.fn(() => errorRejection);
const plainData = jest.fn().mockReturnValue(xmlStr);
const errorRejection = {
isError,
setMessage,
plainData,
};

expect(transform.rejection(errorRejection)).toBe(errorRejection);
expect(isError).toHaveBeenCalled();
expect(plainData).toHaveBeenCalledWith('text');
expect(setMessage).toHaveBeenCalledWith('foo');
});

test('should transform rejection with gsad_response', () => {
const xmlStr =
'<envelope><action_result><message>foo</message></action_result>' +
'<gsad_response><message>bar</message></gsad_response></envelope>';
const isError = jest.fn().mockReturnValue(true);
const setMessage = jest.fn(() => errorRejection);
const plainData = jest.fn().mockReturnValue(xmlStr);
const errorRejection = {
isError,
setMessage,
plainData,
};

expect(transform.rejection(errorRejection)).toBe(errorRejection);
expect(isError).toHaveBeenCalled();
expect(plainData).toHaveBeenCalledWith('text');
expect(setMessage).toHaveBeenCalledWith('bar');
});
});
Loading

0 comments on commit 188ed32

Please sign in to comment.