Skip to content

Commit 94a0fc6

Browse files
authored
Merge pull request #978 from shockey/ft/baseurl
Extend baseUrl and add tests accordingly
2 parents 01adb6a + adf0d31 commit 94a0fc6

File tree

2 files changed

+106
-8
lines changed

2 files changed

+106
-8
lines changed

src/execute.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assign from 'lodash/assign'
22
import getIn from 'lodash/get'
33
import btoa from 'btoa'
4+
import url from 'url'
45
import http, {mergeInQueryOrForm} from './http'
56
import {getOperationRaw, idFromPathMethod} from './helpers'
67

@@ -54,13 +55,13 @@ export function execute({
5455
export function buildRequest({
5556
spec, operationId, parameters, securities, requestContentType,
5657
responseContentType, parameterBuilders, scheme,
57-
requestInterceptor, responseInterceptor
58+
requestInterceptor, responseInterceptor, contextUrl
5859
}) {
5960
parameterBuilders = parameterBuilders || PARAMETER_BUILDERS
60-
61+
6162
// Base Template
6263
let req = {
63-
url: baseUrl(spec, scheme),
64+
url: baseUrl({spec, scheme, contextUrl}),
6465
headers: {
6566
// This breaks CORSs... removing this line... probably breaks oAuth. Need to address that
6667
// This also breaks tests
@@ -173,14 +174,16 @@ export function queryBuilder({req, value, parameter}) {
173174
}
174175

175176
// Compose the baseUrl ( scheme + host + basePath )
176-
export function baseUrl(spec, scheme) {
177+
export function baseUrl({spec, scheme, contextUrl = ''}) {
177178
const {host, basePath, schemes = ['http']} = spec
178179

180+
const parsedUrl = url.parse(contextUrl)
181+
179182
let applyScheme = ['http', 'https'].indexOf(scheme) > -1 ? scheme : schemes[0]
180183
applyScheme = applyScheme ? `${applyScheme}:` : ''
181184

182-
if (host || basePath) {
183-
return `${applyScheme}//${host || ''}${basePath || ''}`
185+
if (host || basePath || contextUrl) {
186+
return `${applyScheme}//${host || parsedUrl.host || ''}${basePath || ''}`
184187
}
185188

186189
return ''

test/execute.js

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import expect, {createSpy, spyOn} from 'expect'
22
import xmock from 'xmock'
3-
import {execute, buildRequest, applySecurities, self as stubs} from '../src/execute'
3+
import {execute, buildRequest, baseUrl, applySecurities, self as stubs} from '../src/execute'
44

55
// Supported shape... { spec, operationId, parameters, securities, fetch }
66
// One can use operationId or pathItem + method
@@ -1334,4 +1334,99 @@ describe('execute', () => {
13341334
})
13351335
})
13361336
})
1337-
})
1337+
1338+
describe('baseUrl', () => {
1339+
let contextUrl = "http://example.com:9090/hello/swagger.json"
1340+
1341+
it('should calculate a valid baseUrl given host, basePath, context, schemes', () => {
1342+
let res = baseUrl({
1343+
spec: {
1344+
schemes: ["https"],
1345+
host: "foo.com:8080",
1346+
basePath: "/bar"
1347+
},
1348+
contextUrl
1349+
})
1350+
1351+
expect(res).toEqual("https://foo.com:8080/bar")
1352+
})
1353+
1354+
it('should calculate a valid baseUrl given host, basePath, context', () => {
1355+
let res = baseUrl({
1356+
spec: {
1357+
host: "foo.com:8080",
1358+
basePath: "/bar"
1359+
},
1360+
contextUrl
1361+
})
1362+
1363+
expect(res).toEqual("http://foo.com:8080/bar")
1364+
})
1365+
1366+
it('should infer the host and port based on the contextUrl', () => {
1367+
let res = baseUrl({
1368+
spec: {
1369+
basePath: "/bar"
1370+
},
1371+
contextUrl
1372+
})
1373+
1374+
expect(res).toEqual("http://example.com:9090/bar")
1375+
})
1376+
1377+
it('should infer the entire url based on the contextUrl', () => {
1378+
let res = baseUrl({
1379+
spec: {},
1380+
contextUrl
1381+
})
1382+
1383+
expect(res).toEqual("http://example.com:9090")
1384+
})
1385+
1386+
it('should infer the host based on the contextUrl', () => {
1387+
let res = baseUrl({
1388+
spec: {
1389+
schemes: ['https'],
1390+
basePath: '/bar'
1391+
},
1392+
contextUrl
1393+
})
1394+
1395+
expect(res).toEqual("https://example.com:9090/bar")
1396+
})
1397+
1398+
it('should default to an empty basePath', () => {
1399+
let res = baseUrl({
1400+
spec: {
1401+
schemes: ['https'],
1402+
host: 'foo.com:8080'
1403+
},
1404+
contextUrl
1405+
})
1406+
1407+
expect(res).toEqual("https://foo.com:8080")
1408+
})
1409+
1410+
it('should default to the correct scheme based on the spec', () => {
1411+
let res = baseUrl({
1412+
spec: {
1413+
schemes: ['https']
1414+
},
1415+
contextUrl
1416+
})
1417+
1418+
expect(res).toEqual("https://example.com:9090")
1419+
})
1420+
1421+
it('should default to the correct scheme based on the spec', () => {
1422+
let res = baseUrl({
1423+
spec: {
1424+
host: 'foo.com:8080'
1425+
},
1426+
contextUrl
1427+
})
1428+
1429+
expect(res).toEqual("http://foo.com:8080")
1430+
})
1431+
})
1432+
})

0 commit comments

Comments
 (0)