/
fileupload.pacttest.ts
104 lines (98 loc) · 4.09 KB
/
fileupload.pacttest.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
import { InteractionObject, Matchers } from "@pact-foundation/pact";
const { like, term } = Matchers
import { readFileSync } from "fs";
import * as jestpact from "jest-pact";
import * as supertest from "supertest";
jestpact.pactWith(
{ consumer: "test-consumer", provider: "file-upload-provider" },
async (provider: any) => {
const client = () => {
const url = `${provider.mockService.baseUrl}`;
return supertest(url);
};
describe("file upload service", () => {
test("should successfully allow upload of a base 64 encoded pdf", async () => {
const pdfname: string = "test-base64.pdf";
const pdf = readFileSync(pdfname);
const body = `----------------------------713166514119664968500586\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\ntest\r\n----------------------------713166514119664968500586\r\nContent-Disposition: form-data; name=\"document\"; filename=\"${pdfname}\"\r\nContent-Type: application/pdf\r\n\r\n${pdf}\r\n----------------------------713166514119664968500586--\r\n`;
const interaction: InteractionObject = {
state: "Service is up and healthy",
uponReceiving:
"a well formed request with a base 64 encoded pdf to upload",
withRequest: {
method: "POST",
path: "/upload",
headers: {
"Content-Type": term({
generate:
"multipart/form-data; boundary=--------------------------560782525175769486914756",
matcher:
"multipart/form-data; boundary=--------------------------[0-9]{24}"
}),
Authorization: term({
generate: "Bearer eyJhbGciOiJIUzI1NiIXVCJ9",
matcher: "Bearer [0-9A-z]{24}"
}),
"Content-Length": like("299")
},
body
},
willRespondWith: {
headers: {
"Content-Type": "application/json"
},
status: 201
}
};
await provider.addInteraction(interaction);
await client()
.post("/upload")
.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIXVCJ9")
.field("test", "test")
.attach("document", pdfname)
.expect(201);
});
test("should successfully allow upload of binary encoded pdf", async () => {
const pdfname: string = "test.pdf";
const pdf = readFileSync(pdfname);
const body = `----------------------------713166514119664968500586\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\ntest\r\n----------------------------713166514119664968500586\r\nContent-Disposition: form-data; name=\"document\"; filename=\"${pdfname}\"\r\nContent-Type: application/pdf\r\n\r\n${pdf}\r\n----------------------------713166514119664968500586--\r\n`;
const interaction: InteractionObject = {
state: "Service is up and healthy",
uponReceiving:
"a well formed request with a binary encoded pdf to upload",
withRequest: {
method: "POST",
path: "/upload",
headers: {
"Content-Type": term({
generate:
"multipart/form-data; boundary=--------------------------560782525175769486914756",
matcher:
"multipart/form-data; boundary=--------------------------[0-9]{24}"
}),
Authorization: term({
generate: "Bearer eyJhbGciOiJIUzI1NiIXVCJ9",
matcher: "Bearer [0-9A-z]{24}"
}),
"Content-Length": like("299")
},
body
},
willRespondWith: {
headers: {
"Content-Type": "application/json"
},
status: 201
}
};
await provider.addInteraction(interaction);
await client()
.post("/upload")
.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIXVCJ9")
.field("test", "test")
.attach("document", pdfname)
.expect(201);
});
});
}
);