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

Pipeline:Rest - Add timeout setting for pipes #25

Merged
merged 2 commits into from Feb 19, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/pipeline/adapters/rest.js
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
Expand Up @@ -611,4 +611,14 @@ $.mockjax({
}
});

$.mockjax({
url: "long",
type: "GET",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be same for POST etc ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they all behave the same so didn't bother with those tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hrm - ok..

headers: {
"Content-Type": "application/json"
},
responseText: "returned",
responseTime: 6000
});

})( jQuery );
22 changes: 22 additions & 0 deletions tests/unit/pipeline/pipeline-rest.js
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 );