-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (117 loc) · 3.04 KB
/
index.js
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
const express = require('express');
const app = express();
let server;
const delayMiddleware = require('./delayMiddleware');
const proxyMiddleware = require('./proxyMiddleware');
const redirectMiddleware = require('./redirectMiddleware');
const port = process.env.PORT || 3000;
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});
app.all(
'/:seconds',
delayMiddleware,
proxyMiddleware,
redirectMiddleware,
(req, res) => {
res.send('OK!');
}
);
app.get('/', (req, res) => {
const html = `
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
box-sizing: border-box;
background: linear-gradient(312deg, rgba(153,218,255,1) 0%, rgba(0,128,128,1) 100%);
color: white;
}
.wrapper {
margin: 0 auto;
max-width: 960px;
padding: 1rem;
}
.example {
font-family: monospace;
font-size: 1.5rem;
color: white;
}
section {
border-bottom: 1px solid rgba(255,255,255, .2);
margin-top: 1rem;
padding-bottom: 1rem;
}
section:first-child {
margin-top: 2rem;
}
</style>
</head>
<body>
<div class="wrapper">
<header>
<h1>😴 httsleep 😴</h1>
<h2>
Delay HTTP requests to test how your app behaves when requests take longer than expected.
</h2>
</header>
<main>
<section>
<h3>Proxy</h3>
<div>
<a class="example" href="https://httsleep.herokuapp.com/3/?proxyUrl=https://httpbin.org/headers">
/:seconds/?proxyUrl=:proxyUrl
</a>
</div>
</section>
<section>
<h3>Redirect</h3>
<div>
<a class="example" href="https://httsleep.herokuapp.com/3/?redirectUrl=https://httpbin.org/headers">
/:seconds/?redirectUrl=:redirectUrl
</a>
</div>
</section>
<section>
<h3>Delaying a 200 response</h3>
<div>
<a class="example" href="https://httsleep.herokuapp.com/3">
/:seconds
</a>
</div>
</section>
</main>
</div>
</body>
</html>
`;
res.send(html);
});
exports.start = cb => {
server = app.listen(port, () => {
const host = server.address().address;
const port = server.address().port;
if (cb) {
cb();
}
console.log('Example app listening at http://%s:%s', host, port);
});
};
exports.close = cb => {
if (server) {
server.close(cb);
} else {
cb();
}
};
if (module.id === require.main.id) {
exports.start();
}