diff --git a/src/execute.js b/src/execute.js index 49fd6b723..6fb256379 100644 --- a/src/execute.js +++ b/src/execute.js @@ -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' @@ -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 @@ -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 '' diff --git a/test/execute.js b/test/execute.js index 7a6fd37c1..fcffe5ba9 100644 --- a/test/execute.js +++ b/test/execute.js @@ -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 @@ -1334,4 +1334,99 @@ describe('execute', () => { }) }) }) -}) \ No newline at end of file + + 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") + }) + }) +})