Skip to content

Commit c702f98

Browse files
committed
Add browser test for endpoint without CORS.
1 parent 1d8a4c2 commit c702f98

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

tests/10-client-api.spec.common.cjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,27 @@ describe('http-client API', () => {
153153
}
154154
});
155155

156+
if(!isNode) {
157+
// browser check for endpoint without CORS
158+
it.only('handles a CORS error', async () => {
159+
let err;
160+
let response;
161+
const url = `http://${httpHost}/nocors`;
162+
try {
163+
response = await httpClient.get(url);
164+
} catch(e) {
165+
err = e;
166+
}
167+
should.not.exist(response);
168+
should.exist(err);
169+
err.message.should.equal(
170+
`Failed to fetch "${url}". Possible CORS error.`);
171+
should.not.exist(err.response);
172+
should.exist(err.requestUrl);
173+
err.requestUrl.should.equal(url);
174+
});
175+
}
176+
156177
it('handles a TimeoutError error', async () => {
157178
let err;
158179
let response;

tests/utils.cjs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,50 +62,54 @@ api.makeAgent = options => {
6262
function createApp() {
6363
const app = express();
6464

65-
app.use(cors());
66-
67-
app.get('/ping', (req, res) => {
65+
app.get('/ping', cors(), (req, res) => {
6866
res.json({
6967
pong: true
7068
});
7169
});
7270

73-
app.get('/json', (req, res) => {
71+
app.get('/json', cors(), (req, res) => {
7472
res.json({
7573
json: true
7674
});
7775
});
7876

79-
app.get('/html', (req, res) => {
77+
app.get('/html', cors(), (req, res) => {
8078
res.setHeader('Content-Type', 'text/html');
8179
res.send(
8280
'<!DOCTYPE html><html><head></head><body><p>HTML</p></body></html>'
8381
);
8482
});
8583

8684
// emulate http://httpbin.org/status/404
87-
app.get('/status/404', (req, res) => {
85+
app.get('/status/404', cors(), (req, res) => {
8886
res.status(404).send('NOT FOUND');
8987
});
9088

9189
// emulate https://httpstat.us/404
92-
app.get('/404', (req, res) => {
90+
app.get('/404', cors(), (req, res) => {
9391
res.status(404).json({
9492
code: 404,
9593
description: 'Not Found'
9694
});
9795
});
9896

99-
app.get('/delay/:seconds', async (req, res) => {
97+
app.get('/delay/:seconds', cors(), async (req, res) => {
10098
await delay(req.params.seconds * 1000);
10199
res.status(200).send();
102100
});
103101

104-
app.get('/headers', (req, res) => {
102+
app.get('/headers', cors(), (req, res) => {
105103
res.json({
106104
headers: req.headers
107105
});
108106
});
109107

108+
app.get('/nocors', (req, res) => {
109+
res.json({
110+
cors: false
111+
});
112+
});
113+
110114
return app;
111115
}

0 commit comments

Comments
 (0)