Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/execute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assign from 'lodash/assign'
import getIn from 'lodash/get'
import btoa from 'btoa'
import url from 'url'
import http, {mergeInQueryOrForm} from './http'
import {getOperationRaw, idFromPathMethod} from './helpers'

Expand Down Expand Up @@ -54,13 +55,13 @@ export function execute({
export function buildRequest({
spec, operationId, parameters, securities, requestContentType,
responseContentType, parameterBuilders, scheme,
requestInterceptor, responseInterceptor
requestInterceptor, responseInterceptor, contextUrl
}) {
parameterBuilders = parameterBuilders || PARAMETER_BUILDERS

// Base Template
let req = {
url: baseUrl(spec, scheme),
url: baseUrl({spec, scheme, contextUrl}),
headers: {
// This breaks CORSs... removing this line... probably breaks oAuth. Need to address that
// This also breaks tests
Expand Down Expand Up @@ -173,14 +174,16 @@ export function queryBuilder({req, value, parameter}) {
}

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

const parsedUrl = url.parse(contextUrl)

let applyScheme = ['http', 'https'].indexOf(scheme) > -1 ? scheme : schemes[0]
applyScheme = applyScheme ? `${applyScheme}:` : ''

if (host || basePath) {
return `${applyScheme}//${host || ''}${basePath || ''}`
if (host || basePath || contextUrl) {
return `${applyScheme}//${host || parsedUrl.host || ''}${basePath || ''}`
}

return ''
Expand Down
99 changes: 97 additions & 2 deletions test/execute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import expect, {createSpy, spyOn} from 'expect'
import xmock from 'xmock'
import {execute, buildRequest, applySecurities, self as stubs} from '../src/execute'
import {execute, buildRequest, baseUrl, applySecurities, self as stubs} from '../src/execute'

// Supported shape... { spec, operationId, parameters, securities, fetch }
// One can use operationId or pathItem + method
Expand Down Expand Up @@ -1334,4 +1334,99 @@ describe('execute', () => {
})
})
})
})

describe('baseUrl', () => {
let contextUrl = "http://example.com:9090/hello/swagger.json"

it('should calculate a valid baseUrl given host, basePath, context, schemes', () => {
let res = baseUrl({
spec: {
schemes: ["https"],
host: "foo.com:8080",
basePath: "/bar"
},
contextUrl
})

expect(res).toEqual("https://foo.com:8080/bar")
})

it('should calculate a valid baseUrl given host, basePath, context', () => {
let res = baseUrl({
spec: {
host: "foo.com:8080",
basePath: "/bar"
},
contextUrl
})

expect(res).toEqual("http://foo.com:8080/bar")
})

it('should infer the host and port based on the contextUrl', () => {
let res = baseUrl({
spec: {
basePath: "/bar"
},
contextUrl
})

expect(res).toEqual("http://example.com:9090/bar")
})

it('should infer the entire url based on the contextUrl', () => {
let res = baseUrl({
spec: {},
contextUrl
})

expect(res).toEqual("http://example.com:9090")
})

it('should infer the host based on the contextUrl', () => {
let res = baseUrl({
spec: {
schemes: ['https'],
basePath: '/bar'
},
contextUrl
})

expect(res).toEqual("https://example.com:9090/bar")
})

it('should default to an empty basePath', () => {
let res = baseUrl({
spec: {
schemes: ['https'],
host: 'foo.com:8080'
},
contextUrl
})

expect(res).toEqual("https://foo.com:8080")
})

it('should default to the correct scheme based on the spec', () => {
let res = baseUrl({
spec: {
schemes: ['https']
},
contextUrl
})

expect(res).toEqual("https://example.com:9090")
})

it('should default to the correct scheme based on the spec', () => {
let res = baseUrl({
spec: {
host: 'foo.com:8080'
},
contextUrl
})

expect(res).toEqual("http://foo.com:8080")
})
})
})