-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpetstore.ts
174 lines (170 loc) · 5.22 KB
/
petstore.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 path from "path";
import MockAPI from "../src";
import https from "https";
import XML from "fast-xml-parser";
let petStore: MockAPI;
beforeAll(async () => {
petStore = await MockAPI.mock(path.join(__dirname, "./api.yaml"));
});
afterAll(() => {
MockAPI.unmock(petStore);
});
afterEach(() => {
petStore.reset();
});
describe("Easily mock APIs", () => {
it("Make it happen with one click with json responses", async () => {
const response: string = await new Promise((res, rej) =>
https
.get("https://petstore.swagger.io/v2/pet/69", resp => {
let data = "";
resp.on("data", chunk => {
data += chunk;
});
resp.on("end", () => {
res(data);
});
})
.on("error", err => {
rej(err);
})
);
expect(petStore.getCallCount()).toBe(1);
if (petStore.getResponses()[0].code === 200) {
expect(JSON.parse(response)).toEqual(petStore.getResponse().response);
} else {
expect(response).toEqual(petStore.getResponse().response);
}
});
it("Make it happen with one click with xml responses", async () => {
const response: string = await new Promise((res, rej) =>
https
.get("https://petstore.swagger.io/v2/store/order/42", resp => {
let data = "";
resp.on("data", chunk => {
data += chunk;
});
resp.on("end", () => {
res(data);
});
})
.on("error", err => {
rej(err);
})
);
expect(petStore.getCallCount()).toBe(1);
if (petStore.getResponses()[0].code === 200) {
expect(XML.parse(response)).toEqual(petStore.getResponse().response);
} else {
expect(response).toEqual(petStore.getResponse({}).response);
}
});
it("Setting the state is easy too", async () => {
petStore.withState(200);
const response: { data: string; code: number } = await new Promise(
(res, rej) =>
https
.get("https://petstore.swagger.io/v2/pet/69", resp => {
let data = "";
resp.on("data", chunk => {
data += chunk;
});
resp.on("end", () => {
res({ data, code: resp.statusCode! });
});
})
.on("error", err => {
rej(err);
})
);
expect(petStore.getCallCount()).toBe(1);
expect(petStore.getResponses()[0].code).toBe(200);
expect(response.code).toBe(200);
});
it("Setting a state a little bit more complex is also possible", async () => {
petStore.withState(req => (req.parameters.petId === "69" ? 200 : 404));
const response69: { data: string; code: number } = await new Promise(
(res, rej) =>
https
.get("https://petstore.swagger.io/v2/pet/69", resp => {
let data = "";
resp.on("data", chunk => {
data += chunk;
});
resp.on("end", () => {
res({ data, code: resp.statusCode! });
});
})
.on("error", err => {
rej(err);
})
);
const response: { data: string; code: number } = await new Promise(
(res, rej) =>
https
.get("https://petstore.swagger.io/v2/pet/123", resp => {
let data = "";
resp.on("data", chunk => {
data += chunk;
});
resp.on("end", () => {
res({ data, code: resp.statusCode! });
});
})
.on("error", err => {
rej(err);
})
);
expect(petStore.getCallCount()).toBe(2);
expect(petStore.getResponse({ path: /69/ }).code).toBe(200);
expect(petStore.getResponse({ path: /123/ }).code).toBe(404);
expect(response69.code).toBe(200);
expect(response.code).toBe(404);
});
it("Overwriting a response is simple", async () => {
petStore.withState(400);
petStore.withResponse(
req => req.url.pathname.includes("/store/order"),
"Something went wrong"
);
const response: { data: string; code: number } = await new Promise(
(res, rej) =>
https
.get("https://petstore.swagger.io/v2/store/order/69", resp => {
let data = "";
resp.on("data", chunk => {
data += chunk;
});
resp.on("end", () => {
res({ data, code: resp.statusCode! });
});
})
.on("error", err => {
rej(err);
})
);
expect(petStore.getCallCount()).toBe(1);
expect(petStore.getResponse().code).toBe(400);
expect(petStore.getResponse().response).toBe("Something went wrong");
expect(response.code).toBe(400);
expect(response.data).toBe("Something went wrong");
});
it("Matches a path with a space inside", async () => {
const response: string = await new Promise((res, rej) =>
https
.get("https://petstore.swagger.io/v2/store order", resp => {
let data = "";
resp.on("data", chunk => {
data += chunk;
});
resp.on("end", () => {
res(data);
});
})
.on("error", err => {
rej(err);
})
);
expect(petStore.getCallCount()).toBe(1);
});
});