-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
sendBuffer.ts
95 lines (85 loc) · 2.68 KB
/
sendBuffer.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
import * as request from "request-promise-native";
import * as stream from "stream";
import { Process, config } from "./../../src/index";
const actionhero = new Process();
let api;
let url;
describe("Server: sendBuffer", () => {
beforeAll(async () => {
api = await actionhero.start();
url = "http://localhost:" + config.servers.web.port;
});
afterAll(async () => {
await actionhero.stop();
});
beforeAll(() => {
api.actions.versions.sendBufferTest = [1];
api.actions.actions.sendBufferTest = {
1: {
name: "sendBufferTest",
description: "sendBufferTest",
version: 1,
run: async data => {
const buffer = "Example of data buffer";
const bufferStream = new stream.PassThrough();
data.connection.rawConnection.responseHeaders.push([
"Content-Disposition",
"attachment; filename=test.csv"
]);
api.servers.servers.web.sendFile(
data.connection,
null,
bufferStream,
"text/csv",
buffer.length,
new Date()
);
data.toRender = false;
bufferStream.end(buffer);
}
}
};
api.actions.versions.sendUnknownLengthBufferTest = [1];
api.actions.actions.sendUnknownLengthBufferTest = {
1: {
name: "sendUnknownLengthBufferTest",
description: "sendUnknownLengthBufferTest",
version: 1,
run: data => {
const bufferStream = new stream.PassThrough();
api.servers.servers.web.sendFile(
data.connection,
null,
bufferStream,
"text/plain",
null,
new Date()
);
const buffer = "Example of unknown length data buffer";
data.toRender = false;
bufferStream.end(buffer);
}
}
};
api.routes.loadRoutes();
});
afterAll(() => {
delete api.actions.actions.sendBufferTest;
delete api.actions.versions.sendBufferTest;
delete api.actions.versions.sendUnknownLengthBufferTest;
delete api.actions.versions.sendUnknownLengthBufferTest;
api.routes.loadRoutes();
});
test("Server should sendBuffer", async () => {
const body = await request.get(url + "/api/sendBufferTest");
expect(body).toEqual("Example of data buffer");
});
test("Server should send a stream with no specified length", async () => {
const { body, headers } = await request.get({
uri: url + "/api/sendUnknownLengthBufferTest",
resolveWithFullResponse: true
});
expect(headers).not.toHaveProperty("content-length");
expect(body).toEqual("Example of unknown length data buffer");
});
});