Skip to content

Commit

Permalink
Merge pull request #780 from apiaryio/honzajavorek/remove-trailing-sl…
Browse files Browse the repository at this point in the history
…ash-from-apiary-api-url

Remove trailing slash from Apiary API URL
  • Loading branch information
honzajavorek committed May 12, 2017
2 parents 711cbcf + 5013840 commit 67d5660
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/reporters/apiary-reporter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ApiaryReporter
@errors = []
@serverError = false
@configuration =
apiUrl: @_get 'apiaryApiUrl', 'APIARY_API_URL', 'https://api.apiary.io'
apiUrl: (@_get 'apiaryApiUrl', 'APIARY_API_URL', 'https://api.apiary.io').replace(/\/$/, '')
apiToken: @_get 'apiaryApiKey', 'APIARY_API_KEY', null
apiSuite: @_get 'apiaryApiName', 'APIARY_API_NAME', null
logger.verbose("Using '#{@type}' reporter.")
Expand Down Expand Up @@ -202,7 +202,7 @@ class ApiaryReporter
logger.debug('Apiary reporter response:', JSON.stringify(info, null, 2))
return callback(null, res, parsedBody)

body = JSON.stringify(reqBody)
body = if reqBody then JSON.stringify(reqBody) else ''
system = os.type() + ' ' + os.release() + '; ' + os.arch()
headers =
'User-Agent': "Dredd Apiary Reporter/#{packageData.version} (#{system})"
Expand Down
60 changes: 60 additions & 0 deletions test/unit/reporters/apiary-reporter-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,67 @@ describe 'ApiaryReporter', () ->
nock.cleanAll()
done()

describe 'constructor', ->
describe 'when custom settings contain API URL without trailing slash', ->
custom =
apiaryReporterEnv: env
apiaryApiUrl: 'https://api.example.com:1234'

it 'uses the provided API URL in configuration', ->
emitter = new EventEmitter
apiaryReporter = new ApiaryReporter emitter, {}, {}, {custom}
assert.equal(
apiaryReporter.configuration.apiUrl,
'https://api.example.com:1234'
)

describe 'when custom settings contain API URL with trailing slash', ->
custom =
apiaryReporterEnv: env
apiaryApiUrl: 'https://api.example.com:1234/'

it 'uses the provided API URL in configuration, without trailing slash', ->
emitter = new EventEmitter
apiaryReporter = new ApiaryReporter emitter, {}, {}, {custom}
assert.equal(
apiaryReporter.configuration.apiUrl,
'https://api.example.com:1234'
)

describe "_performRequestAsync", () ->
describe 'when custom settings contain API URL without trailing slash', ->
custom =
apiaryReporterEnv: env
apiaryApiUrl: 'https://api.example.com:1234'

it 'should use API URL without double slashes', (done) ->
emitter = new EventEmitter
apiaryReporter = new ApiaryReporter emitter, {}, {}, {custom}
apiaryReporter._performRequestAsync '/', 'POST', '', (error) ->
assert.isOk loggerStub.verbose.calledWithMatch('POST https://api.example.com:1234/ (without body)')
done()

describe 'when custom settings contain API URL with trailing slash', ->
custom =
apiaryReporterEnv: env
apiaryApiUrl: 'https://api.example.com:1234/'

describe 'when provided with root path', ->
it 'should use API URL without double slashes', (done) ->
emitter = new EventEmitter
apiaryReporter = new ApiaryReporter emitter, {}, {}, {custom}
apiaryReporter._performRequestAsync '/', 'POST', '', (error) ->
assert.isOk loggerStub.verbose.calledWithMatch('POST https://api.example.com:1234/ (without body)')
done()

describe 'when provided with non-root path', ->
it 'should use API URL without double slashes', (done) ->
emitter = new EventEmitter
apiaryReporter = new ApiaryReporter emitter, {}, {}, {custom}
apiaryReporter._performRequestAsync '/hello?q=1', 'POST', '', (error) ->
assert.isOk loggerStub.verbose.calledWithMatch('POST https://api.example.com:1234/hello?q=1 (without body)')
done()

describe 'when server is not available', () ->
beforeEach () ->
nock.enableNetConnect()
Expand Down

0 comments on commit 67d5660

Please sign in to comment.