forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.ts
More file actions
119 lines (104 loc) · 4.61 KB
/
Copy pathClient.ts
File metadata and controls
119 lines (104 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { assert } from "chai";
import "isomorphic-fetch";
import { CustomAuthenticationProvider, TelemetryHandler } from "../../src";
import { Client } from "../../src/Client";
import { AuthProvider } from "../../src/IAuthProvider";
import { ClientOptions } from "../../src/IClientOptions";
import { Options } from "../../src/IOptions";
import { AuthenticationHandler } from "../../src/middleware/AuthenticationHandler";
import { ChaosHandler } from "../../src/middleware/ChaosHandler";
import { ChaosHandlerOptions } from "../../src/middleware/options/ChaosHandlerOptions";
import { ChaosStrategy } from "../../src/middleware/options/ChaosStrategy";
import { DummyAuthenticationProvider } from "../DummyAuthenticationProvider";
import { DummyHTTPMessageHandler } from "../DummyHTTPMessageHandler";
describe("Client.ts", () => {
/* tslint:disable: no-string-literal */
describe("initWithMiddleware", () => {
const dummyAuthProvider = new DummyAuthenticationProvider();
const dummyHTTPHandler = new DummyHTTPMessageHandler();
it("Should throw an error in case if both auth provider and custom middleware is passed", () => {
try {
const options: ClientOptions = {
authProvider: dummyAuthProvider,
middleware: dummyHTTPHandler,
};
const client: Client = Client.initWithMiddleware(options);
throw new Error("Something wrong with the ambiguity check");
} catch (error) {
assert.equal(error.name, "AmbiguityInInitialization");
}
});
it("Should return client instance for an authentication provider", () => {
const options: ClientOptions = {
authProvider: dummyAuthProvider,
};
const client: Client = Client.initWithMiddleware(options);
assert.isTrue(client instanceof Client);
assert.isDefined(client["httpClient"]);
});
it("Should return client instance for a custom middleware chain", () => {
const options: ClientOptions = {
middleware: dummyHTTPHandler,
};
const client: Client = Client.initWithMiddleware(options);
assert.isTrue(client instanceof Client);
assert.isDefined(client["httpClient"]);
});
it("Should throw error in case of neither auth provider nor custom middleware is passed", () => {
try {
const options: ClientOptions = {};
Client.initWithMiddleware(options);
throw new Error("Something wrong with the client initialization check");
} catch (error) {
assert.equal(error.name, "InvalidMiddlewareChain");
}
});
it("Init middleware using a middleware array", async () => {
const provider: AuthProvider = (done) => {
done(null, "dummy_token");
};
const authHandler = new AuthenticationHandler(new CustomAuthenticationProvider(provider));
const responseBody = "Test response body";
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing middleware array", 200, 0, responseBody);
const middlewareArray = [authHandler, new ChaosHandler(options)];
const client = Client.initWithMiddleware({ middleware: middlewareArray });
const response = await client.api("me").get();
assert.equal(response, responseBody);
});
it("Init middleware using a chained middleware array", async () => {
const provider: AuthProvider = (done) => {
done(null, "dummy_token");
};
const authHandler = new AuthenticationHandler(new CustomAuthenticationProvider(provider));
const responseBody = "Test response body";
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing chained middleware array", 200, 0, responseBody);
const chaosHandler = new ChaosHandler(options);
const telemetryHandler = new TelemetryHandler();
authHandler.setNext(telemetryHandler);
telemetryHandler.setNext(chaosHandler);
const middlewareArray = [authHandler];
const client = Client.initWithMiddleware({ middleware: middlewareArray });
const response = await client.api("me").get();
assert.equal(response, responseBody);
});
});
describe("init", () => {
it("Should return a client instance with default authentication provider and default middleware chain", () => {
const provider: AuthProvider = (done) => {
done(null, "dummy_token");
};
const options: Options = {
authProvider: provider,
};
const client: Client = Client.init(options);
assert.isDefined(client["httpClient"]);
});
});
/* tslint:enable: no-string-literal */
});