Skip to content

Commit

Permalink
test(oas): add tests to cover these cases
Browse files Browse the repository at this point in the history
  • Loading branch information
derevnjuk committed Jan 27, 2022
1 parent c4e68ed commit 5416db2
Show file tree
Hide file tree
Showing 5 changed files with 478 additions and 2 deletions.
39 changes: 37 additions & 2 deletions packages/oas/tests/DefaultConverter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import githubSwagger from './fixtures/github.swagger.json';
import { oas2har } from '../src';
import { ConvertError } from '../src/errors';
import { ConvertError, oas2har } from '../src';
import yaml from 'js-yaml';
import { OpenAPIV2, OpenAPIV3, Request } from '@har-sdk/core';
import { use } from 'chai';
Expand Down Expand Up @@ -134,5 +133,41 @@ describe('DefaultConverter', () => {
.rejectedWith(ConvertError)
.and.eventually.have.property('jsonPointer', '/host');
});

[
{
input: 'wrong-schema-in-body.oas.yaml',
expected:
'/paths//store/order/{orderId}/put/requestBody/content/application/json/schema'
},
{
input: 'wrong-schema-in-header.oas.yaml',
expected: '/paths//store/order/{orderId}/put/parameters/2/schema'
},
{
input: 'wrong-schema-in-path.oas.yaml',
expected: '/paths//store/order/{orderId}/put/parameters/0/schema'
},
{
input: 'wrong-schema-in-query.oas.yaml',
expected: '/paths//store/order/{orderId}/put/parameters/1/schema'
}
].forEach(({ input, expected }) =>
it(`should throw an convert error on auto sampling issues in ${input.replace(
/^wrong-schema-in-(.+).oas.yaml$/,
'$1'
)}`, async () => {
const content: string = await promisify(readFile)(
resolve(`./tests/fixtures/${input}`),
'utf8'
);

const result = oas2har(yaml.load(content) as OpenAPIV2.Document);

return result.should.be
.rejectedWith(ConvertError)
.and.eventually.have.property('jsonPointer', expected);
})
);
});
});
111 changes: 111 additions & 0 deletions packages/oas/tests/fixtures/wrong-schema-in-body.oas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
openapi: 3.0.1
info:
title: Swagger Petstore
description:
'This is a sample server Petstore server. You can find out more about Swagger
at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
termsOfService: http://swagger.io/terms/
contact:
email: [email protected]
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
servers:
- url: https://{instance}.swagger.io:{port}/{basePath}
variables:
instance:
default: petstore
port:
enum:
- '8443'
- '443'
default: '8443'
basePath:
default: v2
paths:
/store/order/{orderId}:
put:
parameters:
- name: orderId
in: path
required: true
schema:
maximum: 10.0
minimum: 1.0
type: integer
format: int64
- name: exclude
in: query
required: true
schema:
type: array
items:
type: string
enum: ['petId', 'quantity', 'shipDate', 'status', 'complete']
minItems: 1
maxItems: 5
uniqueItems: true
- name: api_key
in: header
schema:
type: string
requestBody:
content:
application/json:
schema:
type: 'number'
minimum: 5
maximum: 3
application/xml:
schema:
$ref: '#/components/schemas/Order'
responses:
200:
description: successful operation
content:
application/xml:
schema:
$ref: '#/components/schemas/Order'
application/json:
schema:
$ref: '#/components/schemas/Order'
400:
description: Invalid ID supplied
content: {}
404:
description: Order not found
content: {}
components:
schemas:
Order:
type: object
properties:
id:
type: integer
format: int64
petId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
status:
type: string
description: Order Status
enum:
- placed
- approved
- delivered
complete:
type: boolean
default: false
xml:
name: Order
111 changes: 111 additions & 0 deletions packages/oas/tests/fixtures/wrong-schema-in-header.oas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
openapi: 3.0.1
info:
title: Swagger Petstore
description:
'This is a sample server Petstore server. You can find out more about Swagger
at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
termsOfService: http://swagger.io/terms/
contact:
email: [email protected]
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
servers:
- url: https://{instance}.swagger.io:{port}/{basePath}
variables:
instance:
default: petstore
port:
enum:
- '8443'
- '443'
default: '8443'
basePath:
default: v2
paths:
/store/order/{orderId}:
put:
parameters:
- name: orderId
in: path
required: true
schema:
maximum: 10.0
minimum: 1.0
type: integer
format: int64
- name: exclude
in: query
required: true
schema:
type: array
items:
type: string
enum: ['petId', 'quantity', 'shipDate', 'status', 'complete']
minItems: 1
maxItems: 5
uniqueItems: true
- name: x-date
in: header
schema:
type: 'string'
format: 'date'
minLength: 11
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
application/xml:
schema:
$ref: '#/components/schemas/Order'
responses:
200:
description: successful operation
content:
application/xml:
schema:
$ref: '#/components/schemas/Order'
application/json:
schema:
$ref: '#/components/schemas/Order'
400:
description: Invalid ID supplied
content: {}
404:
description: Order not found
content: {}
components:
schemas:
Order:
type: object
properties:
id:
type: integer
format: int64
petId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
status:
type: string
description: Order Status
enum:
- placed
- approved
- delivered
complete:
type: boolean
default: false
xml:
name: Order
111 changes: 111 additions & 0 deletions packages/oas/tests/fixtures/wrong-schema-in-path.oas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
openapi: 3.0.1
info:
title: Swagger Petstore
description:
'This is a sample server Petstore server. You can find out more about Swagger
at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
termsOfService: http://swagger.io/terms/
contact:
email: [email protected]
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
externalDocs:
description: Find out more about Swagger
url: http://swagger.io
servers:
- url: https://{instance}.swagger.io:{port}/{basePath}
variables:
instance:
default: petstore
port:
enum:
- '8443'
- '443'
default: '8443'
basePath:
default: v2
paths:
/store/order/{orderId}:
put:
parameters:
- name: orderId
in: path
required: true
schema:
minimum: 42,
exclusiveMinimum: true
maximum: 43
exclusiveMaximum: true
type: integer
format: int64
- name: exclude
in: query
required: true
schema:
type: array
items:
type: string
enum: ['petId', 'quantity', 'shipDate', 'status', 'complete']
minItems: 1
maxItems: 5
uniqueItems: true
- name: api_key
in: header
schema:
type: string
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Order'
application/xml:
schema:
$ref: '#/components/schemas/Order'
responses:
200:
description: successful operation
content:
application/xml:
schema:
$ref: '#/components/schemas/Order'
application/json:
schema:
$ref: '#/components/schemas/Order'
400:
description: Invalid ID supplied
content: {}
404:
description: Order not found
content: {}
components:
schemas:
Order:
type: object
properties:
id:
type: integer
format: int64
petId:
type: integer
format: int64
quantity:
type: integer
format: int32
shipDate:
type: string
format: date-time
status:
type: string
description: Order Status
enum:
- placed
- approved
- delivered
complete:
type: boolean
default: false
xml:
name: Order
Loading

0 comments on commit 5416db2

Please sign in to comment.