Skip to content

Commit e44acfd

Browse files
committed
add more HTTP requests to demo dashboard
1 parent 61abd22 commit e44acfd

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

webui/scripts/dashboard-demo/demo-configs/petstore.ts

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { fake } from 'mokapi/faker'
44
export default function() {
55
on('http', (request, response) => {
66
if (request.operationId === 'getUserByName') {
7-
87
const username = request.path.username;
98
console.log(`[HTTP] Incoming request → GET /user/${username}`);
109
console.log(`[HTTP] Operation matched: getUserByName`);
@@ -18,10 +17,67 @@ export default function() {
1817
response.statusCode = 404
1918
}
2019
}
20+
switch (request.key) {
21+
case '/user/login': {
22+
const username = request.query.username;
23+
if (users.has(username)) {
24+
const user = users.get(username);
25+
console.log(`[Script] User ${username} found`)
26+
if (user.password === request.query.password) {
27+
response.data = 'ok'
28+
} else {
29+
console.log(`[Script] Password for ${username} is invalid`)
30+
response.statusCode = 400;
31+
}
32+
} else {
33+
console.log(`[Script] User ${username} not found`)
34+
response.statusCode = 400;
35+
}
36+
break;
37+
}
38+
case '/store/order': {
39+
orders.set(request.body.id, request.body);
40+
response.data = request.body;
41+
break;
42+
}
43+
case '/store/order/{orderId}': {
44+
if (request.method === 'GET') {
45+
const order = orders.get(request.path.orderId);
46+
if (order) {
47+
response.data = order;
48+
} else {
49+
response.statusCode = 404;
50+
}
51+
} else if (request.method === 'DELETE') {
52+
const orderId = request.path.orderId;
53+
if (orders.delete(orderId)) {
54+
response.statusCode = 204;
55+
} else {
56+
response.statusCode = 404;
57+
}
58+
}
59+
break;
60+
}
61+
case '/user/logout': {
62+
// force a script error with undefined variable user
63+
console.log(`logout current user ${user}`)
64+
break;
65+
}
66+
}
2167
})
2268
}
2369

2470
const users = new Map([
71+
['ajohnson', {
72+
id: fake({ type: 'integer', format: 'int64' }),
73+
username: 'ajohnson',
74+
firstName: 'Alice',
75+
lastName: 'Johnson',
76+
77+
password: 'anothersecretpassword456',
78+
phone: fake({ type: 'string', pattern: '(?:(?:\\+|0{0,2})91(\\s*[\\- ]\\s*)?|[0 ]?)?[789]\\d{9}|(\\d[ -]?){10}\\d' }),
79+
userStatus: 1
80+
}],
2581
['bmiller', {
2682
id: fake({ type: 'integer', format: 'int64' }),
2783
username: 'bmiller',
@@ -32,4 +88,13 @@ const users = new Map([
3288
phone: fake({ type: 'string', pattern: '(?:(?:\\+|0{0,2})91(\\s*[\\- ]\\s*)?|[0 ]?)?[789]\\d{9}|(\\d[ -]?){10}\\d' }),
3389
userStatus: 1
3490
}]
35-
]);
91+
]);
92+
93+
const orders = new Map([
94+
[1, {
95+
petId: 74959,
96+
quantity: 1,
97+
shipDate: '2011-01-08T16:59:15Z',
98+
status: 'delivered'
99+
}]
100+
])

webui/scripts/dashboard-demo/drive-http.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export async function driveHttp() {
2-
await fetch('http://localhost/v2/pet/10', { headers: { api_key: 'demo' }})
2+
await fetch('http://localhost/v2/pet/10', { headers: { Accept: 'application/json', api_key: 'demo' }})
33
await fetch('http://localhost/v2/pet', {
44
method: 'POST',
55
body: JSON.stringify({ name: 'Milo', photoUrls: [] }),
@@ -10,4 +10,42 @@ export async function driveHttp() {
1010
});
1111

1212
await fetch('http://localhost/v2/user/bmiller');
13+
14+
// invalid request without password
15+
await fetch('http://localhost/v2/user/login?username=ajohnson');
16+
// valid
17+
await fetch('http://localhost/v2/user/login?username=ajohnson&password=anothersecretpassword456');
18+
// invalid password
19+
await fetch('http://localhost/v2/user/login?username=bmiller&password=12345');
20+
await fetch('http://localhost/v2/user/login?username=bmiller&password=mysecretpassword123');
21+
// request with script error
22+
await fetch('http://localhost/v2/user/login?username=bmiller&password=mysecretpassword123');
23+
// script error
24+
await fetch('http://localhost/v2/user/logout');
25+
26+
// should only return pets with the given status.
27+
await fetch('http://localhost/v2/pet/findByStatus?status=available,pending', {
28+
headers: {
29+
Accept: 'application/xml',
30+
Authorization: 'demo'
31+
}
32+
});
33+
34+
await fetch('http://localhost/v2/store/order/1')
35+
await fetch('http://localhost/v2/store/order', {
36+
method: 'POST',
37+
headers: {
38+
Accept: 'application/json',
39+
'Content-Type': 'application/json',
40+
},
41+
body: JSON.stringify({
42+
id: 2,
43+
petId: 14921,
44+
quantity: 2,
45+
status: 'placed'
46+
})
47+
})
48+
await fetch('http://localhost/v2/store/order/2', {
49+
method: 'DELETE',
50+
})
1351
}

0 commit comments

Comments
 (0)