Skip to content

Commit a2d1631

Browse files
authored
Merge pull request #1019 from shockey/bug/ui-2919-inherited-scheme
baseUrl improvements
2 parents 40c494e + 6c438bb commit a2d1631

File tree

3 files changed

+146
-113
lines changed

3 files changed

+146
-113
lines changed

src/execute.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,19 @@ export function queryBuilder({req, value, parameter}) {
176176
}
177177
}
178178

179+
const stripNonAlpha = str => (str ? str.replace(/\W/g, '') : null)
180+
179181
// Compose the baseUrl ( scheme + host + basePath )
180182
export function baseUrl({spec, scheme, contextUrl = ''}) {
181-
const {host, basePath, schemes = ['http']} = spec
182-
183-
const parsedUrl = url.parse(contextUrl)
183+
const parsedContextUrl = url.parse(contextUrl)
184+
const firstSchemeInSpec = Array.isArray(spec.schemes) ? spec.schemes[0] : null
184185

185-
let applyScheme = ['http', 'https'].indexOf(scheme) > -1 ? scheme : schemes[0]
186-
applyScheme = applyScheme ? `${applyScheme}:` : ''
186+
const computedScheme = scheme || firstSchemeInSpec || stripNonAlpha(parsedContextUrl.protocol) || 'http'
187+
const computedHost = spec.host || parsedContextUrl.host || ''
188+
const computedPath = spec.basePath || ''
187189

188-
if (host || basePath || contextUrl) {
189-
let res = `${applyScheme}//${host || parsedUrl.host || ''}${basePath || ''}`
190+
if (computedScheme && computedHost) {
191+
const res = `${computedScheme}://${computedHost + computedPath}`
190192

191193
// If last character is '/', trim it off
192194
return res[res.length - 1] === '/' ? res.slice(0, -1) : res

test/execute-baseurl.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import expect, {createSpy, spyOn} from 'expect'
2+
import xmock from 'xmock'
3+
import {execute, buildRequest, baseUrl, applySecurities, self as stubs} from '../src/execute'
4+
5+
// Supported shape... { spec, operationId, parameters, securities, fetch }
6+
// One can use operationId or pathItem + method
7+
8+
describe('baseUrl', () => {
9+
10+
describe('with contextUrl', () => {
11+
12+
})
13+
14+
it('should calculate a valid baseUrl given host, basePath, context, schemes', () => {
15+
const res = baseUrl({
16+
spec: {
17+
schemes: ['https'],
18+
host: 'foo.com:8080',
19+
basePath: '/bar'
20+
},
21+
contextUrl: 'http://example.com:9090/hello/swagger.json'
22+
})
23+
24+
expect(res).toEqual('https://foo.com:8080/bar')
25+
})
26+
27+
it('should calculate a valid baseUrl given host, basePath, context', () => {
28+
const res = baseUrl({
29+
spec: {
30+
host: 'foo.com:8080',
31+
basePath: '/bar'
32+
},
33+
contextUrl: 'http://example.com:9090/hello/swagger.json'
34+
})
35+
36+
expect(res).toEqual('http://foo.com:8080/bar')
37+
})
38+
39+
it('should trim the trailing slash when basePath is "/"', () => {
40+
const res = baseUrl({
41+
spec: {
42+
host: 'foo.com:8080',
43+
basePath: '/'
44+
}
45+
})
46+
47+
expect(res).toEqual('http://foo.com:8080')
48+
})
49+
50+
it('should infer the host and port based on the contextUrl', () => {
51+
const res = baseUrl({
52+
spec: {
53+
basePath: '/bar'
54+
},
55+
contextUrl: "http://example.com:9090/hello/swagger.json"
56+
})
57+
58+
expect(res).toEqual("http://example.com:9090/bar")
59+
})
60+
61+
it('should infer the entire url based on the contextUrl', () => {
62+
const res = baseUrl({
63+
spec: {},
64+
contextUrl: "http://example.com:9090/hello/swagger.json"
65+
})
66+
67+
expect(res).toEqual("http://example.com:9090")
68+
})
69+
70+
it('should infer the host based on the contextUrl', () => {
71+
const res = baseUrl({
72+
spec: {
73+
schemes: ['https'],
74+
basePath: '/bar'
75+
},
76+
contextUrl: "http://example.com:9090/hello/swagger.json"
77+
})
78+
79+
expect(res).toEqual("https://example.com:9090/bar")
80+
})
81+
82+
it('should default to an empty basePath', () => {
83+
const res = baseUrl({
84+
spec: {
85+
schemes: ['https'],
86+
host: 'foo.com:8080'
87+
},
88+
contextUrl: "http://example.com:9090/hello/swagger.json"
89+
})
90+
91+
expect(res).toEqual("https://foo.com:8080")
92+
})
93+
94+
it('should default to the correct scheme based on the spec', () => {
95+
const res = baseUrl({
96+
spec: {
97+
schemes: ['https']
98+
},
99+
contextUrl: "http://example.com:9090/hello/swagger.json"
100+
})
101+
102+
expect(res).toEqual("https://example.com:9090")
103+
})
104+
105+
it('should default to HTTP scheme based on the contextUrl', () => {
106+
const res = baseUrl({
107+
spec: {
108+
host: 'foo.com:8080'
109+
},
110+
contextUrl: "http://example.com:9090/hello/swagger.json"
111+
})
112+
113+
expect(res).toEqual("http://foo.com:8080")
114+
})
115+
116+
it('should default to HTTPS scheme based on the contextUrl', () => {
117+
const res = baseUrl({
118+
spec: {
119+
host: 'foo.com:8080'
120+
},
121+
contextUrl: "https://example.com:9090/hello/swagger.json"
122+
})
123+
124+
expect(res).toEqual("https://foo.com:8080")
125+
})
126+
127+
it('should default to HTTPS scheme and host based on the contextUrl', () => {
128+
const res = baseUrl({
129+
spec: {
130+
title: 'a spec'
131+
},
132+
contextUrl: "https://example.com:9090/hello/swagger.json"
133+
})
134+
135+
expect(res).toEqual("https://example.com:9090")
136+
})
137+
})

test/execute.js

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,110 +1423,4 @@ describe('execute', () => {
14231423
})
14241424
})
14251425
})
1426-
1427-
describe('baseUrl', () => {
1428-
let contextUrl = "http://example.com:9090/hello/swagger.json"
1429-
1430-
it('should calculate a valid baseUrl given host, basePath, context, schemes', () => {
1431-
let res = baseUrl({
1432-
spec: {
1433-
schemes: ["https"],
1434-
host: "foo.com:8080",
1435-
basePath: "/bar"
1436-
},
1437-
contextUrl
1438-
})
1439-
1440-
expect(res).toEqual("https://foo.com:8080/bar")
1441-
})
1442-
1443-
it('should calculate a valid baseUrl given host, basePath, context', () => {
1444-
let res = baseUrl({
1445-
spec: {
1446-
host: "foo.com:8080",
1447-
basePath: "/bar"
1448-
},
1449-
contextUrl
1450-
})
1451-
1452-
expect(res).toEqual("http://foo.com:8080/bar")
1453-
})
1454-
1455-
it('should trim the trailing slash when basePath is "/"', () => {
1456-
let res = baseUrl({
1457-
spec: {
1458-
host: "foo.com:8080",
1459-
basePath: "/"
1460-
}
1461-
})
1462-
1463-
expect(res).toEqual("http://foo.com:8080")
1464-
})
1465-
1466-
it('should infer the host and port based on the contextUrl', () => {
1467-
let res = baseUrl({
1468-
spec: {
1469-
basePath: "/bar"
1470-
},
1471-
contextUrl
1472-
})
1473-
1474-
expect(res).toEqual("http://example.com:9090/bar")
1475-
})
1476-
1477-
it('should infer the entire url based on the contextUrl', () => {
1478-
let res = baseUrl({
1479-
spec: {},
1480-
contextUrl
1481-
})
1482-
1483-
expect(res).toEqual("http://example.com:9090")
1484-
})
1485-
1486-
it('should infer the host based on the contextUrl', () => {
1487-
let res = baseUrl({
1488-
spec: {
1489-
schemes: ['https'],
1490-
basePath: '/bar'
1491-
},
1492-
contextUrl
1493-
})
1494-
1495-
expect(res).toEqual("https://example.com:9090/bar")
1496-
})
1497-
1498-
it('should default to an empty basePath', () => {
1499-
let res = baseUrl({
1500-
spec: {
1501-
schemes: ['https'],
1502-
host: 'foo.com:8080'
1503-
},
1504-
contextUrl
1505-
})
1506-
1507-
expect(res).toEqual("https://foo.com:8080")
1508-
})
1509-
1510-
it('should default to the correct scheme based on the spec', () => {
1511-
let res = baseUrl({
1512-
spec: {
1513-
schemes: ['https']
1514-
},
1515-
contextUrl
1516-
})
1517-
1518-
expect(res).toEqual("https://example.com:9090")
1519-
})
1520-
1521-
it('should default to the correct scheme based on the spec', () => {
1522-
let res = baseUrl({
1523-
spec: {
1524-
host: 'foo.com:8080'
1525-
},
1526-
contextUrl
1527-
})
1528-
1529-
expect(res).toEqual("http://foo.com:8080")
1530-
})
1531-
})
15321426
})

0 commit comments

Comments
 (0)