Skip to content

Commit 755736d

Browse files
authored
feat: Add upload buffer via multipart (#146)
* feat: Add upload buffer via multipart
1 parent 0a68f93 commit 755736d

File tree

11 files changed

+802
-463
lines changed

11 files changed

+802
-463
lines changed

lib/helpers/__snapshots__/dto.helper.spec.ts.snap

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,265 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`bufferDtoToFormData should return form data 1`] = `
4+
FormData {
5+
"_checkDataSize": [MockFunction],
6+
"_emitError": [MockFunction],
7+
"_error": [MockFunction],
8+
"_generateBoundary": [MockFunction],
9+
"_getContentDisposition": [MockFunction],
10+
"_getContentType": [MockFunction],
11+
"_getNext": [MockFunction],
12+
"_handleErrors": [MockFunction],
13+
"_lastBoundary": [MockFunction],
14+
"_lengthRetriever": [MockFunction],
15+
"_multiPartFooter": [MockFunction],
16+
"_multiPartHeader": [MockFunction],
17+
"_pipeNext": [MockFunction],
18+
"_realGetNext": [MockFunction],
19+
"_reset": [MockFunction],
20+
"_trackLength": [MockFunction],
21+
"_updateDataSize": [MockFunction],
22+
"addListener": [MockFunction],
23+
"append": [MockFunction] {
24+
"calls": Array [
25+
Array [
26+
"buildId",
27+
"buildId",
28+
],
29+
Array [
30+
"projectId",
31+
"projectId",
32+
],
33+
Array [
34+
"branchName",
35+
"develop",
36+
],
37+
Array [
38+
"name",
39+
"name",
40+
],
41+
Array [
42+
"image",
43+
Object {
44+
"data": Array [
45+
1,
46+
2,
47+
3,
48+
],
49+
"type": "Buffer",
50+
},
51+
Object {
52+
"filename": "image.png",
53+
},
54+
],
55+
Array [
56+
"os",
57+
"os",
58+
],
59+
Array [
60+
"browser",
61+
"browser",
62+
],
63+
Array [
64+
"viewport",
65+
"viewport",
66+
],
67+
Array [
68+
"device",
69+
"device",
70+
],
71+
Array [
72+
"ignoreAreas",
73+
"[{\\"x\\":1,\\"y\\":2,\\"height\\":300,\\"width\\":400}]",
74+
],
75+
Array [
76+
"diffTollerancePercent",
77+
0.123,
78+
],
79+
],
80+
"results": Array [
81+
Object {
82+
"type": "return",
83+
"value": undefined,
84+
},
85+
Object {
86+
"type": "return",
87+
"value": undefined,
88+
},
89+
Object {
90+
"type": "return",
91+
"value": undefined,
92+
},
93+
Object {
94+
"type": "return",
95+
"value": undefined,
96+
},
97+
Object {
98+
"type": "return",
99+
"value": undefined,
100+
},
101+
Object {
102+
"type": "return",
103+
"value": undefined,
104+
},
105+
Object {
106+
"type": "return",
107+
"value": undefined,
108+
},
109+
Object {
110+
"type": "return",
111+
"value": undefined,
112+
},
113+
Object {
114+
"type": "return",
115+
"value": undefined,
116+
},
117+
Object {
118+
"type": "return",
119+
"value": undefined,
120+
},
121+
Object {
122+
"type": "return",
123+
"value": undefined,
124+
},
125+
],
126+
},
127+
"destroy": [MockFunction],
128+
"emit": [MockFunction],
129+
"end": [MockFunction],
130+
"eventNames": [MockFunction],
131+
"getBoundary": [MockFunction],
132+
"getBuffer": [MockFunction],
133+
"getHeaders": [MockFunction],
134+
"getLength": [MockFunction],
135+
"getLengthSync": [MockFunction],
136+
"getMaxListeners": [MockFunction],
137+
"hasKnownLength": [MockFunction],
138+
"listenerCount": [MockFunction],
139+
"listeners": [MockFunction],
140+
"once": [MockFunction],
141+
"pause": [MockFunction],
142+
"pipe": [MockFunction],
143+
"prependListener": [MockFunction],
144+
"prependOnceListener": [MockFunction],
145+
"rawListeners": [MockFunction],
146+
"removeAllListeners": [MockFunction],
147+
"removeListener": [MockFunction],
148+
"resume": [MockFunction],
149+
"setBoundary": [MockFunction],
150+
"setMaxListeners": [MockFunction],
151+
"submit": [MockFunction],
152+
"toString": [MockFunction],
153+
"write": [MockFunction],
154+
}
155+
`;
156+
157+
exports[`bufferDtoToFormData should return form data 2`] = `
158+
FormData {
159+
"_checkDataSize": [MockFunction],
160+
"_emitError": [MockFunction],
161+
"_error": [MockFunction],
162+
"_generateBoundary": [MockFunction],
163+
"_getContentDisposition": [MockFunction],
164+
"_getContentType": [MockFunction],
165+
"_getNext": [MockFunction],
166+
"_handleErrors": [MockFunction],
167+
"_lastBoundary": [MockFunction],
168+
"_lengthRetriever": [MockFunction],
169+
"_multiPartFooter": [MockFunction],
170+
"_multiPartHeader": [MockFunction],
171+
"_pipeNext": [MockFunction],
172+
"_realGetNext": [MockFunction],
173+
"_reset": [MockFunction],
174+
"_trackLength": [MockFunction],
175+
"_updateDataSize": [MockFunction],
176+
"addListener": [MockFunction],
177+
"append": [MockFunction] {
178+
"calls": Array [
179+
Array [
180+
"buildId",
181+
"buildId",
182+
],
183+
Array [
184+
"projectId",
185+
"projectId",
186+
],
187+
Array [
188+
"branchName",
189+
"develop",
190+
],
191+
Array [
192+
"name",
193+
"name",
194+
],
195+
Array [
196+
"image",
197+
Object {
198+
"data": Array [
199+
1,
200+
2,
201+
3,
202+
],
203+
"type": "Buffer",
204+
},
205+
Object {
206+
"filename": "image.png",
207+
},
208+
],
209+
],
210+
"results": Array [
211+
Object {
212+
"type": "return",
213+
"value": undefined,
214+
},
215+
Object {
216+
"type": "return",
217+
"value": undefined,
218+
},
219+
Object {
220+
"type": "return",
221+
"value": undefined,
222+
},
223+
Object {
224+
"type": "return",
225+
"value": undefined,
226+
},
227+
Object {
228+
"type": "return",
229+
"value": undefined,
230+
},
231+
],
232+
},
233+
"destroy": [MockFunction],
234+
"emit": [MockFunction],
235+
"end": [MockFunction],
236+
"eventNames": [MockFunction],
237+
"getBoundary": [MockFunction],
238+
"getBuffer": [MockFunction],
239+
"getHeaders": [MockFunction],
240+
"getLength": [MockFunction],
241+
"getLengthSync": [MockFunction],
242+
"getMaxListeners": [MockFunction],
243+
"hasKnownLength": [MockFunction],
244+
"listenerCount": [MockFunction],
245+
"listeners": [MockFunction],
246+
"once": [MockFunction],
247+
"pause": [MockFunction],
248+
"pipe": [MockFunction],
249+
"prependListener": [MockFunction],
250+
"prependOnceListener": [MockFunction],
251+
"rawListeners": [MockFunction],
252+
"removeAllListeners": [MockFunction],
253+
"removeListener": [MockFunction],
254+
"resume": [MockFunction],
255+
"setBoundary": [MockFunction],
256+
"setMaxListeners": [MockFunction],
257+
"submit": [MockFunction],
258+
"toString": [MockFunction],
259+
"write": [MockFunction],
260+
}
261+
`;
262+
3263
exports[`multipartDtoToFormData should return form data 1`] = `
4264
FormData {
5265
"_checkDataSize": [MockFunction],

lib/helpers/dto.helper.spec.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { multipartDtoToFormData } from "./index";
2-
import { TestRunMultipartDto } from "types/request";
1+
import { multipartDtoToFormData, bufferDtoToFormData } from "./index";
2+
import { TestRunBufferDto, TestRunMultipartDto } from "types/request";
33
import { mocked } from "ts-jest/utils";
44
import * as fs from "fs";
55

@@ -52,3 +52,43 @@ describe("multipartDtoToFormData", () => {
5252
expect(result).toMatchSnapshot();
5353
});
5454
});
55+
56+
describe("bufferDtoToFormData", () => {
57+
it.each([
58+
[
59+
{
60+
branchName: "develop",
61+
projectId: "projectId",
62+
buildId: "buildId",
63+
name: "name",
64+
imageBuffer: Buffer.of(1, 2, 3),
65+
os: "os",
66+
device: "device",
67+
viewport: "viewport",
68+
browser: "browser",
69+
diffTollerancePercent: 0.123,
70+
ignoreAreas: [
71+
{
72+
x: 1,
73+
y: 2,
74+
height: 300,
75+
width: 400,
76+
},
77+
],
78+
},
79+
],
80+
[
81+
{
82+
branchName: "develop",
83+
projectId: "projectId",
84+
buildId: "buildId",
85+
name: "name",
86+
imageBuffer: Buffer.of(1, 2, 3),
87+
},
88+
],
89+
])("should return form data", (dto: TestRunBufferDto) => {
90+
const result = bufferDtoToFormData(dto);
91+
92+
expect(result).toMatchSnapshot();
93+
});
94+
});

lib/helpers/dto.helper.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import FormData from "form-data";
22
import fs from "fs";
3-
import { TestRunMultipartDto } from "types/request";
3+
import { TestRunMultipartDto, TestRunBufferDto } from "types/request";
44

55
export const multipartDtoToFormData = (dto: TestRunMultipartDto): FormData => {
66
const data = new FormData();
@@ -22,3 +22,22 @@ export const multipartDtoToFormData = (dto: TestRunMultipartDto): FormData => {
2222

2323
return data;
2424
};
25+
26+
export const bufferDtoToFormData = (dto: TestRunBufferDto): FormData => {
27+
const data = new FormData();
28+
data.append("buildId", dto.buildId);
29+
data.append("projectId", dto.projectId);
30+
data.append("branchName", dto.branchName);
31+
data.append("name", dto.name);
32+
data.append("image", dto.imageBuffer, { filename: "image.png" });
33+
dto.os && data.append("os", dto.os);
34+
dto.browser && data.append("browser", dto.browser);
35+
dto.viewport && data.append("viewport", dto.viewport);
36+
dto.device && data.append("device", dto.device);
37+
dto.ignoreAreas &&
38+
data.append("ignoreAreas", JSON.stringify(dto.ignoreAreas));
39+
dto.diffTollerancePercent &&
40+
data.append("diffTollerancePercent", dto.diffTollerancePercent);
41+
42+
return data;
43+
};

lib/helpers/type.helper.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { TestRunBase64 } from "types";
1+
import { TestRunBase64, TestRunBuffer } from "types";
22

33
export function instanceOfTestRunBase64(object: any): object is TestRunBase64 {
44
return "imageBase64" in object;
55
}
6+
7+
export function instanceOfTestRunBuffer(object: any): object is TestRunBuffer {
8+
return "imageBuffer" in object;
9+
}

lib/types/request/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./testRunBase64.dto";
22
export * from "./testRunMultipart.dto";
3+
export * from "./testRunBuffer.dto";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { TestRunDto } from "./testRun.dto";
2+
3+
export interface TestRunBufferDto extends TestRunDto {
4+
imageBuffer: Buffer;
5+
}

lib/types/testRun.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ export interface TestRunBase64 extends TestRun {
1717
export interface TestRunMultipart extends TestRun {
1818
imagePath: string;
1919
}
20+
21+
export interface TestRunBuffer extends TestRun {
22+
imageBuffer: Buffer;
23+
}

0 commit comments

Comments
 (0)