Comparing version 3.0.1 to 4.0.0
@@ -9,6 +9,8 @@ "use strict"; | ||
function configureLogger(config) { | ||
// eslint-disable-next-line no-param-reassign | ||
config = config || {}; | ||
// Function which prints errors. | ||
if (!config.hasOwnProperty("logger")) { | ||
config.logger = function () { }; | ||
// eslint-disable-next-line no-empty-function | ||
config.logger = function() {}; | ||
} | ||
@@ -27,3 +29,7 @@ // When set to true, any errors logged will be thrown immediately; | ||
var msg = label + " threw exception: "; | ||
var err = { name: e.name || label, message: e.message || e.toString(), stack: e.stack }; | ||
var err = { | ||
name: e.name || label, | ||
message: e.message || e.toString(), | ||
stack: e.stack | ||
}; | ||
@@ -30,0 +36,0 @@ function throwLoggedError() { |
@@ -18,3 +18,3 @@ "use strict"; | ||
function not(fn) { | ||
return function () { | ||
return function() { | ||
return !fn.apply(this, arguments); | ||
@@ -24,5 +24,7 @@ }; | ||
function hasListenerFilter(listener, capture) { | ||
return function (listenerSpec) { | ||
return listenerSpec.capture === capture | ||
&& listenerSpec.listener === listener; | ||
return function(listenerSpec) { | ||
return ( | ||
listenerSpec.capture === capture && | ||
listenerSpec.listener === listener | ||
); | ||
}; | ||
@@ -33,3 +35,7 @@ } | ||
// https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener | ||
addEventListener: function addEventListener(event, listener, providedOptions) { | ||
addEventListener: function addEventListener( | ||
event, | ||
listener, | ||
providedOptions | ||
) { | ||
// 3. Let capture, passive, and once be the result of flattening more options. | ||
@@ -45,3 +51,3 @@ // Flatten property before executing step 2, | ||
// 2. If callback is null, then return. | ||
if (listener == null) { | ||
if (listener === null || listener === undefined) { | ||
return; | ||
@@ -58,3 +64,7 @@ } | ||
// callback, capture is capture, passive is passive, and once is once. | ||
if (!this.eventListeners[event].some(hasListenerFilter(listener, options.capture))) { | ||
if ( | ||
!this.eventListeners[event].some( | ||
hasListenerFilter(listener, options.capture) | ||
) | ||
) { | ||
this.eventListeners[event].push({ | ||
@@ -69,3 +79,7 @@ listener: listener, | ||
// https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener | ||
removeEventListener: function removeEventListener(event, listener, providedOptions) { | ||
removeEventListener: function removeEventListener( | ||
event, | ||
listener, | ||
providedOptions | ||
) { | ||
if (!this.eventListeners || !this.eventListeners[event]) { | ||
@@ -82,4 +96,5 @@ return; | ||
// removed to true and remove it from the associated list of event listeners. | ||
this.eventListeners[event] = this.eventListeners[event] | ||
.filter(not(hasListenerFilter(listener, options.capture))); | ||
this.eventListeners[event] = this.eventListeners[event].filter( | ||
not(hasListenerFilter(listener, options.capture)) | ||
); | ||
}, | ||
@@ -98,6 +113,6 @@ | ||
// before running dispatch loop to avoid nested dispatch issues | ||
self.eventListeners[type] = listeners.filter(function (listenerSpec) { | ||
self.eventListeners[type] = listeners.filter(function(listenerSpec) { | ||
return !listenerSpec.once; | ||
}); | ||
listeners.forEach(function (listenerSpec) { | ||
listeners.forEach(function(listenerSpec) { | ||
var listener = listenerSpec.listener; | ||
@@ -104,0 +119,0 @@ if (typeof listener === "function") { |
@@ -8,3 +8,3 @@ "use strict"; | ||
Event.prototype = { | ||
initEvent: function (type, bubbles, cancelable, target) { | ||
initEvent: function(type, bubbles, cancelable, target) { | ||
this.type = type; | ||
@@ -17,5 +17,6 @@ this.bubbles = bubbles; | ||
stopPropagation: function () {}, | ||
// eslint-disable-next-line no-empty-function | ||
stopPropagation: function() {}, | ||
preventDefault: function () { | ||
preventDefault: function() { | ||
this.defaultPrevented = true; | ||
@@ -22,0 +23,0 @@ } |
@@ -7,5 +7,11 @@ "use strict"; | ||
this.initEvent(type, false, false, target); | ||
this.loaded = typeof progressEventRaw.loaded === "number" ? progressEventRaw.loaded : null; | ||
this.total = typeof progressEventRaw.total === "number" ? progressEventRaw.total : null; | ||
this.lengthComputable = !!progressEventRaw.total; | ||
this.loaded = | ||
typeof progressEventRaw.loaded === "number" | ||
? progressEventRaw.loaded | ||
: null; | ||
this.total = | ||
typeof progressEventRaw.total === "number" | ||
? progressEventRaw.total | ||
: null; | ||
this.lengthComputable = Boolean(progressEventRaw.total); | ||
} | ||
@@ -12,0 +18,0 @@ |
@@ -6,2 +6,3 @@ "use strict"; | ||
// eslint-disable-next-line no-empty-function | ||
function Server() {} | ||
@@ -26,4 +27,7 @@ Server.prototype = fakeServer; | ||
this.clock.setTimeout = function (fn, timeout) { | ||
server.longestTimeout = Math.max(timeout, server.longestTimeout || 0); | ||
this.clock.setTimeout = function(fn, timeout) { | ||
server.longestTimeout = Math.max( | ||
timeout, | ||
server.longestTimeout || 0 | ||
); | ||
@@ -33,4 +37,7 @@ return clockSetTimeout.apply(this, arguments); | ||
this.clock.setInterval = function (fn, timeout) { | ||
server.longestTimeout = Math.max(timeout, server.longestTimeout || 0); | ||
this.clock.setInterval = function(fn, timeout) { | ||
server.longestTimeout = Math.max( | ||
timeout, | ||
server.longestTimeout || 0 | ||
); | ||
@@ -37,0 +44,0 @@ return clockSetInterval.apply(this, arguments); |
@@ -20,9 +20,12 @@ "use strict"; | ||
if (!supportsArrayBuffer) { | ||
throw new TypeError("Fake server response body should be a string, but was " + | ||
typeof response[2]); | ||
throw new TypeError( | ||
"Fake server response body should be a string, but was " + | ||
typeof response[2] | ||
); | ||
} else if (!(response[2] instanceof ArrayBuffer)) { | ||
throw new TypeError( | ||
"Fake server response body should be a string or ArrayBuffer, but was " + | ||
typeof response[2] | ||
); | ||
} | ||
else if (!(response[2] instanceof ArrayBuffer)) { | ||
throw new TypeError("Fake server response body should be a string or ArrayBuffer, but was " + | ||
typeof response[2]); | ||
} | ||
} | ||
@@ -34,3 +37,3 @@ | ||
function getDefaultWindowLocation() { | ||
return { "host": "localhost", "protocol": "http" }; | ||
return { host: "localhost", protocol: "http" }; | ||
} | ||
@@ -49,3 +52,6 @@ | ||
if ((typeof window.window !== "undefined") && (typeof window.window.location !== "undefined")) { | ||
if ( | ||
typeof window.window !== "undefined" && | ||
typeof window.window.location !== "undefined" | ||
) { | ||
// React Native on Android places location on window.window | ||
@@ -62,3 +68,6 @@ return window.window.location; | ||
var url = response.url; | ||
var matchUrl = !url || url === reqUrl || (typeof url.test === "function" && url.test(reqUrl)); | ||
var matchUrl = | ||
!url || | ||
url === reqUrl || | ||
(typeof url.test === "function" && url.test(reqUrl)); | ||
@@ -82,3 +91,7 @@ return matchMethod && matchUrl; | ||
var ru = response.url; | ||
var args = [request].concat(ru && typeof ru.exec === "function" ? ru.exec(requestUrl).slice(1) : []); | ||
var args = [request].concat( | ||
ru && typeof ru.exec === "function" | ||
? ru.exec(requestUrl).slice(1) | ||
: [] | ||
); | ||
return response.response.apply(response, args); | ||
@@ -110,3 +123,3 @@ } | ||
var fakeServer = { | ||
create: function (config) { | ||
create: function(config) { | ||
var server = Object.create(this); | ||
@@ -120,5 +133,4 @@ server.configure(config); | ||
this.xhr.onCreate = function (xhrObj) { | ||
xhrObj.unsafeHeadersEnabled = function () { | ||
this.xhr.onCreate = function(xhrObj) { | ||
xhrObj.unsafeHeadersEnabled = function() { | ||
return !(server.unsafeHeadersEnabled === false); | ||
@@ -132,16 +144,17 @@ }; | ||
configure: function (config) { | ||
configure: function(config) { | ||
var self = this; | ||
var whitelist = { | ||
"autoRespond": true, | ||
"autoRespondAfter": true, | ||
"respondImmediately": true, | ||
"fakeHTTPMethods": true, | ||
"logger": true, | ||
"unsafeHeadersEnabled": true | ||
autoRespond: true, | ||
autoRespondAfter: true, | ||
respondImmediately: true, | ||
fakeHTTPMethods: true, | ||
logger: true, | ||
unsafeHeadersEnabled: true | ||
}; | ||
// eslint-disable-next-line no-param-reassign | ||
config = config || {}; | ||
Object.keys(config).forEach(function (setting) { | ||
Object.keys(config).forEach(function(setting) { | ||
if (setting in whitelist) { | ||
@@ -161,3 +174,3 @@ self[setting] = config[setting]; | ||
xhrObj.onSend = function () { | ||
xhrObj.onSend = function() { | ||
server.handleRequest(this); | ||
@@ -168,3 +181,3 @@ | ||
} else if (server.autoRespond && !server.responding) { | ||
setTimeout(function () { | ||
setTimeout(function() { | ||
server.responding = false; | ||
@@ -181,3 +194,5 @@ server.respond(); | ||
if (this.fakeHTTPMethods && /post/i.test(request.method)) { | ||
var matches = (request.requestBody || "").match(/_method=([^\b;]+)/); | ||
var matches = (request.requestBody || "").match( | ||
/_method=([^\b;]+)/ | ||
); | ||
return matches ? matches[1] : request.method; | ||
@@ -197,3 +212,3 @@ } | ||
logger: function () { | ||
logger: function() { | ||
// no-op; override via configure() | ||
@@ -222,3 +237,5 @@ }, | ||
if (arguments.length === 1) { | ||
// eslint-disable-next-line no-param-reassign | ||
body = method; | ||
// eslint-disable-next-line no-param-reassign | ||
url = method = null; | ||
@@ -228,4 +245,7 @@ } | ||
if (arguments.length === 2) { | ||
// eslint-disable-next-line no-param-reassign | ||
body = url; | ||
// eslint-disable-next-line no-param-reassign | ||
url = method; | ||
// eslint-disable-next-line no-param-reassign | ||
method = null; | ||
@@ -236,3 +256,4 @@ } | ||
method: method, | ||
url: typeof url === "string" && url !== "" ? pathToRegexp(url) : url, | ||
url: | ||
typeof url === "string" && url !== "" ? pathToRegexp(url) : url, | ||
response: typeof body === "function" ? body : responseArray(body) | ||
@@ -251,3 +272,3 @@ }); | ||
requests.forEach(function (request) { | ||
requests.forEach(function(request) { | ||
self.processRequest(request); | ||
@@ -254,0 +275,0 @@ }); |
"use strict"; | ||
exports.isSupported = (function () { | ||
exports.isSupported = (function() { | ||
try { | ||
return !!new Blob(); | ||
return Boolean(new Blob()); | ||
} catch (e) { | ||
return false; | ||
} | ||
}()); | ||
})(); |
"use strict"; | ||
var GlobalTextEncoder = typeof TextEncoder !== "undefined" | ||
? TextEncoder | ||
: require("@sinonjs/text-encoding").TextEncoder; | ||
var GlobalTextEncoder = | ||
typeof TextEncoder !== "undefined" | ||
? TextEncoder | ||
: require("@sinonjs/text-encoding").TextEncoder; | ||
var globalObject = require("@sinonjs/commons").global; | ||
@@ -25,3 +26,3 @@ var configureLogError = require("../configure-logger"); | ||
if (supportsActiveX) { | ||
return function () { | ||
return function() { | ||
return new globalScope.ActiveXObject("MSXML2.XMLHTTP.3.0"); | ||
@@ -40,20 +41,20 @@ }; | ||
"Accept-Encoding": true, | ||
"Connection": true, | ||
Connection: true, | ||
"Content-Length": true, | ||
"Cookie": true, | ||
"Cookie2": true, | ||
Cookie: true, | ||
Cookie2: true, | ||
"Content-Transfer-Encoding": true, | ||
"Date": true, | ||
"DNT": true, | ||
"Expect": true, | ||
"Host": true, | ||
Date: true, | ||
DNT: true, | ||
Expect: true, | ||
Host: true, | ||
"Keep-Alive": true, | ||
"Origin": true, | ||
"Referer": true, | ||
"TE": true, | ||
"Trailer": true, | ||
Origin: true, | ||
Referer: true, | ||
TE: true, | ||
Trailer: true, | ||
"Transfer-Encoding": true, | ||
"Upgrade": true, | ||
Upgrade: true, | ||
"User-Agent": true, | ||
"Via": true | ||
Via: true | ||
}; | ||
@@ -63,6 +64,14 @@ | ||
var self = this; | ||
var events = ["loadstart", "progress", "abort", "error", "load", "timeout", "loadend"]; | ||
var events = [ | ||
"loadstart", | ||
"progress", | ||
"abort", | ||
"error", | ||
"load", | ||
"timeout", | ||
"loadend" | ||
]; | ||
function addEventListener(eventName) { | ||
self.addEventListener(eventName, function (event) { | ||
self.addEventListener(eventName, function(event) { | ||
var listener = self["on" + eventName]; | ||
@@ -88,3 +97,3 @@ | ||
function getHeader(headers, header) { | ||
var foundHeader = Object.keys(headers).filter(function (h) { | ||
var foundHeader = Object.keys(headers).filter(function(h) { | ||
return h.toLowerCase() === header.toLowerCase(); | ||
@@ -105,12 +114,16 @@ }); | ||
if (responseType === "arraybuffer") { | ||
if (!isString && !(body instanceof ArrayBuffer)) { | ||
error = new Error("Attempted to respond to fake XMLHttpRequest with " + | ||
body + ", which is not a string or ArrayBuffer."); | ||
error = new Error( | ||
"Attempted to respond to fake XMLHttpRequest with " + | ||
body + | ||
", which is not a string or ArrayBuffer." | ||
); | ||
error.name = "InvalidBodyException"; | ||
} | ||
} | ||
else if (!isString) { | ||
error = new Error("Attempted to respond to fake XMLHttpRequest with " + | ||
body + ", which is not a string."); | ||
} else if (!isString) { | ||
error = new Error( | ||
"Attempted to respond to fake XMLHttpRequest with " + | ||
body + | ||
", which is not a string." | ||
); | ||
error.name = "InvalidBodyException"; | ||
@@ -133,3 +146,6 @@ } | ||
function isXmlContentType(contentType) { | ||
return !contentType || /(text\/xml)|(application\/xml)|(\+xml)/.test(contentType); | ||
return ( | ||
!contentType || | ||
/(text\/xml)|(application\/xml)|(\+xml)/.test(contentType) | ||
); | ||
} | ||
@@ -147,13 +163,19 @@ | ||
function fakeXMLHttpRequestFor(globalScope) { | ||
var isReactNative = globalScope.navigator && globalScope.navigator.product === "ReactNative"; | ||
var isReactNative = | ||
globalScope.navigator && | ||
globalScope.navigator.product === "ReactNative"; | ||
var sinonXhr = { XMLHttpRequest: globalScope.XMLHttpRequest }; | ||
sinonXhr.GlobalXMLHttpRequest = globalScope.XMLHttpRequest; | ||
sinonXhr.GlobalActiveXObject = globalScope.ActiveXObject; | ||
sinonXhr.supportsActiveX = typeof sinonXhr.GlobalActiveXObject !== "undefined"; | ||
sinonXhr.supportsActiveX = | ||
typeof sinonXhr.GlobalActiveXObject !== "undefined"; | ||
sinonXhr.supportsXHR = typeof sinonXhr.GlobalXMLHttpRequest !== "undefined"; | ||
sinonXhr.workingXHR = getWorkingXHR(globalScope); | ||
sinonXhr.supportsTimeout = | ||
(sinonXhr.supportsXHR && "timeout" in (new sinonXhr.GlobalXMLHttpRequest())); | ||
sinonXhr.supportsCORS = isReactNative || | ||
(sinonXhr.supportsXHR && "withCredentials" in (new sinonXhr.GlobalXMLHttpRequest())); | ||
sinonXhr.supportsXHR && | ||
"timeout" in new sinonXhr.GlobalXMLHttpRequest(); | ||
sinonXhr.supportsCORS = | ||
isReactNative || | ||
(sinonXhr.supportsXHR && | ||
"withCredentials" in new sinonXhr.GlobalXMLHttpRequest()); | ||
@@ -200,11 +222,18 @@ // Note that for FakeXMLHttpRequest to work pre ES5 | ||
// largest arity in XHR is 5 - XHR#open | ||
var apply = function (obj, method, args) { | ||
var apply = function(obj, method, args) { | ||
switch (args.length) { | ||
case 0: return obj[method](); | ||
case 1: return obj[method](args[0]); | ||
case 2: return obj[method](args[0], args[1]); | ||
case 3: return obj[method](args[0], args[1], args[2]); | ||
case 4: return obj[method](args[0], args[1], args[2], args[3]); | ||
case 5: return obj[method](args[0], args[1], args[2], args[3], args[4]); | ||
default: throw new Error("Unhandled case"); | ||
case 0: | ||
return obj[method](); | ||
case 1: | ||
return obj[method](args[0]); | ||
case 2: | ||
return obj[method](args[0], args[1]); | ||
case 3: | ||
return obj[method](args[0], args[1], args[2]); | ||
case 4: | ||
return obj[method](args[0], args[1], args[2], args[3]); | ||
case 5: | ||
return obj[method](args[0], args[1], args[2], args[3], args[4]); | ||
default: | ||
throw new Error("Unhandled case"); | ||
} | ||
@@ -229,4 +258,4 @@ }; | ||
"removeEventListener" | ||
].forEach(function (method) { | ||
fakeXhr[method] = function () { | ||
].forEach(function(method) { | ||
fakeXhr[method] = function() { | ||
return apply(xhr, method, arguments); | ||
@@ -236,3 +265,3 @@ }; | ||
fakeXhr.send = function () { | ||
fakeXhr.send = function() { | ||
// Ref: https://xhr.spec.whatwg.org/#the-responsetype-attribute | ||
@@ -245,4 +274,4 @@ if (xhr.responseType !== fakeXhr.responseType) { | ||
var copyAttrs = function (args) { | ||
args.forEach(function (attr) { | ||
var copyAttrs = function(args) { | ||
args.forEach(function(attr) { | ||
fakeXhr[attr] = xhr[attr]; | ||
@@ -252,3 +281,3 @@ }); | ||
var stateChangeStart = function () { | ||
var stateChangeStart = function() { | ||
fakeXhr.readyState = xhr.readyState; | ||
@@ -272,5 +301,9 @@ if (xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) { | ||
var stateChangeEnd = function () { | ||
var stateChangeEnd = function() { | ||
if (fakeXhr.onreadystatechange) { | ||
fakeXhr.onreadystatechange.call(fakeXhr, { target: fakeXhr, currentTarget: fakeXhr }); | ||
// eslint-disable-next-line no-useless-call | ||
fakeXhr.onreadystatechange.call(fakeXhr, { | ||
target: fakeXhr, | ||
currentTarget: fakeXhr | ||
}); | ||
} | ||
@@ -287,5 +320,5 @@ }; | ||
Object.keys(fakeXhr.eventListeners).forEach(function (event) { | ||
Object.keys(fakeXhr.eventListeners).forEach(function(event) { | ||
/*eslint-disable no-loop-func*/ | ||
fakeXhr.eventListeners[event].forEach(function (handler) { | ||
fakeXhr.eventListeners[event].forEach(function(handler) { | ||
xhr.addEventListener(event, handler.listener, { | ||
@@ -320,3 +353,6 @@ capture: handler.capture, | ||
function verifyHeadersReceived(xhr) { | ||
if (xhr.async && xhr.readyState !== FakeXMLHttpRequest.HEADERS_RECEIVED) { | ||
if ( | ||
xhr.async && | ||
xhr.readyState !== FakeXMLHttpRequest.HEADERS_RECEIVED | ||
) { | ||
throw new Error("No headers received"); | ||
@@ -363,4 +399,7 @@ } | ||
if (xhr.readyState !== FakeXMLHttpRequest.UNSENT && xhr.sendFlag | ||
&& xhr.readyState !== FakeXMLHttpRequest.DONE) { | ||
if ( | ||
xhr.readyState !== FakeXMLHttpRequest.UNSENT && | ||
xhr.sendFlag && | ||
xhr.readyState !== FakeXMLHttpRequest.DONE | ||
) { | ||
xhr.readyStateChange(FakeXMLHttpRequest.DONE); | ||
@@ -398,4 +437,8 @@ xhr.sendFlag = false; | ||
return result.getElementsByTagNameNS(parsererrorNS, "parsererror").length | ||
? null : result; | ||
return result.getElementsByTagNameNS( | ||
parsererrorNS, | ||
"parsererror" | ||
).length | ||
? null | ||
: result; | ||
} | ||
@@ -405,4 +448,3 @@ var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM"); | ||
xmlDoc.loadXML(text); | ||
return xmlDoc.parseError.errorCode !== 0 | ||
? null : xmlDoc; | ||
return xmlDoc.parseError.errorCode !== 0 ? null : xmlDoc; | ||
} catch (e) { | ||
@@ -476,3 +518,3 @@ // Unable to parse XML - no biggie | ||
var xhrArgs = arguments; | ||
var defake = FakeXMLHttpRequest.filters.some(function (filter) { | ||
var defake = FakeXMLHttpRequest.filters.some(function(filter) { | ||
return filter.apply(this, xhrArgs); | ||
@@ -491,3 +533,8 @@ }); | ||
var readyStateChangeEvent = new sinonEvent.Event("readystatechange", false, false, this); | ||
var readyStateChangeEvent = new sinonEvent.Event( | ||
"readystatechange", | ||
false, | ||
false, | ||
this | ||
); | ||
var event, progress; | ||
@@ -505,6 +552,9 @@ | ||
if (this.timedOut || this.aborted || this.status === 0) { | ||
progress = {loaded: 0, total: 0}; | ||
event = (this.timedOut && "timeout") || (this.aborted && "abort") || "error"; | ||
progress = { loaded: 0, total: 0 }; | ||
event = | ||
(this.timedOut && "timeout") || | ||
(this.aborted && "abort") || | ||
"error"; | ||
} else { | ||
progress = {loaded: 100, total: 100}; | ||
progress = { loaded: 100, total: 100 }; | ||
event = "load"; | ||
@@ -514,10 +564,22 @@ } | ||
if (supportsProgress) { | ||
this.upload.dispatchEvent(new sinonEvent.ProgressEvent("progress", progress, this)); | ||
this.upload.dispatchEvent(new sinonEvent.ProgressEvent(event, progress, this)); | ||
this.upload.dispatchEvent(new sinonEvent.ProgressEvent("loadend", progress, this)); | ||
this.upload.dispatchEvent( | ||
new sinonEvent.ProgressEvent("progress", progress, this) | ||
); | ||
this.upload.dispatchEvent( | ||
new sinonEvent.ProgressEvent(event, progress, this) | ||
); | ||
this.upload.dispatchEvent( | ||
new sinonEvent.ProgressEvent("loadend", progress, this) | ||
); | ||
} | ||
this.dispatchEvent(new sinonEvent.ProgressEvent("progress", progress, this)); | ||
this.dispatchEvent(new sinonEvent.ProgressEvent(event, progress, this)); | ||
this.dispatchEvent(new sinonEvent.ProgressEvent("loadend", progress, this)); | ||
this.dispatchEvent( | ||
new sinonEvent.ProgressEvent("progress", progress, this) | ||
); | ||
this.dispatchEvent( | ||
new sinonEvent.ProgressEvent(event, progress, this) | ||
); | ||
this.dispatchEvent( | ||
new sinonEvent.ProgressEvent("loadend", progress, this) | ||
); | ||
} | ||
@@ -531,3 +593,6 @@ | ||
if (typeof value !== "string") { | ||
throw new TypeError("By RFC7230, section 3.2.4, header values should be strings. Got " + typeof value); | ||
throw new TypeError( | ||
"By RFC7230, section 3.2.4, header values should be strings. Got " + | ||
typeof value | ||
); | ||
} | ||
@@ -541,6 +606,14 @@ verifyState(this); | ||
if (checkUnsafeHeaders && (getHeader(unsafeHeaders, header) !== null || /^(Sec-|Proxy-)/i.test(header))) { | ||
throw new Error("Refused to set unsafe header \"" + header + "\""); | ||
if ( | ||
checkUnsafeHeaders && | ||
(getHeader(unsafeHeaders, header) !== null || | ||
/^(Sec-|Proxy-)/i.test(header)) | ||
) { | ||
throw new Error( | ||
// eslint-disable-next-line quotes | ||
'Refused to set unsafe header "' + header + '"' | ||
); | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
value = normalizeHeaderValue(value); | ||
@@ -568,5 +641,5 @@ | ||
var responseHeaders = this.responseHeaders = {}; | ||
var responseHeaders = (this.responseHeaders = {}); | ||
Object.keys(headers).forEach(function (header) { | ||
Object.keys(headers).forEach(function(header) { | ||
responseHeaders[header] = headers[header]; | ||
@@ -587,8 +660,13 @@ }); | ||
if (!/^(head)$/i.test(this.method)) { | ||
var contentType = getHeader(this.requestHeaders, "Content-Type"); | ||
var contentType = getHeader( | ||
this.requestHeaders, | ||
"Content-Type" | ||
); | ||
if (this.requestHeaders[contentType]) { | ||
var value = this.requestHeaders[contentType].split(";"); | ||
this.requestHeaders[contentType] = value[0] + ";charset=utf-8"; | ||
this.requestHeaders[contentType] = | ||
value[0] + ";charset=utf-8"; | ||
} else if (supportsFormData && !(data instanceof FormData)) { | ||
this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8"; | ||
this.requestHeaders["Content-Type"] = | ||
"text/plain;charset=utf-8"; | ||
} | ||
@@ -608,3 +686,7 @@ | ||
// Only listen if setInterval and Date are a stubbed. | ||
if (sinonXhr.supportsTimeout && typeof setInterval.clock === "object" && typeof Date.clock === "object") { | ||
if ( | ||
sinonXhr.supportsTimeout && | ||
typeof setInterval.clock === "object" && | ||
typeof Date.clock === "object" | ||
) { | ||
var initiatedTime = Date.now(); | ||
@@ -618,11 +700,16 @@ var self = this; | ||
// https://xhr.spec.whatwg.org/#dfnReturnLink-2 | ||
var clearIntervalId = setInterval(function () { | ||
var clearIntervalId = setInterval(function() { | ||
// Check if the readyState has been reset or is done. If this is the case, there | ||
// should be no timeout. This will also prevent aborted requests and | ||
// fakeServerWithClock from triggering unnecessary responses. | ||
if (self.readyState === FakeXMLHttpRequest.UNSENT | ||
|| self.readyState === FakeXMLHttpRequest.DONE) { | ||
if ( | ||
self.readyState === FakeXMLHttpRequest.UNSENT || | ||
self.readyState === FakeXMLHttpRequest.DONE | ||
) { | ||
clearInterval(clearIntervalId); | ||
} else if (typeof self.timeout === "number" && self.timeout > 0) { | ||
if (Date.now() >= (initiatedTime + self.timeout)) { | ||
} else if ( | ||
typeof self.timeout === "number" && | ||
self.timeout > 0 | ||
) { | ||
if (Date.now() >= initiatedTime + self.timeout) { | ||
self.triggerTimeout(); | ||
@@ -635,3 +722,5 @@ clearInterval(clearIntervalId); | ||
this.dispatchEvent(new sinonEvent.Event("loadstart", false, false, this)); | ||
this.dispatchEvent( | ||
new sinonEvent.Event("loadstart", false, false, this) | ||
); | ||
}, | ||
@@ -645,3 +734,3 @@ | ||
error: function () { | ||
error: function() { | ||
clearResponse(this); | ||
@@ -671,2 +760,3 @@ this.errorFlag = true; | ||
// eslint-disable-next-line no-param-reassign | ||
header = getHeader(this.responseHeaders, header); | ||
@@ -685,3 +775,3 @@ | ||
.filter(excludeSetCookie2Header) | ||
.reduce(function (prev, header) { | ||
.reduce(function(prev, header) { | ||
var value = responseHeaders[header]; | ||
@@ -699,5 +789,8 @@ | ||
verifyResponseBodyType(body, this.responseType); | ||
var contentType = this.overriddenMimeType || this.getResponseHeader("Content-Type"); | ||
var contentType = | ||
this.overriddenMimeType || | ||
this.getResponseHeader("Content-Type"); | ||
var isTextResponse = this.responseType === "" || this.responseType === "text"; | ||
var isTextResponse = | ||
this.responseType === "" || this.responseType === "text"; | ||
clearResponse(this); | ||
@@ -712,3 +805,6 @@ if (this.async) { | ||
if (isTextResponse) { | ||
this.responseText = this.response += body.substring(index, index + chunkSize); | ||
this.responseText = this.response += body.substring( | ||
index, | ||
index + chunkSize | ||
); | ||
} | ||
@@ -719,3 +815,7 @@ index += chunkSize; | ||
this.response = convertResponseBody(this.responseType, contentType, body); | ||
this.response = convertResponseBody( | ||
this.responseType, | ||
contentType, | ||
body | ||
); | ||
if (isTextResponse) { | ||
@@ -727,4 +827,9 @@ this.responseText = this.response; | ||
this.responseXML = this.response; | ||
} else if (this.responseType === "" && isXmlContentType(contentType)) { | ||
this.responseXML = FakeXMLHttpRequest.parseXML(this.responseText); | ||
} else if ( | ||
this.responseType === "" && | ||
isXmlContentType(contentType) | ||
) { | ||
this.responseXML = FakeXMLHttpRequest.parseXML( | ||
this.responseText | ||
); | ||
} | ||
@@ -742,3 +847,9 @@ this.readyStateChange(FakeXMLHttpRequest.DONE); | ||
if (supportsProgress) { | ||
this.upload.dispatchEvent(new sinonEvent.ProgressEvent("progress", progressEventRaw, this.upload)); | ||
this.upload.dispatchEvent( | ||
new sinonEvent.ProgressEvent( | ||
"progress", | ||
progressEventRaw, | ||
this.upload | ||
) | ||
); | ||
} | ||
@@ -749,3 +860,9 @@ }, | ||
if (supportsProgress) { | ||
this.dispatchEvent(new sinonEvent.ProgressEvent("progress", progressEventRaw, this)); | ||
this.dispatchEvent( | ||
new sinonEvent.ProgressEvent( | ||
"progress", | ||
progressEventRaw, | ||
this | ||
) | ||
); | ||
} | ||
@@ -756,3 +873,5 @@ }, | ||
if (supportsCustomEvent) { | ||
this.upload.dispatchEvent(new sinonEvent.CustomEvent("error", {detail: error})); | ||
this.upload.dispatchEvent( | ||
new sinonEvent.CustomEvent("error", { detail: error }) | ||
); | ||
} | ||
@@ -802,4 +921,6 @@ }, | ||
globalScope.ActiveXObject = function ActiveXObject(objId) { | ||
if (objId === "Microsoft.XMLHTTP" || /^Msxml2\.XMLHTTP/i.test(objId)) { | ||
if ( | ||
objId === "Microsoft.XMLHTTP" || | ||
/^Msxml2\.XMLHTTP/i.test(objId) | ||
) { | ||
return new FakeXMLHttpRequest(); | ||
@@ -806,0 +927,0 @@ } |
{ | ||
"name": "nise", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Fake XHR and server", | ||
@@ -46,18 +46,21 @@ "keywords": [ | ||
"devDependencies": { | ||
"@sinonjs/referee": "^3.2.0", | ||
"@sinonjs/referee": "^4.0.0", | ||
"browserify": "^16.2.3", | ||
"eslint": "^6.1.0", | ||
"eslint-config-sinon": "^1.0.1", | ||
"eslint-config-prettier": "^6.9.0", | ||
"eslint-config-sinon": "^3.0.1", | ||
"eslint-plugin-ie11": "1.0.0", | ||
"eslint-plugin-mocha": "^6.1.1", | ||
"husky": "^3.0.7", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"husky": "^4.2.1", | ||
"jsdom": "^15.1.1", | ||
"jsdom-global": "3.0.2", | ||
"mocha": "^6.2.0", | ||
"mocha": "^7.0.1", | ||
"mochify": "^6.6.0", | ||
"nyc": "^14.1.1", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.19.1", | ||
"proxyquire": "^2.1.3", | ||
"proxyquire-universal": "^2.1.0", | ||
"proxyquireify": "^3.2.1", | ||
"sinon": "^7.3.2" | ||
"sinon": "^8.0.2" | ||
}, | ||
@@ -64,0 +67,0 @@ "dependencies": { |
@@ -6,2 +6,3 @@ # nise (偽) | ||
[![codecov](https://codecov.io/gh/sinonjs/nise/branch/master/graph/badge.svg)](https://codecov.io/gh/sinonjs/nise) | ||
<a href="CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a> | ||
@@ -8,0 +9,0 @@ fake XHR and Server |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
898396
12092
86
19