Skip to content

Commit d64efd9

Browse files
committed
Merge remote-tracking branch 'upstream/master' into issue-995
2 parents 8bfaa54 + 7558cad commit d64efd9

File tree

4 files changed

+147
-114
lines changed

4 files changed

+147
-114
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-client",
3-
"version": "3.0.5",
3+
"version": "3.0.6",
44
"description": "SwaggerJS - a collection of interfaces for OAI specs",
55
"main": "dist/index.js",
66
"contributors": [

src/execute.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,19 @@ export function queryBuilder({req, value, parameter}) {
186186
}
187187
}
188188

189+
const stripNonAlpha = str => (str ? str.replace(/\W/g, '') : null)
190+
189191
// Compose the baseUrl ( scheme + host + basePath )
190192
export function baseUrl({spec, scheme, contextUrl = ''}) {
191-
const {host, basePath, schemes = ['http']} = spec
192-
193-
const parsedUrl = url.parse(contextUrl)
193+
const parsedContextUrl = url.parse(contextUrl)
194+
const firstSchemeInSpec = Array.isArray(spec.schemes) ? spec.schemes[0] : null
194195

195-
let applyScheme = ['http', 'https'].indexOf(scheme) > -1 ? scheme : schemes[0]
196-
applyScheme = applyScheme ? `${applyScheme}:` : ''
196+
const computedScheme = scheme || firstSchemeInSpec || stripNonAlpha(parsedContextUrl.protocol) || 'http'
197+
const computedHost = spec.host || parsedContextUrl.host || ''
198+
const computedPath = spec.basePath || ''
197199

198-
if (host || basePath || contextUrl) {
199-
let res = `${applyScheme}//${host || parsedUrl.host || ''}${basePath || ''}`
200+
if (computedScheme && computedHost) {
201+
const res = `${computedScheme}://${computedHost + computedPath}`
200202

201203
// If last character is '/', trim it off
202204
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
@@ -1552,110 +1552,4 @@ describe('execute', () => {
15521552
})
15531553
})
15541554
})
1555-
1556-
describe('baseUrl', () => {
1557-
let contextUrl = "http://example.com:9090/hello/swagger.json"
1558-
1559-
it('should calculate a valid baseUrl given host, basePath, context, schemes', () => {
1560-
let res = baseUrl({
1561-
spec: {
1562-
schemes: ["https"],
1563-
host: "foo.com:8080",
1564-
basePath: "/bar"
1565-
},
1566-
contextUrl
1567-
})
1568-
1569-
expect(res).toEqual("https://foo.com:8080/bar")
1570-
})
1571-
1572-
it('should calculate a valid baseUrl given host, basePath, context', () => {
1573-
let res = baseUrl({
1574-
spec: {
1575-
host: "foo.com:8080",
1576-
basePath: "/bar"
1577-
},
1578-
contextUrl
1579-
})
1580-
1581-
expect(res).toEqual("http://foo.com:8080/bar")
1582-
})
1583-
1584-
it('should trim the trailing slash when basePath is "/"', () => {
1585-
let res = baseUrl({
1586-
spec: {
1587-
host: "foo.com:8080",
1588-
basePath: "/"
1589-
}
1590-
})
1591-
1592-
expect(res).toEqual("http://foo.com:8080")
1593-
})
1594-
1595-
it('should infer the host and port based on the contextUrl', () => {
1596-
let res = baseUrl({
1597-
spec: {
1598-
basePath: "/bar"
1599-
},
1600-
contextUrl
1601-
})
1602-
1603-
expect(res).toEqual("http://example.com:9090/bar")
1604-
})
1605-
1606-
it('should infer the entire url based on the contextUrl', () => {
1607-
let res = baseUrl({
1608-
spec: {},
1609-
contextUrl
1610-
})
1611-
1612-
expect(res).toEqual("http://example.com:9090")
1613-
})
1614-
1615-
it('should infer the host based on the contextUrl', () => {
1616-
let res = baseUrl({
1617-
spec: {
1618-
schemes: ['https'],
1619-
basePath: '/bar'
1620-
},
1621-
contextUrl
1622-
})
1623-
1624-
expect(res).toEqual("https://example.com:9090/bar")
1625-
})
1626-
1627-
it('should default to an empty basePath', () => {
1628-
let res = baseUrl({
1629-
spec: {
1630-
schemes: ['https'],
1631-
host: 'foo.com:8080'
1632-
},
1633-
contextUrl
1634-
})
1635-
1636-
expect(res).toEqual("https://foo.com:8080")
1637-
})
1638-
1639-
it('should default to the correct scheme based on the spec', () => {
1640-
let res = baseUrl({
1641-
spec: {
1642-
schemes: ['https']
1643-
},
1644-
contextUrl
1645-
})
1646-
1647-
expect(res).toEqual("https://example.com:9090")
1648-
})
1649-
1650-
it('should default to the correct scheme based on the spec', () => {
1651-
let res = baseUrl({
1652-
spec: {
1653-
host: 'foo.com:8080'
1654-
},
1655-
contextUrl
1656-
})
1657-
1658-
expect(res).toEqual("http://foo.com:8080")
1659-
})
1660-
})
16611555
})

0 commit comments

Comments
 (0)