@centeredgesoft/runtime-mock-routes 2.9.1-alpha.3 → 2.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/appV2.d.ts +1 -0
- package/build/src/appV2.js +4 -1
- package/package.json +1 -1
- package/src/appV2.test.ts +30 -0
- package/src/appV2.ts +7 -2
package/build/src/appV2.d.ts
CHANGED
package/build/src/appV2.js
CHANGED
|
@@ -99,7 +99,7 @@ const appFactory = (runtimeCollection) => {
|
|
|
99
99
|
type: ['application/json', 'application/*+json', 'text/json']
|
|
100
100
|
}));
|
|
101
101
|
// log the request
|
|
102
|
-
if (process.env.
|
|
102
|
+
if (process.env.LOG_MOCK_REQUESTS === 'true') {
|
|
103
103
|
app.use((req, res, next) => {
|
|
104
104
|
console.log(`Request: ${req.method} ${req.url}`);
|
|
105
105
|
console.log('Headers:', req.headers);
|
|
@@ -191,6 +191,9 @@ const appFactory = (runtimeCollection) => {
|
|
|
191
191
|
const template = handlebars_1.default.compile(body);
|
|
192
192
|
const result = template(tokenParams);
|
|
193
193
|
try {
|
|
194
|
+
if (method.raw) {
|
|
195
|
+
return res.send(result);
|
|
196
|
+
}
|
|
194
197
|
const jsonResult = JSON.parse(result);
|
|
195
198
|
return res.send(jsonResult);
|
|
196
199
|
}
|
package/package.json
CHANGED
package/src/appV2.test.ts
CHANGED
|
@@ -275,6 +275,36 @@ describe('Body as Function', () => {
|
|
|
275
275
|
})
|
|
276
276
|
});
|
|
277
277
|
|
|
278
|
+
describe('Body returns quoted JSON when sending raw', () => {
|
|
279
|
+
test('Return JSON stringified string with quotes', async () => {
|
|
280
|
+
const seed: RuntimeRequestCollection = {
|
|
281
|
+
"/test": {
|
|
282
|
+
path: "/test",
|
|
283
|
+
methods: {
|
|
284
|
+
GET: {
|
|
285
|
+
body: function(rp: RequestParameters) { return JSON.stringify(`"<p>Test</p>"`) },
|
|
286
|
+
status: 200,
|
|
287
|
+
headers: () => { return { 'Content-Type': 'application/json; charset=utf-8' } },
|
|
288
|
+
raw: true
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const app = appFactory(seed);
|
|
295
|
+
expect(app).toBeDefined();
|
|
296
|
+
|
|
297
|
+
var response = await request(app).get('/test');
|
|
298
|
+
|
|
299
|
+
expect(response.body).toEqual(`"<p>Test</p>"`);
|
|
300
|
+
expect(response.body).toHaveLength(13)
|
|
301
|
+
expect(response.body[0]).toBe(`"`);
|
|
302
|
+
expect(response.body[1]).toBe(`<`);
|
|
303
|
+
expect(response.body[11]).toBe(`>`);
|
|
304
|
+
expect(response.body[12]).toBe(`"`);
|
|
305
|
+
})
|
|
306
|
+
});
|
|
307
|
+
|
|
278
308
|
describe('Request Body is Simple Type', () => {
|
|
279
309
|
test('Send in string as JSON', async () => {
|
|
280
310
|
const seed: RuntimeRequestCollection = {
|
package/src/appV2.ts
CHANGED
|
@@ -37,6 +37,7 @@ export interface RuntimeRequestMethodBody {
|
|
|
37
37
|
body: any;
|
|
38
38
|
status?: any;
|
|
39
39
|
headers?: any;
|
|
40
|
+
raw?: boolean;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
export interface RequestParameters {
|
|
@@ -115,7 +116,7 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
|
|
|
115
116
|
}));
|
|
116
117
|
|
|
117
118
|
// log the request
|
|
118
|
-
if (process.env.
|
|
119
|
+
if (process.env.LOG_MOCK_REQUESTS === 'true') {
|
|
119
120
|
app.use((req, res, next) => {
|
|
120
121
|
console.log(`Request: ${req.method} ${req.url}`);
|
|
121
122
|
console.log('Headers:', req.headers);
|
|
@@ -218,7 +219,7 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
|
|
|
218
219
|
const status = typeof method.status === 'function'
|
|
219
220
|
? method.status(tokenParams)
|
|
220
221
|
: method.status
|
|
221
|
-
|
|
222
|
+
|
|
222
223
|
res.status(status);
|
|
223
224
|
}
|
|
224
225
|
|
|
@@ -252,6 +253,10 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
|
|
|
252
253
|
const result = template(tokenParams)
|
|
253
254
|
|
|
254
255
|
try {
|
|
256
|
+
if (method.raw) {
|
|
257
|
+
return res.send(result);
|
|
258
|
+
}
|
|
259
|
+
|
|
255
260
|
const jsonResult = JSON.parse(result);
|
|
256
261
|
return res.send(jsonResult);
|
|
257
262
|
} catch (_) {
|