Skip to content

Commit e8868e8

Browse files
authored
Merge pull request #1131 from c4milo/master
Fix empty oAuth2 Bearer token added to req header
2 parents 98d836e + 639330f commit e8868e8

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/execute.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ export function applySecurities({request, securities = {}, operation = {}, spec}
419419
result.headers.authorization = `Basic ${value.base64}`
420420
}
421421
}
422-
else if (type === 'oauth2') {
422+
else if (type === 'oauth2' && accessToken) {
423423
result.headers.authorization = `${tokenType || 'Bearer'} ${accessToken}`
424424
}
425425
}

test/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,47 @@ describe('constructor', () => {
412412
})
413413
})
414414

415+
it('should not add an empty oAuth2 Bearer token header to a request', function () {
416+
const spec = {
417+
securityDefinitions: {
418+
bearer: {
419+
description: 'Bearer authorization token',
420+
type: 'oauth2',
421+
name: 'Authorization',
422+
in: 'header'
423+
}
424+
},
425+
security: [{bearer: []}],
426+
paths: {
427+
'/pet': {
428+
get: {
429+
operationId: 'getPets'
430+
}
431+
}
432+
}
433+
}
434+
435+
const authorizations = {
436+
bearer: {
437+
token: {
438+
access_token: ''
439+
}
440+
}
441+
}
442+
443+
return Swagger({spec, authorizations}).then((client) => {
444+
const http = createSpy()
445+
client.execute({http, operationId: 'getPets'})
446+
expect(http.calls.length).toEqual(1)
447+
expect(http.calls[0].arguments[0]).toEqual({
448+
headers: {},
449+
credentials: 'same-origin',
450+
method: 'GET',
451+
url: '/pet'
452+
})
453+
})
454+
})
455+
415456
it('should add global securites', function () {
416457
const spec = {
417458
securityDefinitions: {

0 commit comments

Comments
 (0)