Add createDefaultHttpPipeline() function and test

This commit is contained in:
Dan Schulte 2018-04-17 12:13:38 -07:00
Родитель 08f56f33cd
Коммит 7470a53093
3 изменённых файлов: 103 добавлений и 2 удалений

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

@ -5,9 +5,19 @@ import { HttpClient } from "./httpClient";
import { HttpPipelineOptions } from "./httpPipelineOptions";
import { HttpRequest } from "./httpRequest";
import { HttpResponse } from "./httpResponse";
import { msRestNodeJsUserAgentPolicy } from "./policies/msRestNodeJsUserAgentPolicy";
import { RequestPolicy } from "./requestPolicy";
import { RequestPolicyFactory } from "./requestPolicyFactory";
import { RequestPolicyOptions } from "./requestPolicyOptions";
import { Constants } from "./util/constants";
import { isNode } from "./util/utils";
import { ServiceClientCredentials } from "./msRest";
import { HttpPipelineLogger } from "./httpPipelineLogger";
import { signingPolicy } from "./policies/signingPolicy";
import { redirectPolicy } from "./policies/redirectPolicy";
import { rpRegistrationPolicy } from "./policies/rpRegistrationPolicy";
import { exponentialRetryPolicy } from "./policies/exponentialRetryPolicy";
import { systemErrorRetryPolicy } from "./policies/systemErrorRetryPolicy";
let defaultHttpClient: HttpClient;
@ -18,6 +28,66 @@ function getDefaultHttpClient(): HttpClient {
return defaultHttpClient;
}
/**
* Options that can be used to configure the default HttpPipeline configuration.
*/
export interface DefaultHttpPipelineOptions {
/**
* Credentials that will be used to authenticate with the target endpoint.
*/
credentials?: ServiceClientCredentials;
/**
* The number of seconds to wait on a resource provider registration request before timing out.
*/
rpRegistrationRetryTimeoutInSeconds?: number;
/**
* Whether or not to add the retry policies to the HttpPipeline.
*/
addRetryPolicies?: boolean;
/**
* The HttpClient to use. If no httpClient is specified, then the default HttpClient will be used.
*/
httpClient?: HttpClient;
/**
* The logger to use when RequestPolicies need to log information.
*/
logger?: HttpPipelineLogger;
}
/**
* Get the default HttpPipeline.
*/
export function createDefaultHttpPipeline(options?: DefaultHttpPipelineOptions): HttpPipeline {
const requestPolicyFactories: RequestPolicyFactory[] = [];
if (options && options.credentials) {
requestPolicyFactories.push(signingPolicy(options.credentials));
}
if (isNode) {
requestPolicyFactories.push(msRestNodeJsUserAgentPolicy([`ms-rest-js/${Constants.msRestVersion}`]));
}
requestPolicyFactories.push(redirectPolicy());
requestPolicyFactories.push(rpRegistrationPolicy(options && options.rpRegistrationRetryTimeoutInSeconds != undefined ? options.rpRegistrationRetryTimeoutInSeconds : undefined));
if (options && options.addRetryPolicies) {
requestPolicyFactories.push(exponentialRetryPolicy());
requestPolicyFactories.push(systemErrorRetryPolicy());
}
const httpPipelineOptions: HttpPipelineOptions = {
httpClient: options && options.httpClient ? options.httpClient : getDefaultHttpClient(),
pipelineLogger: options && options.logger ? options.logger : undefined
};
return new HttpPipeline(requestPolicyFactories, httpPipelineOptions);
}
/**
* A collection of RequestPolicies that will be applied to a HTTP request before it is sent and will
* be applied to a HTTP response when it is received.

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

@ -12,7 +12,7 @@ import { RequestPolicyOptions } from "../requestPolicyOptions";
* Get a RequestPolicyFactory that creates RedirectPolicies.
* @param maximumRedirections The maximum number of redirections to take before failing.
*/
export function redirectPolicy(maximumRedirections: number): RequestPolicyFactory {
export function redirectPolicy(maximumRedirections = 20): RequestPolicyFactory {
return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new RedirectPolicy(maximumRedirections, nextPolicy, options);
};

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

@ -3,13 +3,14 @@
import * as assert from "assert";
import { HttpClient } from "../lib/httpClient";
import { HttpMethod } from "../lib/httpMethod";
import { HttpPipeline } from "../lib/httpPipeline";
import { HttpPipeline, createDefaultHttpPipeline } from "../lib/httpPipeline";
import { HttpRequest } from "../lib/httpRequest";
import { HttpResponse } from "../lib/httpResponse";
import { InMemoryHttpResponse } from "../lib/inMemoryHttpResponse";
import { userAgentPolicy } from "../lib/policies/userAgentPolicy";
import { BaseRequestPolicy } from "../lib/requestPolicy";
import { FakeHttpClient } from "./fakeHttpClient";
import { HttpHeaders } from "../lib/httpHeaders";
describe("HttpPipeline", () => {
it("should send requests when no request policies are assigned", async () => {
@ -76,4 +77,34 @@ describe("HttpPipeline", () => {
const responseBodyAsText: string | undefined = await response.textBody();
assert.strictEqual("hello3", responseBodyAsText);
});
it("should send requests when using the default HTTP pipeline", async () => {
const httpPipeline: HttpPipeline = createDefaultHttpPipeline();
const httpRequest = new HttpRequest({ method: HttpMethod.GET, url: "https://www.httpbin.org" });
const httpResponse: HttpResponse = await httpPipeline.send(httpRequest);
assert(httpResponse);
assert.deepEqual(httpResponse.statusCode, 200);
assert(httpResponse.headers);
assert.strictEqual(httpResponse.headers.get("access-control-allow-credentials"), "true");
assert.strictEqual(httpResponse.headers.get("access-control-allow-origin"), "*");
assert.strictEqual(httpResponse.headers.get("connection"), "close");
assert.strictEqual(httpResponse.headers.get("content-length"), "13129");
assert.strictEqual(httpResponse.headers.get("content-type"), "text/html; charset=utf-8");
assert(httpResponse.headers.get("date"));
assert.strictEqual(httpResponse.headers.get("server"), "meinheld/0.6.1");
assert.strictEqual(httpResponse.headers.get("via"), "1.1 vegur");
assert.strictEqual(httpResponse.headers.get("x-powered-by"), "Flask");
assert(httpResponse.headers.get("x-processed-time"));
const textBody: string = await httpResponse.textBody() as string;
assert(textBody);
assert.notStrictEqual(textBody.indexOf(`<html>`), -1);
assert.notStrictEqual(textBody.indexOf(`httpbin.org`), -1);
assert.deepEqual(httpResponse.request, httpRequest);
});
});