Merge pull request #174 from RikkiGibson/Timeout

Timeout
This commit is contained in:
Rikki Gibson 2018-07-17 13:52:48 -07:00 коммит произвёл GitHub
Родитель 3e09095403 648d4ccf84
Коммит 1a5875a792
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 42 добавлений и 7 удалений

Просмотреть файл

@ -112,7 +112,8 @@ export class AxiosHttpClient implements HttpClient {
// Workaround for https://github.com/axios/axios/issues/1362
maxContentLength: 1024 * 1024 * 1024 * 10,
responseType: httpRequest.rawResponse ? (isNode ? "stream" : "blob") : "text",
cancelToken
cancelToken,
timeout: httpRequest.timeout
};
res = await axiosClient(config);
} catch (err) {

Просмотреть файл

@ -268,6 +268,10 @@ export class ServiceClient {
httpRequest.abortSignal = options.abortSignal;
}
if (options.timeout) {
httpRequest.timeout = options.timeout;
}
if (options.onUploadProgress) {
httpRequest.onUploadProgress = options.onUploadProgress;
}

Просмотреть файл

@ -53,6 +53,7 @@ export class WebResource {
query?: { [key: string]: any; };
operationSpec?: OperationSpec;
withCredentials: boolean;
timeout: number;
abortSignal?: AbortSignalLike;
@ -68,21 +69,23 @@ export class WebResource {
body?: any,
query?: { [key: string]: any; },
headers?: { [key: string]: any; } | HttpHeaders,
rawResponse = false,
withCredentials = false,
rawResponse?: boolean,
withCredentials?: boolean,
abortSignal?: AbortSignalLike,
timeout?: number,
onUploadProgress?: (progress: TransferProgressEvent) => void,
onDownloadProgress?: (progress: TransferProgressEvent) => void) {
this.rawResponse = rawResponse;
this.rawResponse = rawResponse || false;
this.url = url || "";
this.method = method || "GET";
this.headers = (headers instanceof HttpHeaders ? headers : new HttpHeaders(headers));
this.body = body;
this.query = query;
this.formData = undefined;
this.withCredentials = withCredentials;
this.withCredentials = withCredentials || false;
this.abortSignal = abortSignal;
this.timeout = timeout || 0;
this.onUploadProgress = onUploadProgress;
this.onDownloadProgress = onDownloadProgress;
}
@ -290,6 +293,7 @@ export class WebResource {
this.rawResponse,
this.withCredentials,
this.abortSignal,
this.timeout,
this.onUploadProgress,
this.onDownloadProgress);
result.formData = this.formData;
@ -412,6 +416,11 @@ export interface RequestOptionsBase {
*/
abortSignal?: AbortSignalLike;
/**
* The number of milliseconds a request can take before automatically being terminated.
*/
timeout?: number;
/**
* Callback which fires upon upload progress. Only used in the browser.
*/

Просмотреть файл

@ -60,6 +60,7 @@ export class XhrHttpClient implements HttpClient {
}
}
xhr.timeout = request.timeout;
xhr.withCredentials = request.withCredentials;
xhr.open(request.method, request.url);
for (const header of request.headers.headersArray()) {
@ -127,4 +128,5 @@ function parseHeaders(xhr: XMLHttpRequest) {
function rejectOnTerminalEvent(request: WebResource, xhr: XMLHttpRequest, reject: (err: any) => void) {
xhr.addEventListener("error", ev => reject(new RestError(ev.message, "REQUEST_SEND_ERROR", undefined, request)));
xhr.addEventListener("abort", () => reject(new RestError("The request was aborted", "REQUEST_ABORTED_ERROR", undefined, request)));
xhr.addEventListener("timeout", () => reject(new RestError(`timeout of ${xhr.timeout}ms exceeded`, "REQUEST_SEND_ERROR", undefined, request)));
}

Просмотреть файл

@ -160,7 +160,7 @@ describe("defaultHttpClient", () => {
let downloadNotified = false;
const buf = new Uint8Array(1024 * 1024 * 1);
const request = new WebResource(`${baseURL}/fileupload`, "POST", buf, undefined, undefined, true, undefined, undefined,
const request = new WebResource(`${baseURL}/fileupload`, "POST", buf, undefined, undefined, true, undefined, undefined, 0,
ev => {
uploadNotified = true;
ev.should.not.be.instanceof(ProgressEvent);
@ -180,4 +180,15 @@ describe("defaultHttpClient", () => {
assert(uploadNotified);
assert(downloadNotified);
});
it("should honor request timeouts", async function () {
const request = new WebResource(`${baseURL}/slow`, "GET", undefined, undefined, undefined, false, false, undefined, 100);
const client = new DefaultHttpClient();
try {
await client.sendRequest(request);
throw new Error("request did not fail as expected");
} catch (err) {
err.message.should.match(/timeout/);
}
});
});

Просмотреть файл

@ -27,7 +27,8 @@ describe("Log filter", () => {
"body": {
"a": 1
},
"withCredentials": false
"withCredentials": false,
"timeout": 0
}
>> Response status code: 200
>> Body: null

Просмотреть файл

@ -30,6 +30,13 @@ app.get("/cookie", function(req, res) {
res.end();
});
app.get("/slow", function(req, res) {
setTimeout(() => {
res.status(200);
res.end();
}, 2000);
});
app.listen(port, function() {
console.log(`ms-rest-js testserver listening on port ${port}...`);
});