Skip to content

Commit 0663b35

Browse files
committed
More OAS tests and helpers!
1 parent 3f5ad91 commit 0663b35

File tree

7 files changed

+673
-100
lines changed

7 files changed

+673
-100
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"watch": "webpack --config webpack.config.js --watch --progress",
2222
"test": "npm run just-test && npm run lint",
2323
"just-test": "NODE_ENV=test node ./node_modules/.bin/_mocha --recursive --compilers js:babel-core/register",
24-
"test:watch": "npm run test -- -w",
24+
"test:watch": "npm run just-test -- -w",
2525
"lint": "eslint src/ test/",
2626
"deps-license": "license-checker --production --csv --out $npm_package_config_deps_check_dir/licenses.csv && license-checker --development --csv --out $npm_package_config_deps_check_dir/licenses-dev.csv",
2727
"deps-size": "webpack -p --config webpack.check.js --json | webpack-bundle-size-analyzer >| $npm_package_config_deps_check_dir/sizes.txt",

src/helpers.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ const escapeString = (str) => {
55
return str.replace(/[^\w]/gi, '_')
66
}
77

8+
// Spec version detection
9+
export function isOAS3(spec) {
10+
const oasVersion = spec.openapi
11+
if (!oasVersion) {
12+
return false
13+
}
14+
15+
return oasVersion.startsWith('3.0.0')
16+
}
17+
18+
export function isSwagger2(spec) {
19+
const swaggerVersion = spec.swagger
20+
if (!swaggerVersion) {
21+
return false
22+
}
23+
24+
return swaggerVersion.startsWith('2')
25+
}
26+
827
// Strategy for determining operationId
928
export function opId(operation, pathName, method = '') {
1029
const idWithoutWhitespace = (operation.operationId || '').replace(/\s/g, '')

test/execute.js

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,9 @@ describe('execute', () => {
803803
})
804804
})
805805

806-
it('should NOT stringify the body, if provided with a javascript object (execute alone should do that, allowing us to modify the object in a clean way)', function () {
806+
it('should NOT stringify the body, if provided with a javascript object', function () {
807+
// execute alone should do that, allowing us to modify the object in a clean way)
808+
807809
// Given
808810
const spec = {
809811
host: 'swagger.io',
@@ -825,63 +827,6 @@ describe('execute', () => {
825827
})
826828
})
827829

828-
describe.only("buildRequest for OpenAPI 3.0", function () {
829-
it('should build a request for the given operationId', function () {
830-
// Given
831-
const spec = {
832-
openapi: "3.0.0",
833-
paths: {
834-
'/one': {
835-
get: {
836-
operationId: 'getMe'
837-
}
838-
}
839-
}
840-
}
841-
842-
// when
843-
const req = buildRequest({spec, operationId: 'getMe'})
844-
845-
expect(req).toEqual({
846-
method: 'GET',
847-
url: '/one',
848-
credentials: 'same-origin',
849-
headers: {},
850-
})
851-
})
852-
853-
it('should build a request for the given operationId with a server provided', function () {
854-
// Given
855-
const spec = {
856-
openapi: '3.0.0',
857-
servers: [
858-
{
859-
url: 'http://petstore.swagger.io/v2',
860-
name: 'Petstore'
861-
}
862-
],
863-
paths: {
864-
'/one': {
865-
get: {
866-
operationId: 'getMe'
867-
}
868-
}
869-
}
870-
}
871-
872-
// when
873-
const req = buildRequest({spec, operationId: 'getMe'})
874-
875-
expect(req).toEqual({
876-
method: 'GET',
877-
url: 'http://petstore.swagger.io/v2/one',
878-
credentials: 'same-origin',
879-
headers: {},
880-
})
881-
})
882-
883-
884-
})
885830

886831
// Note: this is to handle requestContentType and responseContentType
887832
// although more might end up using it.

test/oas3/client.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import expect from 'expect'
2+
import http from 'http'
3+
import url from 'url'
4+
import path from 'path'
5+
import fs from 'fs'
6+
7+
import Swagger from '../../src/index'
8+
9+
describe('http - OpenAPI Specification 3.0', () => {
10+
let server
11+
before(function () {
12+
server = http.createServer(function (req, res) {
13+
const accept = req.headers.accept
14+
let contentType
15+
const uri = url.parse(req.url).pathname
16+
const filename = path.join('test', 'oas3', 'data', uri)
17+
18+
if (filename.indexOf('.yaml') > 0) {
19+
contentType = 'application/yaml'
20+
}
21+
else {
22+
contentType = 'application/json'
23+
}
24+
25+
if (typeof accept !== 'undefined') {
26+
if (accept === 'invalid') {
27+
res.writeHead(500)
28+
res.end()
29+
return
30+
}
31+
32+
if (accept.indexOf('application/json') >= 0) {
33+
contentType = accept
34+
res.setHeader('Content-Type', contentType)
35+
}
36+
if (filename.indexOf('.yaml') > 0) {
37+
res.setHeader('Content-Type', 'application/yaml')
38+
}
39+
}
40+
41+
fs.exists(filename, function (exists) {
42+
if (exists) {
43+
const fileStream = fs.createReadStream(filename)
44+
res.setHeader('Access-Control-Allow-Origin', '*')
45+
res.writeHead(200, contentType)
46+
fileStream.pipe(res)
47+
}
48+
else {
49+
res.writeHead(404, {'Content-Type': 'text/plain'})
50+
res.write('404 Not Found\n')
51+
res.end()
52+
}
53+
})
54+
}).listen(8000)
55+
})
56+
57+
after(function () {
58+
server.close()
59+
})
60+
61+
afterEach(function () {
62+
})
63+
64+
it('should get the petstore api and build it', (done) => {
65+
Swagger('http://localhost:8000/petstore-oas3.yaml')
66+
.then((client) => {
67+
expect(client).toExist()
68+
69+
// we have 3 tags
70+
expect(Object.keys(client.apis).length).toBe(1)
71+
72+
// the pet tag exists
73+
expect(client.apis.pets).toExist()
74+
75+
// the get pet operation
76+
expect(client.apis.pets.getPetById).toExist()
77+
78+
done()
79+
})
80+
.catch(e => done (e))
81+
})
82+
83+
/**
84+
* See https://github.com/swagger-api/swagger-js/issues/1005
85+
*/
86+
it('should get a pet from the petstore', (done) => {
87+
Swagger('http://localhost:8000/petstore-oas3.yaml')
88+
.then((client) => {
89+
client.apis.pets.getPetById({petId: -1})
90+
.then((data) => {
91+
done()
92+
})
93+
.catch((err) => {
94+
done(err)
95+
})
96+
})
97+
.catch(e => done (e))
98+
})
99+
100+
/**
101+
* See https://github.com/swagger-api/swagger-js/issues/1002
102+
*/
103+
it('should return an error when a spec doesnt exist', (done) => {
104+
Swagger('http://localhost:8000/not-real.yaml')
105+
.then((client) => {
106+
done('expected an error')
107+
})
108+
.catch((error) => {
109+
done()
110+
})
111+
})
112+
})

0 commit comments

Comments
 (0)