|
| 1 | +import { assert } from 'chai'; |
| 2 | +import getPort from 'get-port'; |
| 3 | +import http from 'http'; |
| 4 | +import fs from 'fs-extra'; |
| 5 | +import Server from '../index.js'; |
| 6 | +import { untilResponse, untilParseResult } from './RequestUtils.js'; |
| 7 | + |
| 8 | +describe('CORS settings', () => { |
| 9 | + /** @type Server */ |
| 10 | + let instance; |
| 11 | + /** @type number */ |
| 12 | + let port; |
| 13 | + let simpleRamlApi; |
| 14 | + before(async () => { |
| 15 | + port = await getPort(); |
| 16 | + simpleRamlApi = await fs.readFile('test/apis/simple.raml', 'utf8'); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('CORS enabled with defaults', () => { |
| 20 | + before(async () => { |
| 21 | + port = await getPort(); |
| 22 | + instance = new Server({ |
| 23 | + cors: { |
| 24 | + enabled: true, |
| 25 | + } |
| 26 | + }); |
| 27 | + instance.setupRoutes(); |
| 28 | + await instance.startHttp(port); |
| 29 | + simpleRamlApi = await fs.readFile('test/apis/simple.raml', 'utf8'); |
| 30 | + }); |
| 31 | + |
| 32 | + after(async () => { |
| 33 | + await instance.cleanup(); |
| 34 | + await instance.stopHttp(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('has the CORS headers for OPTIONS request', async () => { |
| 38 | + const request = http.request({ |
| 39 | + hostname: 'localhost', |
| 40 | + port, |
| 41 | + path: '/text', |
| 42 | + method: 'OPTIONS', |
| 43 | + headers: { |
| 44 | + 'Content-Type': 'application/raml', |
| 45 | + 'Authorization': 'Basic test', |
| 46 | + 'x-api-vendor': 'RAML 1.0', |
| 47 | + 'origin': 'https://www.api.com', |
| 48 | + 'Accept-Encoding': 'gzip,deflate', |
| 49 | + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:71.0) Gecko/20100101 Firefox/71.0', |
| 50 | + 'Accept': 'application/json,application/ld+json', |
| 51 | + 'Connection': 'close', |
| 52 | + 'Access-Control-Request-Method': 'POST', |
| 53 | + 'Access-Control-Request-Headers': 'x-api-vendor, Content-Type', |
| 54 | + }, |
| 55 | + }); |
| 56 | + const result = await untilResponse(request); |
| 57 | + const { statusCode, headers } = result; |
| 58 | + assert.equal(statusCode, 204, 'has the 204 status code'); |
| 59 | + |
| 60 | + assert.equal(headers['access-control-allow-origin'], 'https://www.api.com', 'has access-control-allow-origin'); |
| 61 | + assert.equal(headers['access-control-allow-methods'], 'GET,PUT,POST,DELETE', 'has access-control-allow-methods'); |
| 62 | + assert.equal(headers['access-control-allow-headers'], 'x-api-vendor, Content-Type', 'has access-control-allow-headers'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('has the CORS headers for POST request', async () => { |
| 66 | + const request = http.request({ |
| 67 | + hostname: 'localhost', |
| 68 | + port, |
| 69 | + path: '/text', |
| 70 | + method: 'POST', |
| 71 | + headers: { |
| 72 | + 'Content-Type': 'application/raml', |
| 73 | + 'Authorization': 'Basic test', |
| 74 | + 'x-api-vendor': 'RAML 1.0', |
| 75 | + 'origin': 'https://www.api.com', |
| 76 | + 'Accept-Encoding': 'gzip,deflate', |
| 77 | + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:71.0) Gecko/20100101 Firefox/71.0', |
| 78 | + 'Accept': 'application/json,application/ld+json', |
| 79 | + 'Connection': 'close', |
| 80 | + 'Access-Control-Request-Method': 'POST', |
| 81 | + 'Access-Control-Request-Headers': 'x-api-vendor, Content-Type', |
| 82 | + }, |
| 83 | + }); |
| 84 | + request.write(simpleRamlApi); |
| 85 | + const result = await untilResponse(request); |
| 86 | + const { headers } = result; |
| 87 | + |
| 88 | + assert.equal(headers['access-control-allow-origin'], 'https://www.api.com', 'has access-control-allow-origin'); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('CORS enabled with passed configuration', () => { |
| 93 | + before(async () => { |
| 94 | + port = await getPort(); |
| 95 | + instance = new Server({ |
| 96 | + cors: { |
| 97 | + enabled: true, |
| 98 | + cors: { |
| 99 | + origin: 'https://my.domain.org' |
| 100 | + }, |
| 101 | + } |
| 102 | + }); |
| 103 | + instance.setupRoutes(); |
| 104 | + await instance.startHttp(port); |
| 105 | + simpleRamlApi = await fs.readFile('test/apis/simple.raml', 'utf8'); |
| 106 | + }); |
| 107 | + |
| 108 | + after(async () => { |
| 109 | + await instance.cleanup(); |
| 110 | + await instance.stopHttp(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('has the CORS headers for OPTIONS request', async () => { |
| 114 | + const request = http.request({ |
| 115 | + hostname: 'localhost', |
| 116 | + port, |
| 117 | + path: '/text', |
| 118 | + method: 'OPTIONS', |
| 119 | + headers: { |
| 120 | + 'Content-Type': 'application/raml', |
| 121 | + 'Authorization': 'Basic test', |
| 122 | + 'x-api-vendor': 'RAML 1.0', |
| 123 | + 'origin': 'https://www.api.com', |
| 124 | + 'Accept-Encoding': 'gzip,deflate', |
| 125 | + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:71.0) Gecko/20100101 Firefox/71.0', |
| 126 | + 'Accept': 'application/json,application/ld+json', |
| 127 | + 'Connection': 'close', |
| 128 | + 'Access-Control-Request-Method': 'POST', |
| 129 | + 'Access-Control-Request-Headers': 'x-api-vendor, Content-Type', |
| 130 | + }, |
| 131 | + }); |
| 132 | + const result = await untilResponse(request); |
| 133 | + const { statusCode, headers } = result; |
| 134 | + assert.equal(statusCode, 204, 'has the 204 status code'); |
| 135 | + |
| 136 | + assert.equal(headers['access-control-allow-origin'], 'https://my.domain.org', 'has access-control-allow-origin'); |
| 137 | + assert.equal(headers['access-control-allow-methods'], 'GET,HEAD,PUT,POST,DELETE,PATCH', 'has access-control-allow-methods'); |
| 138 | + assert.equal(headers['access-control-allow-headers'], 'x-api-vendor, Content-Type', 'has access-control-allow-headers'); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments