-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
7 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,50 @@ | ||
/** | ||
* Deno module for easy-soap-request | ||
* @author Caleb Lemoine | ||
* @param {object} opts easy-soap-request options | ||
* @param {string} opts.url endpoint URL | ||
* @param {object} opts.headers HTTP headers, can be string or object | ||
* @param {string} opts.xml SOAP envelope, can be read from file or passed as string | ||
* @param {object} opts.extraOpts Object of additional fetch parameters | ||
* @promise response | ||
* @reject {error} | ||
* @fulfill {body,statusCode} | ||
* @returns {Promise.response{body,statusCode}} | ||
*/ | ||
export default function soapRequest(opts = { | ||
url: '', | ||
headers: {}, | ||
xml: '', | ||
extraOpts: {}, | ||
}) { | ||
const { | ||
url, | ||
headers, | ||
xml, | ||
extraOpts, | ||
} = opts; | ||
return new Promise((resolve, reject) => { | ||
fetch(url, { | ||
method: 'post', | ||
headers, | ||
body: xml, | ||
...extraOpts, | ||
}).then(async (response) => { | ||
resolve({ | ||
response: { | ||
headers: response.headers, | ||
body: await response.text(), | ||
statusCode: response.status, | ||
}, | ||
}); | ||
}).catch(async (error) => { | ||
if (error.response) { | ||
console.error(`SOAP FAIL: ${error}`); | ||
reject(await error.response.text()); | ||
} else { | ||
console.error(`SOAP FAIL: ${error}`); | ||
reject(error); | ||
} | ||
}); | ||
}); | ||
} |
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,57 @@ | ||
import { readFileStr } from 'https://deno.land/std/fs/mod.ts'; | ||
import { assertEquals } from 'https://deno.land/std/testing/asserts.ts'; | ||
import soapRequest from '../index.d.js'; | ||
|
||
|
||
const url = 'https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php'; | ||
const urlFail = 'https://graphical.weather.gov:80/xml/SOAP_server/ndfdXMLserver.php'; | ||
const headers = { | ||
'user-agent': 'easy-soap-request-test', | ||
'Content-Type': 'text/xml;charset=UTF-8', | ||
SOAPAction: 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode', | ||
}; | ||
|
||
const coordinates = '32.9612,-96.8372'; | ||
|
||
Deno.test({ | ||
name: `Zip Code 75001 should return ${coordinates}`, | ||
fn: async () => { | ||
const xml = await readFileStr('test/zip-code-envelope.xml'); | ||
const { response } = await soapRequest({ url, headers, xml }); | ||
const { body, statusCode } = response; | ||
assertEquals(body.includes(coordinates), true); | ||
assertEquals(statusCode, 200); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: 'Should catch Promise Rejection', | ||
fn: async () => { | ||
try { | ||
const xmlFail = await readFileStr('test/zip-code-envelope-fail.xml'); | ||
const { response } = await soapRequest({ url, headers, xmlFail }); | ||
const { statusCode } = response; | ||
assertEquals(statusCode, 200); | ||
} catch (e) { | ||
// Test promise rejection for coverage | ||
} | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: 'Should catch connection error Promise Rejection', | ||
fn: async () => { | ||
try { | ||
const xmlFail = await readFileStr('test/zip-code-envelope-fail.xml'); | ||
const { response } = await soapRequest({ | ||
url: urlFail, | ||
headers, | ||
xml: xmlFail, | ||
}); | ||
const { statusCode } = response; | ||
assertEquals(statusCode, 200); | ||
} catch (e) { | ||
// Test promise rejection for coverage | ||
} | ||
}, | ||
}); |