Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.2.1 lint and build

  • Loading branch information...
commit 959a15303ce370690e15fcb4cf98bbb33fa25b23 1 parent 98e00a3
@leobalter authored
View
42 dist/Dexter.js
@@ -1,4 +1,4 @@
-/*! Dexter JS - v0.1.0 - 2012-07-18
+/*! Dexter JS - v0.2.1 - 2012-07-24
* https://github.com/leobalter/DexterJS
* Copyright (c) 2012 Leonardo Balter; Licensed MIT, GPL */
@@ -18,12 +18,14 @@
actions = {
'spy' : function( that, args ) {
+ // call order issues
+ var returned = that._oldCall.apply( this, args );
+
if ( typeof( that.callback ) === 'function' ) {
- that.callback.apply( this, args );
+ that.callback.apply( this, args );
}
// calls the original method
- return that._oldCall.apply( this, args );
-
+ return returned;
},
'stub' : function( that, args ) {
if ( typeof( that.callback ) === 'function' ) {
@@ -198,7 +200,7 @@
***/
function verifyState( state, sendFlag ) {
if ( state !== 1 || sendFlag ) {
- throw new Error( 'INVALID_STATE_ERR' );
+ throw new Error( 'INVALID_STATE_ERR' );
}
}
@@ -338,19 +340,30 @@
* fake .send
***/
send : function( data ) {
+ var reqHeaders;
// readyState verification (xhr should be already opened)
verifyState( this.readyState, this.sendFlag );
+ if ( !/^(get|head)$/i.test( this.method ) ) {
+ if (this.requestHeaders[ 'Content-Type' ]) {
+ reqHeaders = this.requestHeaders[ 'Content-Type' ].split( ';' );
+ this.requestHeaders[ 'Content-Type' ] = reqHeaders[ 0 ] + ';charset=utf-8';
+ } else {
+ this.requestHeaders[ 'Content-Type' ] = "text/plain;charset=utf-8";
+ }
+ this.requestBody = data;
+ }
+
// setting properties
this.errorFlag = false;
- this.sendFlag = this.async;
+ this.sendFlag = true; // this.async;
// trigger readystatechange with Opened status
this.__DexterStateChange( this.OPENED );
// hummm if think I won´t need this, omg, where´s the specification
if ( typeof( this.onSend ) === 'function' ) {
- this.onSend( this );
+ this.onSend( this );
}
},
/***
@@ -581,7 +594,6 @@
* its returned object. Not on the XHR itself.
***/
CreateFakeXHR.prototype = {
- // TODO: test
/***
* interface to export xhr.__DexterRespond and set this.doneRequests
***/
@@ -603,10 +615,22 @@
this.doneRequests.push( xhr );
},
/***
+ * uses a Dexter.spy on xhr send requests
+ ***/
+ spy : function( callback ) {
+ var spy = Dexter.spy( fakeXHRObj, 'send', callback );
+ // this.__spy will be used on .restore();
+ this.__spy = spy;
+ return spy;
+ },
+ /***
* restore the XHR objects to their original states, defaking them
* this won´t affect already created fake ajax requests.
***/
restore : function() {
+ if ( this.__spy ) {
+ this.__spy.restore();
+ }
if ( ajaxObjs.xhr ) {
globalObj.XMLHttpRequest = ajaxObjs.xhr;
}
@@ -624,4 +648,4 @@
return new CreateFakeXHR();
};
-}( this, Dexter ));
+}( this, Dexter ));
View
4 dist/Dexter.min.js
@@ -1,4 +1,4 @@
-/*! Dexter JS - v0.1.0 - 2012-07-18
+/*! Dexter JS - v0.2.1 - 2012-07-24
* https://github.com/leobalter/DexterJS
* Copyright (c) 2012 Leonardo Balter; Licensed MIT, GPL */
-(function(a){function e(a,b){this._oldCall=a[b],this._seenObj=a,this._seenMethod=b}function f(a,b,c,f){var g=this;this.called=0,this.isActive=!0;if(typeof c!="string")throw"Dexter should receive method name as a String";if(!b||typeof b[c]!="function")throw'Dexter should receive a valid object and method combination in arguments. Ex.: window & "alert".';typeof f=="function"&&(this.callback=f),e.call(this,b,c),b[c]=function(){var b=[].slice.apply(arguments);return g.called=g.called+1,d[a].call(this,g,b)}}var b,c,d;b=function(){this._seenObj[this._seenMethod]=this._oldCall,this.isActive=!1},d={spy:function(a,b){return typeof a.callback=="function"&&a.callback.apply(this,b),a._oldCall.apply(this,b)},stub:function(a,b){if(typeof a.callback=="function")return a.callback.apply(this,b)}},f.prototype={restore:b},a.Dexter={spy:function(a,b,c){return new f("spy",a,b,c)},stub:function(a,b,c){return new f("stub",a,b,c)}}})(this),function(a,b,c){function i(a,b){if(a!==1||b)throw new Error("INVALID_STATE_ERR")}function j(b){var c,d;return typeof a.DOMParser!="undefined"?(d=new a.DOMParser,c=d.parseFromString(b,"text/xml")):(c=new ActiveXObject("Microsoft.XMLDOM"),c.async="false",c.loadXML(b)),c}var d={},e,f,g,h;d.xhr=function(){var a;try{return a=new XMLHttpRequest,XMLHttpRequest}catch(b){return!1}}(),d.actX=function(){var a;try{return a=new ActiveXObject("Microsoft.XMLHTTP"),ActiveXObject}catch(b){return!1}}(),e={100:"Continue",101:"Switching Protocols",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",300:"Multiple Choice",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Request Entity Too Large",414:"Request-URI Too Long",415:"Unsupported Media Type",416:"Requested Range Not Satisfiable",417:"Expectation Failed",422:"Unprocessable Entity",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported"},f=["Accept-Charset","Accept-Encoding","Connection","Content-Length","Cookie","Cookie2","Content-Transfer-Encoding","Date","Expect","Host","Keep-Alive","Referer","TE","Trailer","Transfer-Encoding","Upgrade","User-Agent","Via"],typeof Array.prototype.indexOf=="undefined"&&(f.indexOf=function(a,b){var c=b||0,d=this.length;for(;c<d;c++)if(this[c]===a)return c;return-1}),g={UNSENT:0,OPENED:1,HEADERS_RECEIVED:2,LOADING:3,DONE:4,onabort:null,onerror:null,onload:null,onloadend:null,onloadstart:null,onprogress:null,onreadystatechange:null,ontimeout:null,readyState:0,response:"",responseText:"",responseType:"",responseXML:null,withCredentials:!1,status:0,statusText:"",timeout:0,abort:function(){this.aborted=!0,this.errorFlag=!0,this.method=null,this.url=null,this.async=c,this.username=null,this.password=null,this.responseText=null,this.responseXML=null,this.requestHeaders={},this.sendFlag=!1,this.readyState>this.UNSENT&&this.sendFlag?this.__DexterStateChange(this.DONE):this.__DexterStateChange(this.UNSENT)},getResponseHeader:function(a){var b,c=this.responseHeaders;if(this.readyState<this.HEADERS_RECEIVED)return null;if(/^Set-Cookie2?$/i.test(a))return null;a=a.toLowerCase();for(b in c)if(c.hasOwnProperty(b)&&b.toLowerCase()===a)return c[b];return null},open:function(a,b,c,d,e){if(typeof a=="undefined"||typeof b=="undefined")throw new Error("Not enough arguments");this.method=a,this.url=b,this.async=typeof c=="undefined"?!0:!!c,this.username=d,this.password=e,this.responseText=null,this.responseXML=null,this.requestHeaders={},this.sendFlag=!1,this.__DexterStateChange(this.OPENED)},send:function(a){i(this.readyState,this.sendFlag),this.errorFlag=!1,this.sendFlag=this.async,this.__DexterStateChange(this.OPENED),typeof this.onSend=="function"&&this.onSend(this)},setRequestHeader:function(a,b){i(this.readyState,this.sendFlag);if(f.indexOf(a)>=0||/^(Sec-|Proxy-)/.test(a))throw new Error('Refused to set unsafe header "'+a+'"');this.requestHeaders[a]?this.requestHeaders[a]+=","+b:this.requestHeaders[a]=b},getAllResponseHeaders:function(){var a="",b;if(this.readyState<this.HEADERS_RECEIVED)return"";for(b in this.responseHeaders)this.responseHeaders.hasOwnProperty(b)&&!/^Set-Cookie2?$/i.test(b)&&(a+=b+": "+this.responseHeaders[b]+"\r\n");return a},__DexterSetResponseHeaders:function(a){var b;this.responseHeaders={};for(b in a)a.hasOwnProperty(b)&&(this.responseHeaders[b]=a[b]);this.async&&this.__DexterStateChange(this.HEADERS_RECEIVED)},__DexterXHR:!0,__DexterStateChange:function(a){var b;this.readyState=a;if(typeof this.onreadystatechange=="function"){try{b=document.createEvent("Event"),b.initEvent("readystatechange",!1,!1)}catch(c){b={type:"readystatechange"}}this.onreadystatechange.call(this,[b])}},__DexterSetResponseBody:function(a){var b=this.chunkSize||10,c=0,d;this.responseText="";if(this.async)while(c<=a.length)this.__DexterStateChange(this.LOADING),this.responseText+=a.substring(c,c+=b);else this.responseText=a;d=this.getResponseHeader("Content-Type")||"";if(a&&/(text\/xml)|(application\/xml)|(\+xml)/.test(d))try{this.responseXML=j(a)}catch(e){}},__DexterRespond:function(a){var b=!1,c=a.body||"",d=a.headers||{},f=this.DONE;if(this.readyState===f)throw new Error("Request already done");this.__DexterSetResponseHeaders(d),this.status=a.status||200,this.statusText=e[this.status],this.__DexterSetResponseBody(c),this.async?this.__DexterStateChange(f):this.readyState=f}},h=function(){var b=this,c,e,f;this.requests=[],this.doneRequests=[],c=function(a,c){var e=[].slice.call(a);return b.requests.push(this),this.__DexterRef=Date.now(),c==="ActiveXObject"&&e[0]!=="Microsoft.XMLHTTP"?d(e):this},e=function(){c.call(this,arguments,"XMLHttpRequest")},f=function(){c.call(this,arguments,"ActiveXObject")},e.prototype=g,f.prototype=g,d.xhr&&(a.XMLHttpRequest=e),d.actX&&(a.ActiveXObject=f)},h.prototype={respond:function(a,b){var c;a=a||{},b?c=this.requests.splice(b,1)[0]:c=this.requests.shift(),c.__DexterRespond(a),this.doneRequests.push(c)},restore:function(){d.xhr&&(a.XMLHttpRequest=d.xhr),d.actX&&(a.ActiveXObject=d.actX)}},b.fakeXHR=function(){return new h}}(this,Dexter);
+(function(a){function e(a,b){this._oldCall=a[b],this._seenObj=a,this._seenMethod=b}function f(a,b,c,f){var g=this;this.called=0,this.isActive=!0;if(typeof c!="string")throw"Dexter should receive method name as a String";if(!b||typeof b[c]!="function")throw'Dexter should receive a valid object and method combination in arguments. Ex.: window & "alert".';typeof f=="function"&&(this.callback=f),e.call(this,b,c),b[c]=function(){var b=[].slice.apply(arguments);return g.called=g.called+1,d[a].call(this,g,b)}}var b,c,d;b=function(){this._seenObj[this._seenMethod]=this._oldCall,this.isActive=!1},d={spy:function(a,b){var c=a._oldCall.apply(this,b);return typeof a.callback=="function"&&a.callback.apply(this,b),c},stub:function(a,b){if(typeof a.callback=="function")return a.callback.apply(this,b)}},f.prototype={restore:b},a.Dexter={spy:function(a,b,c){return new f("spy",a,b,c)},stub:function(a,b,c){return new f("stub",a,b,c)}}})(this),function(a,b,c){function i(a,b){if(a!==1||b)throw new Error("INVALID_STATE_ERR")}function j(b){var c,d;return typeof a.DOMParser!="undefined"?(d=new a.DOMParser,c=d.parseFromString(b,"text/xml")):(c=new ActiveXObject("Microsoft.XMLDOM"),c.async="false",c.loadXML(b)),c}var d={},e,f,g,h;d.xhr=function(){var a;try{return a=new XMLHttpRequest,XMLHttpRequest}catch(b){return!1}}(),d.actX=function(){var a;try{return a=new ActiveXObject("Microsoft.XMLHTTP"),ActiveXObject}catch(b){return!1}}(),e={100:"Continue",101:"Switching Protocols",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",300:"Multiple Choice",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Request Entity Too Large",414:"Request-URI Too Long",415:"Unsupported Media Type",416:"Requested Range Not Satisfiable",417:"Expectation Failed",422:"Unprocessable Entity",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported"},f=["Accept-Charset","Accept-Encoding","Connection","Content-Length","Cookie","Cookie2","Content-Transfer-Encoding","Date","Expect","Host","Keep-Alive","Referer","TE","Trailer","Transfer-Encoding","Upgrade","User-Agent","Via"],typeof Array.prototype.indexOf=="undefined"&&(f.indexOf=function(a,b){var c=b||0,d=this.length;for(;c<d;c++)if(this[c]===a)return c;return-1}),g={UNSENT:0,OPENED:1,HEADERS_RECEIVED:2,LOADING:3,DONE:4,onabort:null,onerror:null,onload:null,onloadend:null,onloadstart:null,onprogress:null,onreadystatechange:null,ontimeout:null,readyState:0,response:"",responseText:"",responseType:"",responseXML:null,withCredentials:!1,status:0,statusText:"",timeout:0,abort:function(){this.aborted=!0,this.errorFlag=!0,this.method=null,this.url=null,this.async=c,this.username=null,this.password=null,this.responseText=null,this.responseXML=null,this.requestHeaders={},this.sendFlag=!1,this.readyState>this.UNSENT&&this.sendFlag?this.__DexterStateChange(this.DONE):this.__DexterStateChange(this.UNSENT)},getResponseHeader:function(a){var b,c=this.responseHeaders;if(this.readyState<this.HEADERS_RECEIVED)return null;if(/^Set-Cookie2?$/i.test(a))return null;a=a.toLowerCase();for(b in c)if(c.hasOwnProperty(b)&&b.toLowerCase()===a)return c[b];return null},open:function(a,b,c,d,e){if(typeof a=="undefined"||typeof b=="undefined")throw new Error("Not enough arguments");this.method=a,this.url=b,this.async=typeof c=="undefined"?!0:!!c,this.username=d,this.password=e,this.responseText=null,this.responseXML=null,this.requestHeaders={},this.sendFlag=!1,this.__DexterStateChange(this.OPENED)},send:function(a){var b;i(this.readyState,this.sendFlag),/^(get|head)$/i.test(this.method)||(this.requestHeaders["Content-Type"]?(b=this.requestHeaders["Content-Type"].split(";"),this.requestHeaders["Content-Type"]=b[0]+";charset=utf-8"):this.requestHeaders["Content-Type"]="text/plain;charset=utf-8",this.requestBody=a),this.errorFlag=!1,this.sendFlag=!0,this.__DexterStateChange(this.OPENED),typeof this.onSend=="function"&&this.onSend(this)},setRequestHeader:function(a,b){i(this.readyState,this.sendFlag);if(f.indexOf(a)>=0||/^(Sec-|Proxy-)/.test(a))throw new Error('Refused to set unsafe header "'+a+'"');this.requestHeaders[a]?this.requestHeaders[a]+=","+b:this.requestHeaders[a]=b},getAllResponseHeaders:function(){var a="",b;if(this.readyState<this.HEADERS_RECEIVED)return"";for(b in this.responseHeaders)this.responseHeaders.hasOwnProperty(b)&&!/^Set-Cookie2?$/i.test(b)&&(a+=b+": "+this.responseHeaders[b]+"\r\n");return a},__DexterSetResponseHeaders:function(a){var b;this.responseHeaders={};for(b in a)a.hasOwnProperty(b)&&(this.responseHeaders[b]=a[b]);this.async&&this.__DexterStateChange(this.HEADERS_RECEIVED)},__DexterXHR:!0,__DexterStateChange:function(a){var b;this.readyState=a;if(typeof this.onreadystatechange=="function"){try{b=document.createEvent("Event"),b.initEvent("readystatechange",!1,!1)}catch(c){b={type:"readystatechange"}}this.onreadystatechange.call(this,[b])}},__DexterSetResponseBody:function(a){var b=this.chunkSize||10,c=0,d;this.responseText="";if(this.async)while(c<=a.length)this.__DexterStateChange(this.LOADING),this.responseText+=a.substring(c,c+=b);else this.responseText=a;d=this.getResponseHeader("Content-Type")||"";if(a&&/(text\/xml)|(application\/xml)|(\+xml)/.test(d))try{this.responseXML=j(a)}catch(e){}},__DexterRespond:function(a){var b=!1,c=a.body||"",d=a.headers||{},f=this.DONE;if(this.readyState===f)throw new Error("Request already done");this.__DexterSetResponseHeaders(d),this.status=a.status||200,this.statusText=e[this.status],this.__DexterSetResponseBody(c),this.async?this.__DexterStateChange(f):this.readyState=f}},h=function(){var b=this,c,e,f;this.requests=[],this.doneRequests=[],c=function(a,c){var e=[].slice.call(a);return b.requests.push(this),this.__DexterRef=Date.now(),c==="ActiveXObject"&&e[0]!=="Microsoft.XMLHTTP"?d(e):this},e=function(){c.call(this,arguments,"XMLHttpRequest")},f=function(){c.call(this,arguments,"ActiveXObject")},e.prototype=g,f.prototype=g,d.xhr&&(a.XMLHttpRequest=e),d.actX&&(a.ActiveXObject=f)},h.prototype={respond:function(a,b){var c;a=a||{},b?c=this.requests.splice(b,1)[0]:c=this.requests.shift(),c.__DexterRespond(a),this.doneRequests.push(c)},spy:function(a){var c=b.spy(g,"send",a);return this.__spy=c,c},restore:function(){this.__spy&&this.__spy.restore(),d.xhr&&(a.XMLHttpRequest=d.xhr),d.actX&&(a.ActiveXObject=d.actX)}},b.fakeXHR=function(){return new h}}(this,Dexter);
View
2  package.json
@@ -2,7 +2,7 @@
"name": "Dexter",
"title": "Dexter JS",
"description": "Dexter is a simple Test Helper, framework independent.",
- "version": "0.1.0",
+ "version": "0.2.1",
"homepage": "https://github.com/leobalter/DexterJS",
"author": {
"name": "Leonardo Balter",
View
22 src/Dexter.fakeXHR.js
@@ -263,14 +263,14 @@
* fake .send
***/
send : function( data ) {
- var reqHeaders;
+ var reqHeaders;
// readyState verification (xhr should be already opened)
verifyState( this.readyState, this.sendFlag );
- if ( !/^(get|head)$/i.test( this.method ) ) {
- if (this.requestHeaders[ 'Content-Type' ]) {
+ if ( !/^(get|head)$/i.test( this.method ) ) {
+ if (this.requestHeaders[ 'Content-Type' ]) {
reqHeaders = this.requestHeaders[ 'Content-Type' ].split( ';' );
- this.requestHeaders[ 'Content-Type' ] = value[ 0 ] + ';charset=utf-8';
+ this.requestHeaders[ 'Content-Type' ] = reqHeaders[ 0 ] + ';charset=utf-8';
} else {
this.requestHeaders[ 'Content-Type' ] = "text/plain;charset=utf-8";
}
@@ -541,19 +541,19 @@
* uses a Dexter.spy on xhr send requests
***/
spy : function( callback ) {
- var spy = Dexter.spy( fakeXHRObj, 'send', callback );
- // this.__spy will be used on .restore();
- this.__spy = spy;
- return spy;
+ var spy = Dexter.spy( fakeXHRObj, 'send', callback );
+ // this.__spy will be used on .restore();
+ this.__spy = spy;
+ return spy;
},
/***
* restore the XHR objects to their original states, defaking them
* this won´t affect already created fake ajax requests.
***/
restore : function() {
- if ( this.__spy ) {
- this.__spy.restore();
- }
+ if ( this.__spy ) {
+ this.__spy.restore();
+ }
if ( ajaxObjs.xhr ) {
globalObj.XMLHttpRequest = ajaxObjs.xhr;
}
View
2  test/unit/spy.js
@@ -102,7 +102,7 @@
Dexter.__bar__ = function() {
foo = 'a';
- }
+ };
Dexter.spy( Dexter, '__bar__', function() {
foo += 'b';
Please sign in to comment.
Something went wrong with that request. Please try again.