Skip to content

Commit

Permalink
Add tests for verifying that namespace is treated correctly on empty …
Browse files Browse the repository at this point in the history
…element
  • Loading branch information
kjellmorten committed Sep 28, 2023
1 parent bf5ad39 commit 65d370b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,38 @@ test('should serialize data in payload', async (t) => {
t.deepEqual(ret, expected)
})

test('should serialize data in payload with empty root element', async (t) => {
const options = { namespaces, includeHeaders: false }
const action = {
type: 'GET',
payload: {
type: 'entry',
data: {
'env:Envelope': {
'env:Body': {
GetPaymentMethods: null,
},
},
},
sourceService: 'api',
},
meta: { ident: { id: 'johnf' } },
}
const expected = {
type: 'GET',
payload: {
type: 'entry',
data: `<?xml version="1.0" encoding="utf-8"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body><GetPaymentMethods xsi:nil="true" xmlns="http://example.com/webservices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/></env:Body></env:Envelope>`,
sourceService: 'api',
},
meta: { ident: { id: 'johnf' } },
}

const ret = await adapter.serialize(action, options)

t.deepEqual(ret, expected)
})

test('should not double encode already encoded entities', async (t) => {
const options = { namespaces, dontDoubleEncode: true, includeHeaders: false } // Tell adapter to not double encode
const data = {
Expand Down
26 changes: 26 additions & 0 deletions src/utils/setNamespaceAttrs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ test('should set namespace on first occurence of prefix', (t) => {
t.deepEqual(ret, expected)
})

test('should set namespace on first occurence of prefix when it is empty', (t) => {
const data = {
'soap:Envelope': {
'soap:Body': {
GetPaymentMethodsResponse: null, // Is empty
},
},
}
const expected = {
'soap:Envelope': {
'@xmlns:soap': 'http://www.w3.org/2003/05/soap-envelope',
'soap:Body': {
GetPaymentMethodsResponse: {
'@xmlns': 'http://example.com/webservices',
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'@xsi:nil': 'true',
},
},
},
}

const ret = setNamespaceAttrs(data, namespaces, 'xsi')

t.deepEqual(ret, expected)
})

test('should declare namespace on leaf', (t) => {
const data = {
'soap:Envelope': {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/setNamespaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('should set soap namespace for version 1.1', (t) => {

const { namespaces: nextNS, soapPrefix } = setNamespaces(
namespaces,
soapVersion
soapVersion,
)

t.deepEqual(nextNS, expected)
Expand All @@ -65,7 +65,7 @@ test('should set soap namespace for version 1.2', (t) => {

const { namespaces: nextNS, soapPrefix } = setNamespaces(
namespaces,
soapVersion
soapVersion,
)

t.deepEqual(nextNS, expected)
Expand All @@ -86,7 +86,7 @@ test('should pick up soap namespace prefix', (t) => {

const { namespaces: nextNS, soapPrefix } = setNamespaces(
namespaces,
soapVersion
soapVersion,
)

t.deepEqual(nextNS, expected)
Expand Down
16 changes: 16 additions & 0 deletions src/utils/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ test('should stringify object structure to xml', (t) => {
t.is(ret, expected)
})

test('should stringify object structure to xml when first element is empty', (t) => {
const data = {
'soap:Envelope': {
'soap:Body': {
GetPaymentMethodsResponse: null, // Is empty
},
},
}
const expected =
'<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><GetPaymentMethodsResponse xsi:nil="true" xmlns="http://example.com/webservices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/></soap:Body></soap:Envelope>'

const { data: ret } = stringify(data, { namespaces })

t.is(ret, expected)
})

test('should stringify object structure to xml without directive', (t) => {
const hideXmlDirective = true
const data = {
Expand Down

0 comments on commit 65d370b

Please sign in to comment.