|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import { assert } from "chai"; |
| 9 | +import * as sinon from "sinon"; |
9 | 10 |
|
10 | 11 | import { Context } from "../../../src/IContext"; |
11 | 12 | import { MiddlewareControl } from "../../../src/middleware/MiddlewareControl"; |
@@ -293,6 +294,67 @@ describe("RedirectHandler.ts", () => { |
293 | 294 | assert.equal(cxt.response.status, 200); |
294 | 295 | }); |
295 | 296 |
|
| 297 | + it("Should not drop Cookie header for same authority redirect url", async () => { |
| 298 | + const options = new RedirectHandlerOptions(); |
| 299 | + const cxt: Context = { |
| 300 | + request: "https://graph.microsoft.com/v1.0/me", |
| 301 | + options: { |
| 302 | + method: "POST", |
| 303 | + body: "dummy body", |
| 304 | + headers: { |
| 305 | + [RedirectHandler["COOKIE_HEADER"]]: "session=abc", |
| 306 | + }, |
| 307 | + }, |
| 308 | + }; |
| 309 | + dummyHTTPHandler.setResponses([ |
| 310 | + new Response("", { |
| 311 | + status: 301, |
| 312 | + headers: { |
| 313 | + [RedirectHandler["LOCATION_HEADER"]]: "https://graph.microsoft.com/v2.0/me", |
| 314 | + }, |
| 315 | + }), |
| 316 | + new Response("ok", { status: 200 }), |
| 317 | + ]); |
| 318 | + await handler["executeWithRedirect"](cxt, 0, options); |
| 319 | + assert.isDefined(cxt.options.headers[RedirectHandler["COOKIE_HEADER"]]); |
| 320 | + assert.equal(cxt.response.status, 200); |
| 321 | + }); |
| 322 | + |
| 323 | + it("Should drop both Authorization and Cookie headers for cross origin redirects", async () => { |
| 324 | + const options = new RedirectHandlerOptions(); |
| 325 | + const cxt: Context = { |
| 326 | + request: "https://graph.microsoft.com/v1.0/me", |
| 327 | + options: { |
| 328 | + method: "POST", |
| 329 | + body: "dummy body", |
| 330 | + headers: { |
| 331 | + [RedirectHandler["AUTHORIZATION_HEADER"]]: "******", |
| 332 | + [RedirectHandler["COOKIE_HEADER"]]: "session=abc", |
| 333 | + "X-Custom-Header": "custom-value", |
| 334 | + }, |
| 335 | + }, |
| 336 | + }; |
| 337 | + dummyHTTPHandler.setResponses([ |
| 338 | + new Response("", { |
| 339 | + status: 301, |
| 340 | + headers: { |
| 341 | + [RedirectHandler["LOCATION_HEADER"]]: "https://graphredirection.microsoft.com/v1.0/me", |
| 342 | + }, |
| 343 | + }), |
| 344 | + new Response("ok", { status: 200 }), |
| 345 | + ]); |
| 346 | + const stub = sinon.stub(handler as any, "shouldDropAuthorizationHeader").returns(true); |
| 347 | + try { |
| 348 | + await handler["executeWithRedirect"](cxt, 0, options); |
| 349 | + } finally { |
| 350 | + stub.restore(); |
| 351 | + } |
| 352 | + assert.isUndefined(cxt.options.headers[RedirectHandler["AUTHORIZATION_HEADER"]]); |
| 353 | + assert.isUndefined(cxt.options.headers[RedirectHandler["COOKIE_HEADER"]]); |
| 354 | + assert.equal(cxt.options.headers["X-Custom-Header"], "custom-value"); |
| 355 | + assert.equal(cxt.response.status, 200); |
| 356 | + }); |
| 357 | + |
296 | 358 | it("Should return success response after successful redirect", async () => { |
297 | 359 | const options = new RedirectHandlerOptions(); |
298 | 360 | const cxt: Context = { |
|
0 commit comments