Skip to content

Commit

Permalink
test(oas): oas query parameters style and explode tests
Browse files Browse the repository at this point in the history
relates to #120
  • Loading branch information
pmstss committed Feb 11, 2022
1 parent b15a994 commit 2a3cc86
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 128 deletions.
2 changes: 1 addition & 1 deletion packages/oas/src/converter/DefaultConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ export class DefaultConverter implements Converter {
switch (options.style) {
case 'spaceDelimited':
case 'ssv':
return '%20';
return ' ';
case 'pipeDelimited':
case 'pipes':
return '|';
Expand Down
188 changes: 61 additions & 127 deletions packages/oas/tests/DefaultConverter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,79 @@
import githubSwagger from './fixtures/github.swagger.json';
import { ConvertError, oas2har } from '../src';
import yaml from 'js-yaml';
import { OpenAPIV2, OpenAPIV3, Request } from '@har-sdk/core';
import yaml, { load } from 'js-yaml';
import { OpenAPIV2, Request } from '@har-sdk/core';
import { use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { resolve } from 'path';
import { readFile } from 'fs';
import { readFile, readFileSync } from 'fs';
import { promisify } from 'util';
import 'chai/register-should';

use(chaiAsPromised);

describe('DefaultConverter', () => {
describe('convert', () => {
it('should convert GitHub OAS v2 (JSON) to HAR', async () => {
const expected = JSON.parse(
await promisify(readFile)(
resolve('./tests/fixtures/github.swagger.result.json'),
'utf-8'
)
);

const result: Request[] = await oas2har(
githubSwagger as unknown as OpenAPIV2.Document
);

result.should.deep.eq(expected);
});

it('should convert Petstore OAS v3 (YAML) to HAR', async () => {
const content: string = await promisify(readFile)(
resolve('./tests/fixtures/petstore.oas.yaml'),
'utf8'
);

const expected = JSON.parse(
await promisify(readFile)(
resolve('./tests/fixtures/petstore.oas.result.json'),
'utf-8'
)
);

const result: Request[] = await oas2har(
yaml.load(content) as OpenAPIV3.Document
);

result.should.deep.eq(expected);
});

it('should generate multipart/form-data according to OAS definition', async () => {
const content: string = await promisify(readFile)(
resolve('./tests/fixtures/multipart.oas.yaml'),
'utf8'
);

const expected = JSON.parse(
await promisify(readFile)(
resolve('./tests/fixtures/multipart.oas.result.json'),
'utf-8'
)
);

const result: Request[] = await oas2har(
yaml.load(content) as OpenAPIV3.Document
);

result.should.deep.eq(expected);
});

it('should convert OAS v3 spec even if parameters are declared via ref', async () => {
const content: string = await promisify(readFile)(
resolve('./tests/fixtures/refs.oas.yaml'),
'utf8'
);

const expected = JSON.parse(
await promisify(readFile)(
resolve('./tests/fixtures/refs.oas.result.json'),
[
{
input: 'github.swagger.json',
expected: 'github.swagger.result.json',
message: 'should convert GitHub OAS v2 (JSON) to HAR'
},
{
input: 'petstore.oas.yaml',
expected: 'petstore.oas.result.json',
message: 'should convert Petstore OAS v3 (YAML) to HAR'
},
{
input: 'multipart.oas.yaml',
expected: 'multipart.oas.result.json',
message:
'should generate multipart/form-data according to OAS definition'
},
{
input: 'refs.oas.yaml',
expected: 'refs.oas.result.json',
message:
'should convert OAS v3 spec even if parameters are declared via ref'
},
{
input: 'servers-with-variables.oas.yaml',
expected: 'servers-with-variables.oas.result.json',
message: 'should substitute variables in servers'
},
{
input: 'empty-schema.swagger.yaml',
expected: 'empty-schema.swagger.result.json',
message: 'should correctly handle empty schemas (swagger)'
},
{
input: 'empty-schema.oas.yaml',
expected: 'empty-schema.oas.result.json',
message: 'should correctly handle empty schemas (oas)'
},
{
// TODO support for deepObject style
input: 'query-params.oas.yaml',
expected: 'query-params.oas.result.json',
message: 'should correctly convert oas query parameters'
}
].forEach(({ input: inputFile, expected: expectedFile, message }) => {
it(message, async () => {
const content = readFileSync(
resolve(`./tests/fixtures/${inputFile}`),
'utf-8'
)
);

const result: Request[] = await oas2har(
yaml.load(content) as OpenAPIV3.Document
);

result.should.deep.eq(expected);
});

it('should substitute variables in servers', async () => {
const content: string = await promisify(readFile)(
resolve('./tests/fixtures/servers-with-variables.oas.yaml'),
'utf8'
);
);
const input = inputFile.endsWith('json')
? JSON.parse(content)
: load(content);

const expected = JSON.parse(
await promisify(readFile)(
resolve('./tests/fixtures/servers-with-variables.oas.result.json'),
'utf-8'
)
);
const expected = JSON.parse(
readFileSync(resolve(`./tests/fixtures/${expectedFile}`), 'utf-8')
);

const result: Request[] = await oas2har(
yaml.load(content) as OpenAPIV3.Document
);
const result: Request[] = await oas2har(input as any);

result.should.deep.eq(expected);
result.should.deep.eq(expected);
});
});

[
Expand Down Expand Up @@ -179,39 +147,5 @@ describe('DefaultConverter', () => {
.and.eventually.have.property('jsonPointer', expected);
})
);

[
{
input: 'empty-schema.swagger.yaml',
expected: 'empty-schema.swagger.result.json'
},
{
input: 'empty-schema.oas.yaml',
expected: 'empty-schema.oas.result.json'
}
].forEach(({ input: inputFilename, expected: expectedFilename }) =>
it(`should correctly handle empty schemas ${inputFilename.replace(
/^[^.]+\.(.+)\.yaml$/,
'($1)'
)}`, async () => {
const input: OpenAPIV2.Document = yaml.load(
await promisify(readFile)(
resolve(`./tests/fixtures/${inputFilename}`),
'utf8'
)
) as OpenAPIV2.Document;

const expected = JSON.parse(
await promisify(readFile)(
resolve(`./tests/fixtures/${expectedFilename}`),
'utf8'
)
);

const result = oas2har(input);

return result.should.eventually.deep.eq(expected);
})
);
});
});
116 changes: 116 additions & 0 deletions packages/oas/tests/fixtures/query-params.oas.result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
[
{
"queryString": [
{
"name": "p",
"value": "lorem,lorem"
}
],
"url": "http://petstore.swagger.io/v1/formStyleParamArray?p=lorem%2Clorem",
"method": "GET",
"headers": [],
"httpVersion": "HTTP/1.1",
"cookies": [],
"headersSize": 0,
"bodySize": 0
},
{
"queryString": [
{
"name": "p",
"value": "lorem"
},
{
"name": "p",
"value": "lorem"
}
],
"url": "http://petstore.swagger.io/v1/formStyleParamArrayExploded?p=lorem&p=lorem",
"method": "GET",
"headers": [],
"httpVersion": "HTTP/1.1",
"cookies": [],
"headersSize": 0,
"bodySize": 0
},
{
"queryString": [
{
"name": "p",
"value": "key,x,value,42"
}
],
"url": "http://petstore.swagger.io/v1/formStyleParamObject?p=key%2Cx%2Cvalue%2C42",
"method": "GET",
"headers": [],
"httpVersion": "HTTP/1.1",
"cookies": [],
"headersSize": 0,
"bodySize": 0
},
{
"queryString": [
{
"name": "key",
"value": "x"
},
{
"name": "value",
"value": "42"
}
],
"url": "http://petstore.swagger.io/v1/formStyleParamObjectExploded?key=x&value=42",
"method": "GET",
"headers": [],
"httpVersion": "HTTP/1.1",
"cookies": [],
"headersSize": 0,
"bodySize": 0
},
{
"queryString": [
{
"name": "p",
"value": "42 42"
},
{
"name": "pExploded",
"value": "42"
},
{
"name": "pExploded",
"value": "42"
}
],
"url": "http://petstore.swagger.io/v1/spaceDelimitedStyleParam?p=42+42&pExploded=42&pExploded=42",
"method": "GET",
"headers": [],
"httpVersion": "HTTP/1.1",
"cookies": [],
"headersSize": 0,
"bodySize": 0
},
{
"queryString": [
{
"name": "p",
"value": "42|42"
},
{
"name": "pExploded",
"value": "42"
},
{
"name": "pExploded",
"value": "42"
}
],
"url": "http://petstore.swagger.io/v1/pipeDelimitedStyleParam?p=42%7C42&pExploded=42&pExploded=42",
"method": "GET",
"headers": [],
"httpVersion": "HTTP/1.1",
"cookies": [],
"headersSize": 0,
"bodySize": 0
}
]
Loading

0 comments on commit 2a3cc86

Please sign in to comment.