mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-31 19:51:36 +08:00
refactor(test): move outside of src folder
This commit is contained in:
parent
3354d5df7a
commit
3c21050a1f
@ -1,182 +1,182 @@
|
||||
import * as utils from "./utils";
|
||||
import * as ParseRangeHeaderExports from "./parseRangeHeader";
|
||||
import { ContentDoesNotExistError } from "./ContentDoesNotExistError";
|
||||
import { SinonSandbox, createSandbox, SinonStub, SinonSpy } from "sinon";
|
||||
import { createPartialContentHandler } from "./createPartialContentHandler";
|
||||
import { ContentProvider } from "./ContentProvider";
|
||||
import { Logger } from "./Logger";
|
||||
import { expect } from "chai";
|
||||
import { Request, Response } from "express";
|
||||
import { Content } from "./Content";
|
||||
import { Stream } from "stream";
|
||||
import { Range } from "./Range";
|
||||
|
||||
describe("createPartialContentHandler tests", () => {
|
||||
let sandbox: SinonSandbox;
|
||||
let logger: Logger;
|
||||
beforeEach(() => {
|
||||
sandbox = createSandbox();
|
||||
logger = {
|
||||
debug: sandbox.stub() as (message: string, extra?: any) => void
|
||||
};
|
||||
});
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
it("returns a handler", () => {
|
||||
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
expect(typeof handler === "function");
|
||||
});
|
||||
|
||||
describe("handler tests", () => {
|
||||
let req: Request;
|
||||
let res: Response;
|
||||
let statusSpy: SinonSpy;
|
||||
let sendSpy: SinonSpy;
|
||||
let sendStatusSpy: SinonSpy;
|
||||
beforeEach(() => {
|
||||
req = {} as Request;
|
||||
res = {
|
||||
status: (code: number) => res,
|
||||
send: (message: string) => res,
|
||||
sendStatus: (code: number) => res,
|
||||
setHeader: sandbox.stub() as (name: string, value: string) => void
|
||||
} as Response;
|
||||
statusSpy = sandbox.spy(res, "status");
|
||||
sendSpy = sandbox.spy(res, "send");
|
||||
sendStatusSpy = sandbox.spy(res, "sendStatus");
|
||||
});
|
||||
it("invokes contentProvider with the specified request", async () => {
|
||||
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
try {
|
||||
await handler(req, res);
|
||||
} catch {}
|
||||
expect((contentProvider as SinonStub).calledOnceWith(req));
|
||||
});
|
||||
it("returns 404 if contentProvider throws ContentDoesNotExistError error", async () => {
|
||||
const error = new ContentDoesNotExistError("404-File not found!");
|
||||
const contentProvider = sandbox.stub().rejects(error) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(statusSpy.calledOnceWith(404));
|
||||
expect(sendSpy.calledOnceWith(error.message));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns 500 if contentProvider throws any other error", async () => {
|
||||
const error = new Error("Something went wrong!");
|
||||
const contentProvider = sandbox.stub().rejects(error) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(sendStatusSpy.calledOnceWith(500));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns 416 if parseRangeHeader throws RangeParserError error", async () => {
|
||||
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
req.headers = { range: "bytes=30-10" };
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(statusSpy.calledOnceWith(416));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns 500 if parseRangeHeader throws other errors", async () => {
|
||||
const parseRangeHeaderStub = sandbox
|
||||
.stub(ParseRangeHeaderExports, "parseRangeHeader")
|
||||
.throws(new Error("Something went wrong!"));
|
||||
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(sendStatusSpy.calledOnceWith(500));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns correct response if range is not specified", async () => {
|
||||
const result = ({
|
||||
pipe() {
|
||||
return result;
|
||||
}
|
||||
} as any) as Stream;
|
||||
const content: Content = {
|
||||
fileName: "file.txt",
|
||||
totalSize: 10,
|
||||
mimeType: "text/plain",
|
||||
getStream(range?: Range) {
|
||||
return result;
|
||||
}
|
||||
};
|
||||
const pipeSpy = sandbox.spy(result, "pipe");
|
||||
const getStreamSpy = sandbox.spy(content, "getStream");
|
||||
const contentProvider = sandbox.stub().resolves(content) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
const setContentTypeHeaderSpy = sandbox.spy(utils, "setContentTypeHeader");
|
||||
const setContentDispositionHeaderSpy = sandbox.spy(utils, "setContentDispositionHeader");
|
||||
const setAcceptRangesHeaderSpy = sandbox.spy(utils, "setAcceptRangesHeader");
|
||||
const setContentLengthHeaderSpy = sandbox.spy(utils, "setContentLengthHeader");
|
||||
const setContentRangeHeaderSpy = sandbox.spy(utils, "setContentRangeHeader");
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(setContentTypeHeaderSpy.calledOnceWith(content.mimeType, res));
|
||||
expect(setContentDispositionHeaderSpy.calledOnceWith(content.fileName, res));
|
||||
expect(setAcceptRangesHeaderSpy.calledOnceWith(res));
|
||||
expect(setContentLengthHeaderSpy.calledOnceWith(content.totalSize, res));
|
||||
expect(getStreamSpy.calledOnceWith());
|
||||
expect(pipeSpy.calledOnceWith(res));
|
||||
expect(setContentRangeHeaderSpy.notCalled);
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns correct partial response if range is specified", async () => {
|
||||
req.headers = {
|
||||
range: "bytes=0-5"
|
||||
};
|
||||
const result = ({
|
||||
pipe() {
|
||||
return result;
|
||||
}
|
||||
} as any) as Stream;
|
||||
const content: Content = {
|
||||
fileName: "file.txt",
|
||||
totalSize: 10,
|
||||
mimeType: "text/plain",
|
||||
getStream(range?: Range) {
|
||||
return result;
|
||||
}
|
||||
};
|
||||
const range = { start: 0, end: 5 };
|
||||
const pipeSpy = sandbox.spy(result, "pipe");
|
||||
const getStreamSpy = sandbox.spy(content, "getStream");
|
||||
const contentProvider = sandbox.stub().resolves(content) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
const setContentTypeHeaderSpy = sandbox.spy(utils, "setContentTypeHeader");
|
||||
const setContentDispositionHeaderSpy = sandbox.spy(utils, "setContentDispositionHeader");
|
||||
const setAcceptRangesHeaderSpy = sandbox.spy(utils, "setAcceptRangesHeader");
|
||||
const setContentLengthHeaderSpy = sandbox.spy(utils, "setContentLengthHeader");
|
||||
const setContentRangeHeaderSpy = sandbox.spy(utils, "setContentRangeHeader");
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(setContentTypeHeaderSpy.calledOnceWith(content.mimeType, res));
|
||||
expect(setContentDispositionHeaderSpy.calledOnceWith(content.fileName, res));
|
||||
expect(setAcceptRangesHeaderSpy.calledOnceWith(res));
|
||||
expect(setContentRangeHeaderSpy.calledOnceWith(range, content.totalSize, res));
|
||||
expect(setContentLengthHeaderSpy.calledOnceWith(6, res));
|
||||
expect(getStreamSpy.calledOnceWith(range));
|
||||
expect(pipeSpy.calledOnceWith(res));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
import * as utils from "../src/utils";
|
||||
import * as ParseRangeHeaderExports from "../src/parseRangeHeader";
|
||||
import { ContentDoesNotExistError } from "../src/ContentDoesNotExistError";
|
||||
import { SinonSandbox, createSandbox, SinonStub, SinonSpy } from "sinon";
|
||||
import { createPartialContentHandler } from "../src/createPartialContentHandler";
|
||||
import { ContentProvider } from "../src/ContentProvider";
|
||||
import { Logger } from "../src/Logger";
|
||||
import { expect } from "chai";
|
||||
import { Request, Response } from "express";
|
||||
import { Content } from "../src/Content";
|
||||
import { Stream } from "stream";
|
||||
import { Range } from "../src/Range";
|
||||
|
||||
describe("createPartialContentHandler tests", () => {
|
||||
let sandbox: SinonSandbox;
|
||||
let logger: Logger;
|
||||
beforeEach(() => {
|
||||
sandbox = createSandbox();
|
||||
logger = {
|
||||
debug: sandbox.stub() as (message: string, extra?: any) => void
|
||||
};
|
||||
});
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
it("returns a handler", () => {
|
||||
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
expect(typeof handler === "function");
|
||||
});
|
||||
|
||||
describe("handler tests", () => {
|
||||
let req: Request;
|
||||
let res: Response;
|
||||
let statusSpy: SinonSpy;
|
||||
let sendSpy: SinonSpy;
|
||||
let sendStatusSpy: SinonSpy;
|
||||
beforeEach(() => {
|
||||
req = {} as Request;
|
||||
res = {
|
||||
status: (code: number) => res,
|
||||
send: (message: string) => res,
|
||||
sendStatus: (code: number) => res,
|
||||
setHeader: sandbox.stub() as (name: string, value: string) => void
|
||||
} as Response;
|
||||
statusSpy = sandbox.spy(res, "status");
|
||||
sendSpy = sandbox.spy(res, "send");
|
||||
sendStatusSpy = sandbox.spy(res, "sendStatus");
|
||||
});
|
||||
it("invokes contentProvider with the specified request", async () => {
|
||||
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
try {
|
||||
await handler(req, res);
|
||||
} catch {}
|
||||
expect((contentProvider as SinonStub).calledOnceWith(req));
|
||||
});
|
||||
it("returns 404 if contentProvider throws ContentDoesNotExistError error", async () => {
|
||||
const error = new ContentDoesNotExistError("404-File not found!");
|
||||
const contentProvider = sandbox.stub().rejects(error) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(statusSpy.calledOnceWith(404));
|
||||
expect(sendSpy.calledOnceWith(error.message));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns 500 if contentProvider throws any other error", async () => {
|
||||
const error = new Error("Something went wrong!");
|
||||
const contentProvider = sandbox.stub().rejects(error) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(sendStatusSpy.calledOnceWith(500));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns 416 if parseRangeHeader throws RangeParserError error", async () => {
|
||||
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
req.headers = { range: "bytes=30-10" };
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(statusSpy.calledOnceWith(416));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns 500 if parseRangeHeader throws other errors", async () => {
|
||||
const parseRangeHeaderStub = sandbox
|
||||
.stub(ParseRangeHeaderExports, "parseRangeHeader")
|
||||
.throws(new Error("Something went wrong!"));
|
||||
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(sendStatusSpy.calledOnceWith(500));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns correct response if range is not specified", async () => {
|
||||
const result = ({
|
||||
pipe() {
|
||||
return result;
|
||||
}
|
||||
} as any) as Stream;
|
||||
const content: Content = {
|
||||
fileName: "file.txt",
|
||||
totalSize: 10,
|
||||
mimeType: "text/plain",
|
||||
getStream(range?: Range) {
|
||||
return result;
|
||||
}
|
||||
};
|
||||
const pipeSpy = sandbox.spy(result, "pipe");
|
||||
const getStreamSpy = sandbox.spy(content, "getStream");
|
||||
const contentProvider = sandbox.stub().resolves(content) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
const setContentTypeHeaderSpy = sandbox.spy(utils, "setContentTypeHeader");
|
||||
const setContentDispositionHeaderSpy = sandbox.spy(utils, "setContentDispositionHeader");
|
||||
const setAcceptRangesHeaderSpy = sandbox.spy(utils, "setAcceptRangesHeader");
|
||||
const setContentLengthHeaderSpy = sandbox.spy(utils, "setContentLengthHeader");
|
||||
const setContentRangeHeaderSpy = sandbox.spy(utils, "setContentRangeHeader");
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(setContentTypeHeaderSpy.calledOnceWith(content.mimeType, res));
|
||||
expect(setContentDispositionHeaderSpy.calledOnceWith(content.fileName, res));
|
||||
expect(setAcceptRangesHeaderSpy.calledOnceWith(res));
|
||||
expect(setContentLengthHeaderSpy.calledOnceWith(content.totalSize, res));
|
||||
expect(getStreamSpy.calledOnceWith());
|
||||
expect(pipeSpy.calledOnceWith(res));
|
||||
expect(setContentRangeHeaderSpy.notCalled);
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
it("returns correct partial response if range is specified", async () => {
|
||||
req.headers = {
|
||||
range: "bytes=0-5"
|
||||
};
|
||||
const result = ({
|
||||
pipe() {
|
||||
return result;
|
||||
}
|
||||
} as any) as Stream;
|
||||
const content: Content = {
|
||||
fileName: "file.txt",
|
||||
totalSize: 10,
|
||||
mimeType: "text/plain",
|
||||
getStream(range?: Range) {
|
||||
return result;
|
||||
}
|
||||
};
|
||||
const range = { start: 0, end: 5 };
|
||||
const pipeSpy = sandbox.spy(result, "pipe");
|
||||
const getStreamSpy = sandbox.spy(content, "getStream");
|
||||
const contentProvider = sandbox.stub().resolves(content) as ContentProvider;
|
||||
const handler = createPartialContentHandler(contentProvider, logger);
|
||||
const setContentTypeHeaderSpy = sandbox.spy(utils, "setContentTypeHeader");
|
||||
const setContentDispositionHeaderSpy = sandbox.spy(utils, "setContentDispositionHeader");
|
||||
const setAcceptRangesHeaderSpy = sandbox.spy(utils, "setAcceptRangesHeader");
|
||||
const setContentLengthHeaderSpy = sandbox.spy(utils, "setContentLengthHeader");
|
||||
const setContentRangeHeaderSpy = sandbox.spy(utils, "setContentRangeHeader");
|
||||
try {
|
||||
await handler(req, res);
|
||||
expect(setContentTypeHeaderSpy.calledOnceWith(content.mimeType, res));
|
||||
expect(setContentDispositionHeaderSpy.calledOnceWith(content.fileName, res));
|
||||
expect(setAcceptRangesHeaderSpy.calledOnceWith(res));
|
||||
expect(setContentRangeHeaderSpy.calledOnceWith(range, content.totalSize, res));
|
||||
expect(setContentLengthHeaderSpy.calledOnceWith(6, res));
|
||||
expect(getStreamSpy.calledOnceWith(range));
|
||||
expect(pipeSpy.calledOnceWith(res));
|
||||
} catch {
|
||||
expect(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
@ -1,58 +1,58 @@
|
||||
import { parseRangeHeader } from "./parseRangeHeader";
|
||||
import { SinonSandbox, createSandbox } from "sinon";
|
||||
import { Logger } from "./Logger";
|
||||
import { expect } from "chai";
|
||||
import { RangeParserError } from "./RangeParserError";
|
||||
|
||||
describe("parseRangeHeader tests", () => {
|
||||
let sandbox: SinonSandbox;
|
||||
let logger: Logger;
|
||||
beforeEach(() => {
|
||||
sandbox = createSandbox();
|
||||
logger = {
|
||||
debug: sandbox.stub() as (message: string, extra?: any) => void
|
||||
};
|
||||
});
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
it("returns null if range is not specified", () => {
|
||||
let value = parseRangeHeader("", 10, logger);
|
||||
expect(value).to.be.equal(null);
|
||||
value = parseRangeHeader(null, 10, logger);
|
||||
expect(value).to.be.equal(null);
|
||||
});
|
||||
it("returns null if total size is zero", () => {
|
||||
let value = parseRangeHeader("bytes=0-5", 0, logger);
|
||||
expect(value).to.be.equal(null);
|
||||
});
|
||||
it("if end is not provided, sets end to the last byte (totalSize - 1).", () => {
|
||||
let value = parseRangeHeader("bytes=0-", 10, logger);
|
||||
expect(value).to.be.deep.equal({ start: 0, end: 9 });
|
||||
});
|
||||
it('if start is not provided, set it to the offset of last "end" bytes from the end of the file.', () => {
|
||||
let value = parseRangeHeader("bytes=-5", 10, logger);
|
||||
expect(value).to.be.deep.equal({ start: 5, end: 9 });
|
||||
});
|
||||
it("handles invalid ranges", () => {
|
||||
try {
|
||||
parseRangeHeader("bytes=6-5", 10, logger);
|
||||
} catch (error) {
|
||||
expect(error).that.be.instanceOf(RangeParserError);
|
||||
}
|
||||
try {
|
||||
parseRangeHeader("bytes=6-7", 10, logger);
|
||||
} catch (error) {
|
||||
expect(error).that.be.instanceOf(RangeParserError);
|
||||
}
|
||||
try {
|
||||
parseRangeHeader("bytes=6-11", 10, logger);
|
||||
} catch (error) {
|
||||
expect(error).that.be.instanceOf(RangeParserError);
|
||||
}
|
||||
});
|
||||
it("returns a valid parsed range.", () => {
|
||||
let value = parseRangeHeader("bytes=0-5", 10, logger);
|
||||
expect(value).to.be.deep.equal({ start: 0, end: 5 });
|
||||
});
|
||||
});
|
||||
import { parseRangeHeader } from "../src/parseRangeHeader";
|
||||
import { SinonSandbox, createSandbox } from "sinon";
|
||||
import { Logger } from "../src/Logger";
|
||||
import { expect } from "chai";
|
||||
import { RangeParserError } from "../src/RangeParserError";
|
||||
|
||||
describe("parseRangeHeader tests", () => {
|
||||
let sandbox: SinonSandbox;
|
||||
let logger: Logger;
|
||||
beforeEach(() => {
|
||||
sandbox = createSandbox();
|
||||
logger = {
|
||||
debug: sandbox.stub() as (message: string, extra?: any) => void
|
||||
};
|
||||
});
|
||||
afterEach(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
it("returns null if range is not specified", () => {
|
||||
let value = parseRangeHeader("", 10, logger);
|
||||
expect(value).to.be.equal(null);
|
||||
value = parseRangeHeader(null, 10, logger);
|
||||
expect(value).to.be.equal(null);
|
||||
});
|
||||
it("returns null if total size is zero", () => {
|
||||
let value = parseRangeHeader("bytes=0-5", 0, logger);
|
||||
expect(value).to.be.equal(null);
|
||||
});
|
||||
it("if end is not provided, sets end to the last byte (totalSize - 1).", () => {
|
||||
let value = parseRangeHeader("bytes=0-", 10, logger);
|
||||
expect(value).to.be.deep.equal({ start: 0, end: 9 });
|
||||
});
|
||||
it('if start is not provided, set it to the offset of last "end" bytes from the end of the file.', () => {
|
||||
let value = parseRangeHeader("bytes=-5", 10, logger);
|
||||
expect(value).to.be.deep.equal({ start: 5, end: 9 });
|
||||
});
|
||||
it("handles invalid ranges", () => {
|
||||
try {
|
||||
parseRangeHeader("bytes=6-5", 10, logger);
|
||||
} catch (error) {
|
||||
expect(error).that.be.instanceOf(RangeParserError);
|
||||
}
|
||||
try {
|
||||
parseRangeHeader("bytes=6-7", 10, logger);
|
||||
} catch (error) {
|
||||
expect(error).that.be.instanceOf(RangeParserError);
|
||||
}
|
||||
try {
|
||||
parseRangeHeader("bytes=6-11", 10, logger);
|
||||
} catch (error) {
|
||||
expect(error).that.be.instanceOf(RangeParserError);
|
||||
}
|
||||
});
|
||||
it("returns a valid parsed range.", () => {
|
||||
let value = parseRangeHeader("bytes=0-5", 10, logger);
|
||||
expect(value).to.be.deep.equal({ start: 0, end: 5 });
|
||||
});
|
||||
});
|
@ -1,104 +1,104 @@
|
||||
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", () => {
|
||||
let range = { start: 10, end: 100 };
|
||||
const size = 1000;
|
||||
let value = `bytes ${range.start}-${range.end}/${size}`;
|
||||
setContentRangeHeader(range, size, res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith("Content-Range", value));
|
||||
range = null;
|
||||
value = `bytes */${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));
|
||||
});
|
||||
});
|
||||
});
|
||||
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 "../src/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", () => {
|
||||
let range = { start: 10, end: 100 };
|
||||
const size = 1000;
|
||||
let value = `bytes ${range.start}-${range.end}/${size}`;
|
||||
setContentRangeHeader(range, size, res);
|
||||
expect((res.setHeader as SinonStub).calledOnceWith("Content-Range", value));
|
||||
range = null;
|
||||
value = `bytes */${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,4 +1,7 @@
|
||||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"include": ["src/**/*.ts"]
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"spec/**/*.ts"
|
||||
]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user