xmock().get('http://example.com/remote.json', function (req, res, next) { xmock().restore() return res.send({ baz: { $ref: '#/remoteOther' }, remoteOther: { result: 'it works!' } }) })
it('should always load a spec as text', () => { xapp = xmock() xapp.get('http://swagger.io/somespec', (req, res) => { res.set('Content-Type', 'application/octet-stream') res.send('key: val') }) return http({url: 'http://swagger.io/somespec', loadSpec: true}).then((res) => { expect(res.status).toEqual(200) expect(res.text).toEqual('key: val') }) })
it('should be able to GET a url', () => { xapp = xmock() xapp.get('http://swagger.io', (req, res) => res.send('hi')) return http({ url: 'http://swagger.io' }) .then((res) => { expect(res.status).toEqual(200) expect(res.text).toEqual('hi') }) })
beforeEach(() => { Swagger.clearCache() const xapp = xmock() xapp .get('http://petstore.swagger.io/v2/swagger.json', () => require('./data/petstore.json')) .get('http://petstore.swagger.io/v2/pet/3', () => ({id: 3})) .get('http://petstore.swagger.io/v2/pet/4', () => ({id: 4})) .get('http://petstore.swagger.io/v2/ref.json', () => ({b: 2})) .get('http://petstore.swagger.io/v2/base.json', () => ( { $ref: 'http://petstore.swagger.io/v2/ref.json#b' } )) })
it('should throw if fetch error', function (cb) { const xapp = xmock() xapp.get('http://petstore.swagger.io/404', (req, res) => { res.status(404) res.send('not found') }) new Swagger({url: 'http://petstore.swagger.io/404'}) .catch((err) => { expect(err.status).toBe(404) expect(err.message).toBe('Not Found') cb() }) })
it('should handle invalid JSON bodies', function () { // Given const xapp = xmock().get('https://swagger.io/one', function (req, res, next) { return res.send('[') }) const req = { url: 'https://swagger.io/one' } return Swagger.http(req) .then((res) => { const {body, text, status} = res expect(status).toEqual(200) expect(text).toEqual('[') expect(body).toEqual() }) })
it('should include status code and response with HTTP Error', () => { xapp = xmock() xapp.get('http://swagger.io', (req, res) => res.status(400).send('hi')) return http({ url: 'http://swagger.io' }) .then( (res) => { throw new Error('Expected rejection for HTTP status 400') }, (err) => { expect(err.status).toEqual(400) expect(err.statusCode).toEqual(400) expect(err.response.text).toEqual('hi') } ) })
test('should fully resolve across remote documents correctly', async () => { const input = { foo: { bar: { $ref: './remote.json' } } } xmock().get('http://example.com/remote.json', function (req, res, next) { xmock().restore() return res.send({ baz: { $ref: '#/remoteOther' }, remoteOther: { result: 'it works!' } }) }) const res = await resolve(input, [], { baseDoc: 'http://example.com/main.json' }) expect(res).toEqual({ errors: [], spec: { foo: { bar: { $$ref: './remote.json', baz: { $$ref: '#/remoteOther', result: 'it works!' }, remoteOther: { result: 'it works!' } } } } }) })
it('should apply responseInterceptor to error responses', () => { xapp = xmock() xapp.get('http://swagger.io', (req, res) => res.status(400).send('hi')) return http({ url: 'http://swagger.io', responseInterceptor: (res) => { res.testValue = 5 } }) .then( (res) => { throw new Error('Expected rejection for HTTP status 400') }, (err) => { expect(err.response.testValue).toEqual(5) } ) })
it('should set responseError on responseInterceptor Error', () => { xapp = xmock() xapp.get('http://swagger.io', (req, res) => res.status(400).send('hi')) const testError = new Error() return http({ url: 'http://swagger.io', responseInterceptor: (res) => { throw testError } }) .then( (res) => { throw new Error('Expected rejection for HTTP status 400') }, (err) => { expect(err.response).toEqual(null) expect(err.responseError).toBe(testError) } ) })
it('should serialize the response', function () { // Given require('cross-fetch/polyfill') // To ensure global.Headers const xapp = xmock().get('https://swagger.io/one', function (req, res, next) { res.set('hi', 'ho') return res.send({me: true}) }) const req = { url: 'https://swagger.io/one', } return Swagger.http(req) .then((res) => { expect(res).toInclude({ url: req.url, ok: true, status: 200, headers: { connection: 'close', 'content-type': 'application/json', hi: 'ho' }, statusText: 'OK', data: '{"me":true}', text: '{"me":true}', body: { me: true }, obj: { me: true }, }) }) })
test.only('should redirect and resolve nested remote document requests', async () => { const input = { foo: { bar: { $ref: './fake-remote.json' } } } const requestInterceptor = createSpy((req) => { req.headers.authorization = 'wow' if (req.url === 'http://example.com/fake-remote.json') { req.url = 'http://example.com/remote.json' } if (req.url === 'http://example.com/fake-nested.json') { req.url = 'http://example.com/nested.json' } return req }).andCallThrough() xmock() .get('http://example.com/remote.json', function (req, res, next) { if (req.header.authorization !== 'wow') { res.status(403) } res.send({ baz: { $ref: '#/remoteOther' }, remoteOther: { $ref: './fake-nested.json' } }) }) .get('http://example.com/nested.json', function (req, res, next) { if (req.header.authorization !== 'wow') { res.status(403) } res.send({ result: 'it works!' }) }) const res = await resolve(input, [], { baseDoc: 'http://example.com/main.json', requestInterceptor }) expect(requestInterceptor.calls.length).toEqual(2) expect(requestInterceptor.calls[0].arguments[0].url).toEqual('http://example.com/remote.json') expect(requestInterceptor.calls[1].arguments[0].url).toEqual('http://example.com/nested.json') expect(res).toEqual({ errors: [], spec: { foo: { bar: { $$ref: './fake-remote.json', baz: { $$ref: './fake-nested.json', result: 'it works!' }, remoteOther: { $$ref: './fake-nested.json', result: 'it works!' } } } } }) })
beforeAll(() => { xapp = xmock() })
afterEach(function () { expect.restoreSpies() xmock().restore() })
afterEach(() => { xmock().restore() })