Skip to content
18 changes: 17 additions & 1 deletion src/commands/runtime/api/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,23 @@ class ApiList extends RuntimeBaseCommand {
const result = await ow.routes.list(options)

if (shouldOutputJson) {
Comment thread
shazron marked this conversation as resolved.
this.logJSON('', result.apis[0].value.apidoc)
const api = result.apis && result.apis[0]
if (!api || !api.value) {
this.logJSON('', {})
return
}
const apidoc = api.value.apidoc
const gwApiUrl = api.value.gwApiUrl
Object.keys(apidoc.paths).forEach(path => {
if (!path.startsWith('/')) return
Object.keys(apidoc.paths[path]).forEach(verb => {
const operation = apidoc.paths[path][verb]
if (operation['x-openwhisk']) {
operation['x-openwhisk'].url = `${gwApiUrl}${path}`
}
})
})
this.logJSON('', apidoc)
return
}

Expand Down
65 changes: 64 additions & 1 deletion test/commands/runtime/api/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,70 @@ describe('instance methods', () => {
const output = stdout.output.trim()
const jsonMatch = output.match(/\{[\s\S]*\}$/)
const jsonOutput = jsonMatch ? jsonMatch[0] : output
expect(JSON.parse(jsonOutput)).toMatchObject(expectedJson.apis[0].value.apidoc)
const parsed = JSON.parse(jsonOutput)
const { apis } = expectedJson
const gwApiUrl = apis[0].value.gwApiUrl
// apidoc structure preserved
expect(parsed.basePath).toEqual(apis[0].value.apidoc.basePath)
expect(parsed.info).toMatchObject(apis[0].value.apidoc.info)
expect(parsed.swagger).toEqual(apis[0].value.apidoc.swagger)
// x-openwhisk.url updated with actual gateway URL (was "not-used")
expect(parsed.paths['/mypath'].get['x-openwhisk'].url).toEqual(`${gwApiUrl}/mypath`)
})
.finally(() => {
stdout.stop()
})
})

test('--json flag, empty apis array returns empty object', () => {
rtLib.mockResolved(rtAction, { apis: [] })
stdout.stop()
stdout.start()
const cmd = new TheCommand(['--json'])
return cmd.run()
.then(() => {
const output = stdout.output.trim()
const jsonMatch = output.match(/\{[\s\S]*\}$/)
const parsed = JSON.parse(jsonMatch ? jsonMatch[0] : output)
expect(parsed).toEqual({})
})
.finally(() => {
stdout.stop()
})
})

test('--json flag, operation without x-openwhisk leaves url unchanged', () => {
rtLib.mockResolved(rtAction, {
apis: [{
value: {
gwApiUrl: 'https://example.com/api',
apidoc: {
basePath: '/test',
info: { title: 'test', version: '1.0.0' },
swagger: '2.0',
paths: {
'/mypath': {
get: {
operationId: 'testOp',
responses: { default: { description: 'Default response' } }
// no x-openwhisk field
}
}
}
}
}
}]
})
stdout.stop()
stdout.start()
const cmd = new TheCommand(['--json'])
return cmd.run()
.then(() => {
const output = stdout.output.trim()
const jsonMatch = output.match(/\{[\s\S]*\}$/)
const parsed = JSON.parse(jsonMatch ? jsonMatch[0] : output)
// operation without x-openwhisk should be left intact
expect(parsed.paths['/mypath'].get).not.toHaveProperty('x-openwhisk')
})
.finally(() => {
stdout.stop()
Expand Down
Loading