Skip to content

Commit

Permalink
More robust options handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chgeo committed Jul 12, 2021
1 parent 678f0f3 commit 3b1b116
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

- Requires `@sap/cds-dk` 4.3.0, no more polyfill for older versions

### Fixed

- Options can be provided partially

## Version 0.1.1 - 2021-07-06

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const cds = require ('@sap/cds-dk')
const swaggerUi = require('swagger-ui-express')
const express = require('express')

module.exports = (options={basePath:'/$api-docs'}) => {
module.exports = (options={}) => {
options = Object.assign({ basePath:'/$api-docs' }, options)
const router = express.Router()

cds.on ('serving', service => {
if (!isOData (service)) return
const apiPath = options.basePath+service.path
Expand Down
9 changes: 7 additions & 2 deletions tests/app/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const cds = require ('@sap/cds')
const cds_swagger = require('../..')

cds.on ('bootstrap', app => app.use(cds_swagger()) )

module.exports = cds.server

let options = {}
if (process.env.TEST_OPTIONS) {
options = JSON.parse(process.env.TEST_OPTIONS)
}

cds.on ('bootstrap', app => app.use(cds_swagger(options)) )
20 changes: 20 additions & 0 deletions tests/server-options.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
process.env.TEST_OPTIONS = JSON.stringify({basePath:'/test-base', diagram:false})

const cds = require('@sap/cds/lib')
const { GET, expect } = cds.test.in(__dirname, 'app').run('serve', 'all')

describe('with options', ()=>{
afterAll(() =>{ delete process.env.TEST_OPTIONS })

test('Main HTML', async()=>{
const { status } = await GET `/test-base/browse`
expect (status) .equal (200)
})

test('Diagram', async()=>{
const { status, data } = await GET `/test-base/browse/swagger-ui-init.js`
expect (status) .equal (200)
expect (data) .not.to.contain('yuml')
})

})
13 changes: 9 additions & 4 deletions tests/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ describe('Swagger UI', ()=>{

test('Service endpoint', async()=>{
const { status } = await GET `/browse/$metadata`
expect (status ) .equal (200)
expect (status) .equal (200)
})
test('Main HTML', async()=>{
const { status } = await GET `/$api-docs/browse`
expect (status ) .equal (200)
expect (status) .equal (200)
})
test('Diagram', async()=>{
const { status, data } = await GET `/$api-docs/browse/swagger-ui-init.js`
expect (status) .equal (200)
expect (data) .to.contain('yuml')
})
test('preview link in index.html', async()=>{
const { status,data } = await GET `/`
expect (status ) .equal (200)
expect (data). match (/Show in Swagger UI/i)
expect (status) .equal (200)
expect (data) .match (/Show in Swagger UI/i)
})

})

0 comments on commit 3b1b116

Please sign in to comment.