mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-09-17 16:51:57 +08:00
Add tests for utils
This commit is contained in:
parent
fb5a68e830
commit
1ab683b1e7
16
package.json
16
package.json
@ -7,9 +7,11 @@
|
||||
"author": "sukantgujar <sukantgujar@yahoo.com>",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"clean": "rimraf ./dist",
|
||||
"test": "tsc && mocha dist/**/*.spec.js",
|
||||
"build:watch": "npx tsc -w",
|
||||
"build:prod": "rimraf ./dist && cross-env NODE_ENV=production tsc -p ./tsconfig.production.json",
|
||||
"push": "yarn build:prod && yarn publish",
|
||||
"build:prod": "yarn clean && cross-env NODE_ENV=production tsc -p ./tsconfig.production.json",
|
||||
"push": "yarn test && yarn build:prod && yarn publish",
|
||||
"run:examples:file": "node ./dist/examples/express-file-server/index.js"
|
||||
},
|
||||
"keywords": [
|
||||
@ -18,11 +20,17 @@
|
||||
"stream"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.1.7",
|
||||
"@types/express": "^4.16.1",
|
||||
"@types/mocha": "^5.2.6",
|
||||
"@types/sinon": "^7.0.9",
|
||||
"chai": "^4.2.0",
|
||||
"cross-env": "^5.2.0",
|
||||
"express": "^4.16.4",
|
||||
"mocha": "^6.0.2",
|
||||
"rimraf": "^2.6.3",
|
||||
"typescript": "^3.3.3333",
|
||||
"express": "^4.16.4"
|
||||
"sinon": "^7.2.7",
|
||||
"typescript": "^3.3.3333"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"express": "^4.16.4"
|
||||
|
100
src/utils.spec.ts
Normal file
100
src/utils.spec.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import { Request, Response } from "express";
|
||||
import { expect } from "chai";
|
||||
import sinon, { SinonStub, SinonSpy } from "sinon";
|
||||
|
||||
import {
|
||||
getHeader,
|
||||
setHeader,
|
||||
getRangeHeader,
|
||||
setContentTypeHeader,
|
||||
setContentLengthHeader,
|
||||
setAcceptRangesHeader,
|
||||
setContentDispositionHeader,
|
||||
setContentRangeHeader,
|
||||
setCacheControlHeaderNoCache
|
||||
} from "./utils";
|
||||
|
||||
describe("utils tests", () => {
|
||||
let req: Request;
|
||||
let res: Response;
|
||||
beforeEach(() => {
|
||||
req = {
|
||||
headers: {
|
||||
"content-type": "application/octet-stream",
|
||||
range: "*"
|
||||
}
|
||||
} as Request;
|
||||
res = {
|
||||
setHeader: sinon.stub() as (name: string, value: string) => void
|
||||
} as Response;
|
||||
});
|
||||
describe("getHeader tests", () => {
|
||||
it("gets the specified header value if present", () => {
|
||||
const value = getHeader("content-type", req);
|
||||
expect(value).to.equal("application/octet-stream");
|
||||
});
|
||||
it("returns undefined if the specified header value is absent", () => {
|
||||
const value = getHeader("mime-type", req);
|
||||
expect(value).to.be.undefined;
|
||||
});
|
||||
});
|
||||
describe("setHeader tests", () => {
|
||||
it("invokes res.setHeader API with the specified name and value args", () => {
|
||||
const name = "Content-Type";
|
||||
const value = "application/octet-stream";
|
||||
setHeader(name, value, res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith(name, value));
|
||||
});
|
||||
});
|
||||
describe("getRangeHeader tests", () => {
|
||||
it("gets range header value", () => {
|
||||
const value = getRangeHeader(req);
|
||||
expect(value).to.equal("*");
|
||||
});
|
||||
});
|
||||
describe("setContentTypeHeader tests", () => {
|
||||
it("sets Content-Type header with specified value", () => {
|
||||
const value = "application/octet-stream";
|
||||
setContentTypeHeader(value, res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith("Content-Type", value));
|
||||
});
|
||||
});
|
||||
describe("setContentLengthHeader tests", () => {
|
||||
it("sets Content-Length header with specified value", () => {
|
||||
const value = 100;
|
||||
setContentLengthHeader(value, res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith("Content-Length", value));
|
||||
});
|
||||
});
|
||||
describe("setAcceptRangesHeader tests", () => {
|
||||
it("sets Accept-Ranges header with specified value", () => {
|
||||
const value = "bytes";
|
||||
setAcceptRangesHeader(res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith("Accept-Ranges", value));
|
||||
});
|
||||
});
|
||||
describe("setContentRangeHeader tests", () => {
|
||||
it("sets Content-Range header with specified value", () => {
|
||||
const range = { start: 10, end: 100 };
|
||||
const size = 1000;
|
||||
const value = `bytes ${range ? `${range.start}-${range.end}` : "*"}/${size}`;
|
||||
setContentRangeHeader(range, size, res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith("Content-Range", value));
|
||||
});
|
||||
});
|
||||
describe("setContentDispositionHeader tests", () => {
|
||||
it("sets Content-Disposition header with specified value", () => {
|
||||
const fileName = "file.txt";
|
||||
const value = `attachment; filename="${fileName}"`;
|
||||
setContentDispositionHeader(fileName, res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith("Content-Disposition", value));
|
||||
});
|
||||
});
|
||||
describe("setCacheControlHeaderNoCache tests", () => {
|
||||
it("sets Cache-Control header with specified value", () => {
|
||||
const value = "no-cache";
|
||||
setCacheControlHeaderNoCache(res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith("Cache-Control", value));
|
||||
});
|
||||
});
|
||||
});
|
@ -1,8 +1,8 @@
|
||||
import { Request, Response } from "express";
|
||||
import { Range } from "./Range";
|
||||
const getHeader = (name: string, req: Request) => req.headers[name];
|
||||
export const getHeader = (name: string, req: Request) => req.headers[name];
|
||||
export const getRangeHeader = getHeader.bind(null, "range");
|
||||
const setHeader = (name: string, value: string, res: Response) => res.setHeader(name, value);
|
||||
export const setHeader = (name: string, value: string, res: Response) => res.setHeader(name, value);
|
||||
export const setContentTypeHeader = setHeader.bind(null, "Content-Type");
|
||||
export const setContentLengthHeader = setHeader.bind(null, "Content-Length");
|
||||
export const setAcceptRangesHeader = setHeader.bind(null, "Accept-Ranges", "bytes");
|
||||
|
Loading…
x
Reference in New Issue
Block a user