swift/test/cors/test-info.js
Tim Burke c5152ed4d3 Add some functional CORS tests
If you've got selenium installed (and working), the whole thing can be
automated pretty well; run main.py, wait while some windows pop up (or
use xvfb-run to run things on a virtual display), then check out what
tests were run on which browsers and whether any of them failed. Exit
code is the number of failed tests.

Includes tests against:
- Account
- Containers, with various ACLs/CORS settings
- Objects
- /info
- SLOs
- DLOs
- Symlinks

Include a gate job that runs the tests in firefox.

Areas for future work:

- Install chromium and chromedriver in the gate; tests should
  automatically pick up on the fact that it's available
- Capture the web browser's console logs, too, so we can get
  more info when things go wrong

Change-Id: Ic1d3a062419f1133c6e2f00a598867d567358c9f
2021-03-01 10:09:03 -08:00

61 lines
1.9 KiB
JavaScript

import {
runTests,
MakeRequest,
HasStatus,
HasHeaders,
DoesNotHaveHeaders,
HasNoBody,
CorsBlocked
} from './harness.js'
function CheckInfoHeaders (resp) {
return Promise.resolve(resp)
.then(HasHeaders({ 'Content-Type': 'application/json; charset=UTF-8' }))
.then(HasHeaders(['X-Trans-Id']))
.then(DoesNotHaveHeaders([
'X-Openstack-Request-Id', // TODO: this is blocked by CORS but almost certainly shouldn't
'X-Timestamp',
'Accept-Ranges',
'Access-Control-Allow-Origin',
'Access-Control-Expose-Headers',
'Date',
'Content-Range'
]))
}
function CheckInfoBody (resp) {
const clusterInfo = JSON.parse(resp.responseText)
if (!('swift' in clusterInfo)) {
throw new Error('Expected to find "swift" in /info response; ' +
'got ' + JSON.stringify(clusterInfo))
}
if (!('version' in clusterInfo.swift)) {
throw new Error('Expected to find "swift.version" in /info response; ' +
'got ' + JSON.stringify(clusterInfo.swift))
}
console.log('Tested against Swift version ' + clusterInfo.swift.version)
return clusterInfo
}
export const GetClusterInfo = MakeRequest('GET', '/info')
.then(HasStatus(200, 'OK'))
.then(CheckInfoHeaders)
.then(CheckInfoBody)
// TODO: /info should probably get an automatic access-control-allow-origin: *
runTests('cluster info', [
['GET', () => GetClusterInfo],
['GET with header', () => MakeRequest('GET', '/info', { 'X-Trans-Id-Extra': 'my-tracker' })
// 200, but missing Access-Control-Allow-Origin
.then(CorsBlocked)],
['HEAD', () => MakeRequest('HEAD', '/info')
.then(HasStatus(200, 'OK'))
.then(CheckInfoHeaders)
.then(HasNoBody)],
['OPTIONS', () => MakeRequest('OPTIONS', '/info')
// 200, but missing Access-Control-Allow-Origin
.then(CorsBlocked)],
['POST', () => MakeRequest('POST', '/info')
// 405, but missing Access-Control-Allow-Origin
.then(CorsBlocked)]
])