|
8 | 8 | import "isomorphic-fetch"; |
9 | 9 |
|
10 | 10 | import { assert } from "chai"; |
| 11 | +import * as sinon from "sinon"; |
11 | 12 |
|
12 | 13 | import { CustomAuthenticationProvider, TelemetryHandler } from "../../../src"; |
13 | 14 | import { Client } from "../../../src/Client"; |
@@ -148,6 +149,63 @@ describe("Client.ts", () => { |
148 | 149 | assert.equal(error.customError, customError); |
149 | 150 | } |
150 | 151 | }); |
| 152 | + |
| 153 | + it("Init middleware with custom hosts", async () => { |
| 154 | + const accessToken = "DUMMY_TOKEN"; |
| 155 | + const provider: AuthProvider = (done) => { |
| 156 | + done(null, "DUMMY_TOKEN"); |
| 157 | + }; |
| 158 | + |
| 159 | + const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing chained middleware array", 200, 100, ""); |
| 160 | + const chaosHandler = new ChaosHandler(options); |
| 161 | + |
| 162 | + const authHandler = new AuthenticationHandler(new CustomAuthenticationProvider(provider)); |
| 163 | + |
| 164 | + const telemetry = new TelemetryHandler(); |
| 165 | + const middleware = [authHandler, telemetry, chaosHandler]; |
| 166 | + |
| 167 | + const customHost = "test_custom"; |
| 168 | + const customHosts = new Set<string>([customHost]); |
| 169 | + const client = Client.initWithMiddleware({ middleware, customHosts }); |
| 170 | + |
| 171 | + const spy = sinon.spy(telemetry, "execute"); |
| 172 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 173 | + const response = await client.api(`https://${customHost}/v1.0/me`).get(); |
| 174 | + const context = spy.getCall(0).args[0]; |
| 175 | + |
| 176 | + assert.equal(context.options.headers["Authorization"], `Bearer ${accessToken}`); |
| 177 | + }); |
| 178 | + |
| 179 | + it("Pass invalid custom hosts", async () => { |
| 180 | + try { |
| 181 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 182 | + const accessToken = "DUMMY_TOKEN"; |
| 183 | + const provider: AuthProvider = (done) => { |
| 184 | + done(null, "DUMMY_TOKEN"); |
| 185 | + }; |
| 186 | + |
| 187 | + const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing chained middleware array", 200, 100, ""); |
| 188 | + const chaosHandler = new ChaosHandler(options); |
| 189 | + |
| 190 | + const authHandler = new AuthenticationHandler(new CustomAuthenticationProvider(provider)); |
| 191 | + |
| 192 | + const telemetry = new TelemetryHandler(); |
| 193 | + const middleware = [authHandler, telemetry, chaosHandler]; |
| 194 | + |
| 195 | + const customHost = "https://test_custom"; |
| 196 | + const customHosts = new Set<string>([customHost]); |
| 197 | + const client = Client.initWithMiddleware({ middleware, customHosts }); |
| 198 | + |
| 199 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 200 | + const response = await client.api(`https://${customHost}/v1.0/me`).get(); |
| 201 | + |
| 202 | + throw new Error("Test fails - Error expected when custom host is not valid"); |
| 203 | + } catch (error) { |
| 204 | + assert.isDefined(error); |
| 205 | + assert.isDefined(error.message); |
| 206 | + assert.equal(error.message, "Please add only hosts or hostnames to the CustomHosts config. If the url is `http://example.com:3000/`, host is `example:3000`"); |
| 207 | + } |
| 208 | + }); |
151 | 209 | }); |
152 | 210 |
|
153 | 211 | describe("init", () => { |
|
0 commit comments