Exemplo n.º 1
0
 xmock().get('http://example.com/remote.json', function (req, res, next) {
   xmock().restore()
   return res.send({
     baz: {
       $ref: '#/remoteOther'
     },
     remoteOther: {
       result: 'it works!'
     }
   })
 })
Exemplo n.º 2
0
  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')
    })
  })
Exemplo n.º 3
0
  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')
    })
  })
Exemplo n.º 4
0
 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'
       }
     ))
 })
Exemplo n.º 5
0
    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()
        })
    })
Exemplo n.º 6
0
    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()
        })
    })
Exemplo n.º 7
0
  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')
      }
    )
  })
Exemplo n.º 8
0
  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!'
            }
          }
        }
      }
    })
  })
Exemplo n.º 9
0
  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)
      }
    )
  })
Exemplo n.º 10
0
  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)
      }
    )
  })
Exemplo n.º 11
0
    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
            },
          })
        })
    })
Exemplo n.º 12
0
  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!'
            }
          }
        }
      }
    })
  })
Exemplo n.º 13
0
 beforeAll(() => {
   xapp = xmock()
 })
Exemplo n.º 14
0
 afterEach(function () {
   expect.restoreSpies()
   xmock().restore()
 })
Exemplo n.º 15
0
 afterEach(() => {
   xmock().restore()
 })