-
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 #207 from cyberhck/patch-1
test(ContentTypeMiddleware): add content-type middleware
- Loading branch information
Showing
3 changed files
with
37 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {ContentTypeMiddleware} from "./ContentTypeMiddleware"; | ||
import {IFetchRequest} from "./FetchMiddleware"; | ||
|
||
describe("ContentTypeMiddleware", () => { | ||
it("should add a content type application/json by default", () => { | ||
const spy = jest.fn((options: IFetchRequest) => { | ||
const contentType = options.headers && options.headers["content-type"]; | ||
expect(contentType).toBe("application/json"); | ||
}); | ||
const mw = new ContentTypeMiddleware(); | ||
mw.process({}, spy); | ||
}); | ||
it("should accept a content type in constructor if we want to change it", () => { | ||
const spy = jest.fn((options: IFetchRequest) => { | ||
const contentType = options.headers && options.headers["content-type"]; | ||
expect(contentType).toBe("text/plain"); | ||
}); | ||
const mw = new ContentTypeMiddleware("text/plain"); | ||
mw.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,15 @@ | ||
import {IMiddleware} from "../Stack"; | ||
import {IFetchRequest, IFetchResponse} from "./FetchMiddleware"; | ||
|
||
export class ContentTypeMiddleware implements IMiddleware<IFetchRequest, Promise<IFetchResponse<any>>> { | ||
constructor(private contentType: string = "application/json") { | ||
} | ||
|
||
public process(options: IFetchRequest, next: (nextOptions: IFetchRequest) => Promise<IFetchResponse<any>>): Promise<IFetchResponse<any>> { | ||
if (!options.headers) { | ||
options.headers = {}; | ||
} | ||
options.headers["content-type"] = this.contentType; | ||
return next(options); | ||
} | ||
} |
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