Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Pipeline:Rest - Add timeout setting for pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
kborchers committed Feb 19, 2013
1 parent e0831cb commit 7ca6df8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/pipeline/adapters/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@param {String} [settings.pageConfig.nextIdentifier="next"] - the name of the next link header, content var or web link rel
@param {Function} [settings.pageConfig.parameterProvider] - a function for handling custom parameter placement within header and body based paging - for header paging, the function receives a jqXHR object and for body paging, the function receives the JSON formatted body as an object. the function should then return an object containing keys named for the previous/nextIdentifier options and whos values are either a map of parameters and values or a properly formatted query string
@param {String} [settings.recordId="id"] - the name of the field used to uniquely identify a "record" in the data
@param {Number} [settings.timeout=60] - the amount of time, in seconds, to wait before timing out a connection and firing the complete callback for that request
@returns {Object} The created pipe
*/
AeroGear.Pipeline.adapters.Rest = function( pipeName, settings ) {
Expand All @@ -50,7 +51,8 @@
recordId = settings.recordId || "id",
authenticator = settings.authenticator || null,
type = "Rest",
pageConfig = settings.pageConfig;
pageConfig = settings.pageConfig,
timeout = settings.timeout ? settings.timeout * 1000 : 60000;

// Privileged Methods
/**
Expand Down Expand Up @@ -95,6 +97,16 @@
return recordId;
};

/**
Returns the value of the private timeout var
@private
@augments Rest
@returns {Number}
*/
this.getTimeout = function() {
return timeout;
};

/**
Returns the value of the private pageConfig var
@private
Expand Down Expand Up @@ -311,7 +323,8 @@
url: url,
statusCode: options.statusCode,
complete: options.complete,
headers: options.headers
headers: options.headers,
timeout: this.getTimeout()
};

if( options.jsonp ) {
Expand Down Expand Up @@ -403,7 +416,8 @@
error: error,
statusCode: options.statusCode,
complete: options.complete,
headers: options.headers
headers: options.headers,
timeout: this.getTimeout()
});

// Stringify data if we actually want to POST/PUT JSON data
Expand Down Expand Up @@ -495,7 +509,8 @@
error: error,
statusCode: options.statusCode,
complete: options.complete,
headers: options.headers
headers: options.headers,
timeout: this.getTimeout()
};

return $.ajax( this.addAuthIdentifier( $.extend( {}, ajaxSettings, extraOptions ) ) );
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/pipeline/pipeline-mockjax-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,14 @@ $.mockjax({
}
});

$.mockjax({
url: "long",
type: "GET",
headers: {
"Content-Type": "application/json"
},
responseText: "returned",
responseTime: 6000
});

})( jQuery );
22 changes: 22 additions & 0 deletions tests/unit/pipeline/pipeline-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,4 +822,26 @@ asyncTest( "body - custom identifiers and query parameters with parameter provid
}
});
});

module( "Pipeline: Rest - Misc" );

var longPipe = AeroGear.Pipeline({
name: "long",
type: "Rest",
settings: {
timeout: 5
}
}).pipes.long;

asyncTest( "timeout", function() {
expect( 1 );

longPipe.read({
complete: function( jqXHR, textStatus ) {
equal( textStatus, "timeout", "Timeout Triggered Complete Callback" );
start();
}
});
});

})( jQuery );

0 comments on commit 7ca6df8

Please sign in to comment.