-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1414 from bjoernricks/fast-xml-parser-encoding
Fast xml parser encoding
- Loading branch information
Showing
21 changed files
with
444 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"<>&'/\</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"<>&'/\"></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'); | ||
}); | ||
}); |
Oops, something went wrong.