-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.spec.ts
174 lines (149 loc) · 9.38 KB
/
usage.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import express = require('express');
import request = require("supertest");
import { Application, Request } from "express";
import { expresshelper, HTTP_CODES, ResponseHelper } from "../../";
function expectStatus(router: Application, code: number, done: Function, content?: any) {
request(router).get("/").expect((res) => {
expect(res.status).toBe(code);
if (content) {
expect(res.body).toEqual(content);
}
done();
}).end(() => {});
}
describe("Should work as expected", () => {
beforeEach(function (this: { router: Application }) {
this.router = express();
this.router.use(expresshelper({
enableJSONP: false,
shouldSend404onEmpty: true
}));
});
it("should return not found when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
res.locals.expresshelper.notFound();
});
expectStatus(this.router, HTTP_CODES.NotFoundError, done, { "error": "An error has occurred", "details": "Not found" });
});
it("should return not implemented when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
res.locals.expresshelper.notImplemented();
});
expectStatus(this.router, HTTP_CODES.NotImplementedError, done, { "error": "Not implemented" });
});
it("should return unauthenticated error when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
res.locals.expresshelper.unauthenticated("should be logged to use this functionality");
});
expectStatus(this.router, HTTP_CODES.UnauthorizedError, done, { "error": "Unauthenticated", "details": "should be logged to use this functionality" });
});
it("should return forbidden error when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
res.locals.expresshelper.forbidden("should be logged to use this functionality");
});
expectStatus(this.router, HTTP_CODES.ForbiddenError, done, { "error": "Unauthorized", "details": "should be logged to use this functionality" });
});
it("should return unauthorized error when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
res.locals.expresshelper.unauthorized("should be logged to use this functionality");
});
expectStatus(this.router, HTTP_CODES.ForbiddenError, done, { "error": "Unauthorized", "details": "should be logged to use this functionality" });
});
it("should return missing parameter when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
res.locals.expresshelper.missingParameter("id");
});
expectStatus(this.router, HTTP_CODES.BadRequestError, done, { "error": "Missing parameter", "details": "id" });
});
it("should work for promises when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const promise = Promise.resolve([]);
res.locals.expresshelper.promiseWrapper(promise);
});
expectStatus(this.router, HTTP_CODES.Ok, done, []);
});
it("should work for promises when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const promise = Promise.reject("error test");
res.locals.expresshelper.promiseWrapper(promise);
});
expectStatus(this.router, HTTP_CODES.InternalServerError, done, { "error": "An error has occurred", "details": "error test" });
});
it("should work for functions with values when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const fn = (fn: Function) => { fn({id: 1, name: "named"}); }; /* pretend a function */
setTimeout(() => { fn(res.locals.expresshelper.ok()); }, 0);
});
expectStatus(this.router, HTTP_CODES.Ok, done, { "id": 1, "name": "named" });
});
it("should work for functions without values, returning a not found error when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const fn = (fn: Function) => { fn(); }; /* pretend a callback function with no error but with empty value */
setTimeout(() => { fn(res.locals.expresshelper.ok()); }, 0);
});
expectStatus(this.router, HTTP_CODES.NotFoundError, done);
});
it("should work for ok functions with values when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const fn = (fn: Function) => { fn({id: 1, name: "named"}); }; /* pretend a function */
setTimeout(() => { fn(res.locals.expresshelper.okWithDefaultValue({"attr": "value"}, HTTP_CODES.Ok)); }, 0);
});
expectStatus(this.router, HTTP_CODES.Ok, done, { "attr": "value" });
});
it("should work for ok functions with no values when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const fn = (fn: Function) => { fn({id: 1, name: "named"}); }; /* pretend a function */
setTimeout(() => { fn(res.locals.expresshelper.okWithDefaultValue("", HTTP_CODES.CreatedResource)); }, 0);
});
expectStatus(this.router, HTTP_CODES.CreatedResource, done, "");
});
it("should work for callback functions with values when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const cb = (fn: Function) => { fn(null, {id: 1, name: "named"}); }; /* pretend a callback function with (err, value) signature */
setTimeout(() => { cb(res.locals.expresshelper.cb()); }, 0);
});
expectStatus(this.router, HTTP_CODES.Ok, done, { "id": 1, "name": "named" });
});
it("should work for callback functions with values when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const cb = (fn: Function) => { fn({err: "my error"}); }; /* pretend an errored callback function with (err, value) signature */
setTimeout(() => { cb(res.locals.expresshelper.cb()); }, 0);
});
expectStatus(this.router, HTTP_CODES.InternalError, done, { "err": "my error" });
});
it("should work for callback functions with values, returning default value when specified when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const cb = (fn: Function) => { fn(null, "another value"); }; /* pretend an errored callback function with (err, value) signature */
setTimeout(() => { cb(res.locals.expresshelper.cbWithDefaultValue("defaultValue")); }, 0);
});
expectStatus(this.router, HTTP_CODES.Ok, done, "defaultValue");
});
it("should work for callback functions with error values, although a default value was specified when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const cb = (fn: Function) => { fn("error", "another value"); }; /* pretend an errored callback function with (err, value) signature */
setTimeout(() => { cb(res.locals.expresshelper.cbWithDefaultValue("defaultValue")); }, 0);
});
expectStatus(this.router, HTTP_CODES.InternalError, done, "error");
});
it("should work for callback error functions when asking for /", function (this: { router: Application }, done) {
this.router.get("/", (_req: Request, res: ResponseHelper) => {
const cb = () => ({ "err": "my error"}); /* pretend an errored callback function with (err, value) signature */
res.locals.expresshelper.callbackError(cb());
});
expectStatus(this.router, HTTP_CODES.InternalError, done, {"error": "An error has occurred", "details": { "err": "my error"}});
});
it("should work for error functions when asking for /", function (this: { router: Application }, done) {
const err = { "err": "my error"};
this.router.get("/", (_req: Request, res: ResponseHelper) => {
res.locals.expresshelper.error(HTTP_CODES.ImATeapotError, "My error")(err);
});
expectStatus(this.router, HTTP_CODES.ImATeapotError, done, {"error": "My error", "details": err});
});
it("should work for error functions when asking for /", function (this: { router: Application }, done) {
const err = { "err": "my error"};
this.router.get("/", (_req: Request, res: ResponseHelper) => {
res.locals.expresshelper.error("My error")(err);
});
expectStatus(this.router, HTTP_CODES.InternalError, done, {"error": "My error", "details": err});
});
});