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
2 changes: 1 addition & 1 deletion src/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export function applySecurities({request, securities = {}, operation = {}, spec}
result.headers.authorization = `Basic ${value.base64}`
}
}
else if (type === 'oauth2') {
else if (type === 'oauth2' && accessToken) {
result.headers.authorization = `${tokenType || 'Bearer'} ${accessToken}`
}
}
Expand Down
41 changes: 41 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,47 @@ describe('constructor', () => {
})
})

it('should not add an empty oAuth2 Bearer token header to a request', function () {
const spec = {
securityDefinitions: {
bearer: {
description: 'Bearer authorization token',
type: 'oauth2',
name: 'Authorization',
in: 'header'
}
},
security: [{bearer: []}],
paths: {
'/pet': {
get: {
operationId: 'getPets'
}
}
}
}

const authorizations = {
bearer: {
token: {
access_token: ''
}
}
}

return Swagger({spec, authorizations}).then((client) => {
const http = createSpy()
client.execute({http, operationId: 'getPets'})
expect(http.calls.length).toEqual(1)
expect(http.calls[0].arguments[0]).toEqual({
headers: {},
credentials: 'same-origin',
method: 'GET',
url: '/pet'
})
})
})

it('should add global securites', function () {
const spec = {
securityDefinitions: {
Expand Down