UNPKG

mojax

Version:

extendable ajax request module

312 lines (311 loc) 11.7 kB
<!doctype html> <html lang="en"> <head> <title>Code coverage report for lib/finalizeParams.js</title> <meta charset="utf-8" /> <link rel="stylesheet" href="../prettify.css" /> <link rel="stylesheet" href="../base.css" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type='text/css'> .coverage-summary .sorter { background-image: url(../sort-arrow-sprite.png); } </style> </head> <body> <div class='wrapper'> <div class='pad1'> <h1> <a href="../index.html">all files</a> / <a href="index.html">lib/</a> finalizeParams.js </h1> <div class='clearfix'> <div class='fl pad1y space-right2'> <span class="strong">29.41% </span> <span class="quiet">Statements</span> <span class='fraction'>10/34</span> </div> <div class='fl pad1y space-right2'> <span class="strong">23.33% </span> <span class="quiet">Branches</span> <span class='fraction'>7/30</span> </div> <div class='fl pad1y space-right2'> <span class="strong">33.33% </span> <span class="quiet">Functions</span> <span class='fraction'>1/3</span> </div> <div class='fl pad1y space-right2'> <span class="strong">29.41% </span> <span class="quiet">Lines</span> <span class='fraction'>10/34</span> </div> </div> </div> <div class='status-line low'></div> <pre><table class="coverage"> <tr><td class="line-count quiet">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83</td><td class="line-coverage quiet"><span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">var mapCallbacks = require("./mapCallbacks"); &nbsp; /** * @summary enforces required properties of the request configuration object. * @param {object} c - request configuration object */ function finalizeParams(c) { "use strict"; &nbsp; if (Object.prototype.toString.call(c) !== "[object Object]") { throw new TypeError("ajax request expects an object argument. Provided\n" + c + " (" + typeof c + ")"); } &nbsp; // url must be string if (typeof c.url !== "string") { throw new TypeError("ajax request object expects prop 'url' to be a string. Provided\n" + c.url + " (" + typeof c.url + ")"); } &nbsp; // timeout can be set <span class="missing-if-branch" title="if path not taken" >I</span>if (c.timeout &amp;&amp; <span class="branch-1 cbranch-no" title="branch not covered" >typeof c.timeout !== "number")</span> { <span class="cstat-no" title="statement not covered" > throw new TypeError("ajax request object timeout property must be an integer. Provided\n" + c.timeout + " (" + typeof c.timeout + ")");</span> } &nbsp; // request method must be uppercase string === GET | POST | PUT | DELETE <span class="missing-if-branch" title="if path not taken" >I</span>if (typeof c.method === "string") { // enforce proper case <span class="cstat-no" title="statement not covered" > c.method = c.method.toUpperCase();</span> &nbsp; <span class="cstat-no" title="statement not covered" > if (["GET","POST","PUT","DELETE"].indexOf(c.method) === -1) {</span> <span class="cstat-no" title="statement not covered" > throw new RangeError("ajax: 'method' must have one of the following values:\nGET\nPOST\nPUT\nDELETE\nProvided: " + c.method);</span> } } else { throw new RangeError("ajax request object expects property 'method' to be one of:\n GET, POST, PUT, DELETE\n Provided:\n" + c.method); } &nbsp; // optional <span class="cstat-no" title="statement not covered" > if (c.responseType) {</span> // enforce proper case <span class="cstat-no" title="statement not covered" > c.responseType = c.responseType.toLowerCase();</span> &nbsp; <span class="cstat-no" title="statement not covered" > if (["json","text","xml","text/xml","text/html","arraybuffer","blob"].indexOf(c.responseType) === -1) {</span> <span class="cstat-no" title="statement not covered" > throw new RangeError("ajax: responseType must have one of the following values:\narraybuffer\nblob\njson\ntext\nxml\ntext/xml\ntext/html\n\nProvided: " + c.responseType);</span> } } else { <span class="cstat-no" title="statement not covered" > c.responseType = "text";</span> } &nbsp; // force mandatory X-Requested-With header <span class="cstat-no" title="statement not covered" > if (c.headers) {</span> <span class="cstat-no" title="statement not covered" > if (Object.prototype.toString.call(c.headers) !== "[object Object]") {</span> <span class="cstat-no" title="statement not covered" > throw new TypeError("ajax request object expects prop 'headers' to be an object of key:value pairing for header:value\nProvided:\n" + c.headers);</span> } else { <span class="cstat-no" title="statement not covered" > Object.keys(c.headers).forEach(<span class="fstat-no" title="function not covered" >function(key) {</span></span> <span class="cstat-no" title="statement not covered" > if (typeof c.headers[key] !== "string") {</span> <span class="cstat-no" title="statement not covered" > throw new TypeError("ajax request object 'headers' properties must all be strings.\nProvided " + typeof c.headers[key] + " for " + key);</span> } }); &nbsp; <span class="cstat-no" title="statement not covered" > if (!c.headers["X-Requested-With"]) {</span> <span class="cstat-no" title="statement not covered" > c.headers["X-Requested-With"] = "XMLHttpRequest";</span> } } } else { <span class="cstat-no" title="statement not covered" > c.headers = {</span> "X-Requested-With": "XMLHttpRequest" }; } &nbsp; <span class="cstat-no" title="statement not covered" > if (c.method === "POST" || c.method === "PUT") {</span> <span class="cstat-no" title="statement not covered" > if (typeof c.data !== "string") {</span> <span class="cstat-no" title="statement not covered" > throw new TypeError("ajax request object prop 'data' must be present on POST or PUT requests and must be a string. Provided\n" + c.data + " (" + typeof c.data + ")");</span> } } &nbsp; <span class="cstat-no" title="statement not covered" > ["onOpen", "onHeaders", "onSuccess", "onFailure"].forEach(<span class="fstat-no" title="function not covered" >function(key) {</span></span> <span class="cstat-no" title="statement not covered" > c[key] = mapCallbacks(c[key]);</span> }); &nbsp; <span class="cstat-no" title="statement not covered" > return c;</span> } &nbsp; module.exports = finalizeParams; &nbsp;</pre></td></tr> </table></pre> <div class='push'></div><!-- for sticky footer --> </div><!-- /wrapper --> <div class='footer quiet pad2 space-top1 center small'> Code coverage generated by <a href="http://istanbul-js.org/" target="_blank">istanbul</a> at Thu May 19 2016 15:43:41 GMT-0400 (EDT) </div> </div> <script src="../prettify.js"></script> <script> window.onload = function () { if (typeof prettyPrint === 'function') { prettyPrint(); } }; </script> <script src="../sorter.js"></script> </body> </html>