@@ -4,6 +4,7 @@ import isPlainObject from 'lodash/isPlainObject'
44import isArray from 'lodash/isArray'
55import btoa from 'btoa'
66import url from 'url'
7+ import cookie from 'cookie'
78import http , { mergeInQueryOrForm } from '../http'
89import createError from '../specmap/lib/create-error'
910
@@ -109,7 +110,8 @@ export function buildRequest(options) {
109110 // This breaks CORSs... removing this line... probably breaks oAuth. Need to address that
110111 // This also breaks tests
111112 // 'access-control-allow-origin': '*'
112- }
113+ } ,
114+ cookies : { }
113115 }
114116
115117 if ( requestInterceptor ) {
@@ -124,6 +126,11 @@ export function buildRequest(options) {
124126
125127 // Mostly for testing
126128 if ( ! operationId ) {
129+ // Not removing req.cookies causes testing issues and would
130+ // change our interface, so we're always sure to remove it.
131+ // See the same statement lower down in this function for
132+ // more context.
133+ delete req . cookies
127134 return req
128135 }
129136
@@ -198,6 +205,26 @@ export function buildRequest(options) {
198205 req = swagger2BuildRequest ( versionSpecificOptions , req )
199206 }
200207
208+
209+ // If the cookie convenience object exists in our request,
210+ // serialize its content and then delete the cookie object.
211+ if ( req . cookies && Object . keys ( req . cookies ) . length ) {
212+ const cookieString = Object . keys ( req . cookies ) . reduce ( ( prev , cookieName ) => {
213+ const cookieValue = req . cookies [ cookieName ]
214+ const prefix = prev ? '&' : ''
215+ const stringified = cookie . serialize ( cookieName , cookieValue )
216+ return prev + prefix + stringified
217+ } , '' )
218+ req . headers . Cookie = cookieString
219+ }
220+
221+ if ( req . cookies ) {
222+ // even if no cookies were defined, we need to remove
223+ // the cookies key from our request, or many many legacy
224+ // tests will break.
225+ delete req . cookies
226+ }
227+
201228 // Will add the query object into the URL, if it exists
202229 // ... will also create a FormData instance, if multipart/form-data (eg: a file)
203230 mergeInQueryOrForm ( req )
@@ -219,7 +246,7 @@ function oas3BaseUrl({spec, server, contextUrl, serverVariables = {}}) {
219246 let selectedServerUrl = ''
220247 let selectedServerObj = null
221248
222- if ( server ) {
249+ if ( server && servers ) {
223250 const serverUrls = servers . map ( srv => srv . url )
224251
225252 if ( serverUrls . indexOf ( server ) > - 1 ) {
@@ -259,15 +286,18 @@ function buildOas3UrlWithContext(ourUrl = '', contextUrl = '') {
259286 const computedScheme = stripNonAlpha ( parsedUrl . protocol ) || stripNonAlpha ( parsedContextUrl . protocol ) || ''
260287 const computedHost = parsedUrl . host || parsedContextUrl . host
261288 const computedPath = parsedUrl . pathname || ''
289+ let res
262290
263291 if ( computedScheme && computedHost ) {
264- const res = `${ computedScheme } ://${ computedHost + computedPath } `
292+ res = `${ computedScheme } ://${ computedHost + computedPath } `
265293
266294 // If last character is '/', trim it off
267- return res [ res . length - 1 ] === '/' ? res . slice ( 0 , - 1 ) : res
295+ }
296+ else {
297+ res = computedPath
268298 }
269299
270- return ''
300+ return res [ res . length - 1 ] === '/' ? res . slice ( 0 , - 1 ) : res
271301}
272302
273303function getVariableTemplateNames ( str ) {
@@ -290,13 +320,17 @@ function swagger2BaseUrl({spec, scheme, contextUrl = ''}) {
290320 const computedScheme = scheme || firstSchemeInSpec || stripNonAlpha ( parsedContextUrl . protocol ) || 'http'
291321 const computedHost = spec . host || parsedContextUrl . host || ''
292322 const computedPath = spec . basePath || ''
323+ let res
293324
294325 if ( computedScheme && computedHost ) {
295- const res = `${ computedScheme } ://${ computedHost + computedPath } `
296-
297- // If last character is '/', trim it off
298- return res [ res . length - 1 ] === '/' ? res . slice ( 0 , - 1 ) : res
326+ // we have what we need for an absolute URL
327+ res = `${ computedScheme } ://${ computedHost + computedPath } `
328+ }
329+ else {
330+ // if not, a relative URL will have to do
331+ res = computedPath
299332 }
300333
301- return ''
334+ // If last character is '/', trim it off
335+ return res [ res . length - 1 ] === '/' ? res . slice ( 0 , - 1 ) : res
302336}
0 commit comments