Skip to content

Commit f1dd55b

Browse files
committed
refactor: restructure test data and utils
1 parent dfc0550 commit f1dd55b

File tree

13 files changed

+366
-376
lines changed

13 files changed

+366
-376
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "docs/openapi-cop.wiki"]
2+
path = docs/openapi-cop.wiki
3+
url = https://github.com/EXXETA/openapi-cop.wiki.git

docs/openapi-cop.wiki

Submodule openapi-cop.wiki added at 892a4ee

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"fix": "gts fix",
3333
"start": "node build/src/cli",
3434
"dev-start": "npm run compile && node build/src/cli --port 3000 --target \"http://localhost:8889\" --file",
35-
"dev-start-along-mock": "(export DEBUG=openapi-cop:* || set DEBUG=openapi-cop:*) && npm run compile && node build/test/util/spawn",
35+
"dev-start-along-mock": "(export DEBUG=openapi-cop:* || set DEBUG=openapi-cop:*) && npm run compile && node build/test/scripts/spawn",
3636
"pretest": "npm run compile",
3737
"test": "mocha --bail --timeout 60000 --exit build/test/*.test.js",
3838
"test:docker": "bash ./test/docker/run-docker-test.bash",

test/02.integration.test.ts

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,40 @@
11
// tslint:disable: only-arrow-functions
22

3-
/**
4-
* NOTE: When debugging test cases, make use of the script dev-start-along-mock
5-
* to spawn a proxy server along with a mock server using a specified
6-
* OpenAPI file, e.g.
7-
*
8-
* npm run dev-start-along-mock -- test/schemas/v3/3-parameters.yaml
9-
*
10-
* Afterwards, you can use curl to reproduce the requests.
11-
*/
12-
13-
import { assert } from 'chai';
3+
import {assert} from 'chai';
144
import * as path from 'path';
155

16-
import {
17-
INVALID_TEST_REQUESTS,
18-
STRICTLY_INVALID_TEST_REQUESTS,
19-
STRICTLY_VALID_TEST_REQUESTS,
20-
VALID_TEST_REQUESTS,
21-
} from './test_data/test-requests';
22-
import {
23-
NON_COMPLIANT_SERVERS,
24-
STRICTLY_NON_COMPLIANT_SERVERS,
25-
} from './test_data/test-target-servers';
26-
import { killProcesses } from './util/process';
27-
import {
28-
spawnProxyServer,
29-
testRequestForEachFile,
30-
testRequestForEachFileWithServers,
31-
} from './util/testing';
32-
6+
import {INVALID_TEST_REQUESTS, STRICTLY_INVALID_TEST_REQUESTS,} from './test-requests/invalid-requests';
7+
import {INVALID_RESPONSES, STRICTLY_INVALID_RESPONSES,} from './test-responses/invalid-responses';
8+
import {killProcesses} from './util/process';
9+
import {testRequestForEachFile, testRequestForEachFileWithServers,} from './util/testing';
10+
import {DEFAULT_OPENAPI_FILE, PROXY_PORT, SCHEMAS_DIR, TARGET_SERVER_PORT,} from './config';
11+
import {ChildProcess} from 'child_process';
12+
import {Readable} from 'stream';
13+
import axios, {AxiosRequestConfig} from 'axios';
14+
import {runProxy} from '../src/app';
15+
import {closeServer} from '../src/util';
16+
import {STRICTLY_VALID_TEST_REQUESTS, VALID_TEST_REQUESTS} from './test-requests/valid-requests';
17+
import {spawnProxyServer} from './util/server';
3318
import findProcess = require('find-process');
34-
import {
35-
PROXY_PORT,
36-
TARGET_SERVER_PORT,
37-
SCHEMAS_DIR,
38-
DEFAULT_OPENAPI_FILE,
39-
} from './config';
40-
import { ChildProcess } from 'child_process';
41-
import { Readable } from 'stream';
42-
import axios, { AxiosRequestConfig } from 'axios';
43-
import { runProxy } from '../src/app';
44-
import { closeServer } from '../src/util';
4519

46-
describe('integration.test.js', function() {
20+
describe('integration.test.js', function () {
4721
this.slow(1000 * 15); // 15 seconds
4822

4923
const contentType = 'application/json';
5024
const clients = {
5125
proxy: axios.create({
5226
baseURL: `http://localhost:${PROXY_PORT}`,
53-
headers: { 'content-type': contentType },
27+
headers: {'content-type': contentType},
5428
validateStatus: () => true,
5529
}),
5630
target: axios.create({
5731
baseURL: `http://localhost:${TARGET_SERVER_PORT}`,
58-
headers: { 'content-type': contentType },
32+
headers: {'content-type': contentType},
5933
validateStatus: () => true,
6034
}),
6135
};
6236

63-
before(async function() {
37+
before(async function () {
6438
// Kill active processes listening on any of the given ports
6539
const pid1 = await findProcess('port', PROXY_PORT);
6640
const pid2 = await findProcess('port', TARGET_SERVER_PORT);
@@ -71,10 +45,10 @@ describe('integration.test.js', function() {
7145
]);
7246
});
7347

74-
describe('OpenAPI v3', function() {
48+
describe('OpenAPI v3', function () {
7549
const schemasDirV3 = path.join(SCHEMAS_DIR, 'v3');
7650

77-
describe('Invariance tests', function() {
51+
describe('Invariance tests', function () {
7852
testRequestForEachFile({
7953
testTitle:
8054
'should return the same status and response bodies as the target server in silent mode',
@@ -113,7 +87,7 @@ describe('integration.test.js', function() {
11387
});
11488
});
11589

116-
it('should return the source request object inside the response header', async function() {
90+
it('should return the source request object inside the response header', async function () {
11791
console.log('Starting proxy server...');
11892
const server = await runProxy({
11993
port: PROXY_PORT,
@@ -126,7 +100,7 @@ describe('integration.test.js', function() {
126100
const originalRequest: AxiosRequestConfig = {
127101
method: 'GET',
128102
url: '/pets',
129-
data: JSON.stringify({ search: 'something' }),
103+
data: JSON.stringify({search: 'something'}),
130104
};
131105

132106
const proxyResponse = await clients.proxy.request(originalRequest);
@@ -178,7 +152,7 @@ describe('integration.test.js', function() {
178152
assert.isBoolean(validationResults[k]['valid']);
179153
assert(
180154
validationResults[k]['errors'] === null ||
181-
Array.isArray(validationResults[k]['errors']),
155+
Array.isArray(validationResults[k]['errors']),
182156
'validation error should be null or an array',
183157
);
184158
if (Array.isArray(validationResults[k]['errors'])) {
@@ -204,7 +178,7 @@ describe('integration.test.js', function() {
204178
},
205179
});
206180

207-
it('should fail when target server is not available', async function() {
181+
it('should fail when target server is not available', async function () {
208182
this.timeout(10000);
209183

210184
console.log('Starting proxy server...');
@@ -227,7 +201,7 @@ describe('integration.test.js', function() {
227201
}
228202
});
229203

230-
clients.proxy.request({ method: 'GET', url: '/pets' });
204+
clients.proxy.request({method: 'GET', url: '/pets'});
231205

232206
return new Promise((resolve) => {
233207
ps.on('exit', (code: number) => {
@@ -307,7 +281,7 @@ describe('integration.test.js', function() {
307281
testTitle:
308282
'should return correct validation errors for invalid RESponses',
309283
dir: schemasDirV3,
310-
testServers: NON_COMPLIANT_SERVERS.v3,
284+
testServers: INVALID_RESPONSES.v3,
311285
client: clients,
312286
callback(proxyRes, targetRes, fileName, expectedError) {
313287
assert.isDefined(
@@ -366,7 +340,7 @@ describe('integration.test.js', function() {
366340
testTitle:
367341
'should return correct validation errors for strictly invalid RESponses',
368342
dir: schemasDirV3,
369-
testServers: STRICTLY_NON_COMPLIANT_SERVERS.v3,
343+
testServers: STRICTLY_INVALID_RESPONSES.v3,
370344
client: clients,
371345
defaultForbidAdditionalProperties: true,
372346
callback(proxyRes, targetRes, fileName, expectedError) {

test/scripts/spawn.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Utility script for spawning a openapi-cop instance and a mock server in parallel
3+
* that are both based on a given OpenAPI document.
4+
*
5+
* node ./spawn [openapi-path]
6+
*
7+
* openapi-path (optional): path to the OpenAPI file on which both the proxy server
8+
* and the mock server will be based on. Defaults to a hard-coded file.
9+
*
10+
* This script is called/aliased by `npm run dev-start-along-mock`.
11+
*/
12+
13+
import * as path from 'path';
14+
15+
import {PROXY_PORT, TARGET_SERVER_PORT} from '../config';
16+
import {spawnProxyWithMockServer} from '../util/server';
17+
18+
const apiDocFile =
19+
process.argv[2] ||
20+
path.resolve(__dirname, '../../../test/schemas/v3/3-parameters.yaml');
21+
22+
spawnProxyWithMockServer(PROXY_PORT, TARGET_SERVER_PORT, apiDocFile, {
23+
stdio: 'inherit',
24+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* NOTE: To enable tests for a specific OpenAPI file, add the file name
3+
* as a key of the object and add at least one TestRequestConfig to the array.
4+
*/
5+
import {TestRequests} from 'test-requests';
6+
7+
export const INVALID_TEST_REQUESTS: { [dir: string]: TestRequests } = {
8+
v3: {
9+
'2-path.yaml': [
10+
{
11+
method: 'POST',
12+
url: '/echo',
13+
data: JSON.stringify({blurp: 'BLUUURP!'}),
14+
expectedError: {keyword: 'required'},
15+
},
16+
],
17+
'3-parameters.yaml': [
18+
{
19+
method: 'POST',
20+
url: '/pets/cat?limit=nonsense',
21+
data: JSON.stringify({search: 'Garfield'}),
22+
expectedError: {keyword: 'type'},
23+
},
24+
{
25+
method: 'POST',
26+
url: '/pets/cat',
27+
data: '{}',
28+
expectedError: {keyword: 'required'},
29+
},
30+
],
31+
'6-examples.yaml': [
32+
{
33+
method: 'GET',
34+
url: '/pets/cat',
35+
expectedError: {keyword: 'type'},
36+
},
37+
],
38+
},
39+
};
40+
41+
export const STRICTLY_INVALID_TEST_REQUESTS: { [dir: string]: TestRequests } = {
42+
v3: {
43+
'2-path.yaml': [
44+
{
45+
method: 'POST',
46+
url: '/echo',
47+
data: JSON.stringify({input: 'Marco!', sponge: 'Bob'}),
48+
expectedError: {keyword: 'additionalProperties'},
49+
},
50+
],
51+
'3-parameters.yaml': [
52+
{
53+
method: 'POST',
54+
url: '/pets/cat?limit=3&test=false',
55+
data: JSON.stringify({search: 'Dark Side of the Moon'}),
56+
expectedError: {keyword: 'additionalProperties'},
57+
},
58+
],
59+
},
60+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* NOTE: To enable tests for a specific OpenAPI file, add the file name
3+
* as a key of the object and add at least one `TestRequestConfig` to the array.
4+
*/
5+
import {TestRequests} from 'test-requests';
6+
7+
export const VALID_TEST_REQUESTS: { [dir: string]: TestRequests } = {
8+
v3: {
9+
'2-path.yaml': [
10+
{
11+
method: 'POST',
12+
url: '/echo',
13+
data: JSON.stringify({input: 'ECHO!'}),
14+
},
15+
],
16+
'3-parameters.yaml': [
17+
{
18+
method: 'POST',
19+
url: '/pets/cats',
20+
data: JSON.stringify({search: 'Garfield'}),
21+
},
22+
],
23+
'4-refs.yaml': [
24+
{
25+
method: 'POST',
26+
url: '/echo',
27+
data: JSON.stringify({input: 'ECHO!'}),
28+
},
29+
],
30+
'5-external-refs.yaml': [
31+
{
32+
method: 'POST',
33+
url: '/echo',
34+
data: JSON.stringify({input: 'ECHO!'}),
35+
},
36+
],
37+
'6-examples.yaml': [
38+
{method: 'GET', url: '/pets'},
39+
{
40+
method: 'POST',
41+
url: '/pets',
42+
data: JSON.stringify({search: 'Scooby'}),
43+
},
44+
],
45+
'7-petstore.yaml': [
46+
{method: 'GET', url: '/pets'},
47+
{method: 'GET', url: '/pets/1'},
48+
],
49+
},
50+
};
51+
52+
export const STRICTLY_VALID_TEST_REQUESTS: { [dir: string]: TestRequests } = {
53+
v3: {
54+
'3-parameters.yaml': [
55+
{
56+
method: 'POST',
57+
url: '/pets/cats',
58+
// request contains an additional property, but the OpenAPI document
59+
// explicitly allows it, so it should pass
60+
data: JSON.stringify({
61+
search: 'Is anybody in there?',
62+
strict: false,
63+
watson: 'Sherlock!',
64+
}),
65+
},
66+
],
67+
},
68+
};

0 commit comments

Comments
 (0)