-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from crazyfactory/180-cookie-middleware
feat(CredentialsMiddleware): add a middleware which adds cookies to requests
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {IFetchRequest} from "./FetchMiddleware"; | ||
import {IncludeCredentialsMiddleware} from "./IncludeCredentialsMiddleware"; | ||
|
||
describe("IncludeCredentialsMiddleware", () => { | ||
it("should be defined", () => { | ||
expect(IncludeCredentialsMiddleware).toBeDefined(); | ||
}); | ||
describe("process", () => { | ||
it("should be defined", () => { | ||
const middleware = new IncludeCredentialsMiddleware(); | ||
expect(middleware.process).toBeDefined(); | ||
}); | ||
it("should set request.credentials to include", () => { | ||
const middleware = new IncludeCredentialsMiddleware(); | ||
const spy = jest.fn((options: IFetchRequest) => { | ||
expect(options.credentials).toBe("include"); | ||
}); | ||
middleware.process({}, spy); | ||
}); | ||
it("should be able to set value of credentials via constructor", () => { | ||
const middleware = new IncludeCredentialsMiddleware("omit"); | ||
const spy = jest.fn((options: IFetchRequest) => { | ||
expect(options.credentials).toBe("omit"); | ||
}); | ||
middleware.process({}, spy); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {IMiddleware} from "../Stack"; | ||
import {IFetchRequest, IFetchResponse, TCredentials} from "./FetchMiddleware"; | ||
|
||
export class IncludeCredentialsMiddleware implements IMiddleware<IFetchRequest, Promise<IFetchResponse<any>>> { | ||
constructor(private mode: TCredentials = "include") {} | ||
|
||
public process(options: IFetchRequest, next: (nextOptions: IFetchRequest) => Promise<IFetchResponse<any>>) | ||
: Promise<IFetchResponse<any>> { | ||
options.credentials = this.mode; | ||
return next(options); | ||
} | ||
} |