helpers = require('../helpers')
AWS = helpers.AWS
describe 'AWS.SQS', ->
sqs = null
beforeEach ->
sqs = new AWS.SQS params: QueueUrl: 'http://url'
checksumValidate = (operation, input, response, shouldPass, cb) ->
output = null
helpers.mockHttpResponse 200, {}, response
runs ->
sqs[operation](input, (e, d) -> output = error: e, data: d)
waitsFor -> output
runs ->
if shouldPass
expect(output.error).toEqual(null)
else
expect(output.error).not.toEqual(null)
if cb
cb(output.error, output.data)
describe 'buildEndpoint', ->
it 'should detect correct region from QueueUrl', ->
sqs = new AWS.SQS
computeChecksums: false
params: QueueUrl: 'http://sqs.region-1.amazonaws.com/queue'
helpers.mockHttpResponse 200, {}, ''
req = sqs.sendMessage(MessageBody: 'foo')
req.build()
expect(req.httpRequest.region).toEqual('region-1')
describe 'sendMessage', ->
input = MessageBody: 'foo'
md5 = 'acbd18db4cc2f85cedef654fccc4a4d8'
payload = (md5) ->
"""
#{md5}
MSGID
"""
it 'correctly validates MD5 of message input', ->
checksumValidate 'sendMessage', input, payload(md5), true, (err, data) ->
expect(data.MD5OfMessageBody).toEqual(md5)
it 'raises InvalidChecksum if MD5 does not match message input', ->
checksumValidate 'sendMessage', input, payload('000'), false, (err) ->
expect(err.message).toMatch('Got "000", expecting "acbd18db4cc2f85cedef654fccc4a4d8"')
expect(err.messageIds).toEqual(['MSGID'])
it 'ignores checksum errors if computeChecksums is false', ->
sqs.config.computeChecksums = false
checksumValidate 'sendMessage', input, payload('000'), true
describe 'sendMessageBatch', ->
input = Entries: [
{ Id: 'a', MessageBody: 'foo' },
{ Id: 'b', MessageBody: 'bar' },
{ Id: 'c', MessageBody: 'bar' }
]
md5foo = 'acbd18db4cc2f85cedef654fccc4a4d8'
md5bar = '37b51d194a7513e45b56f6524f2d51f2'
payload = (md5a, md5b, md5c) ->
"""
a
MSGID1
#{md5a}
b
MSGID2
#{md5b}
c
MSGID3
#{md5c}
"""
it 'correctly validates MD5 of operation', ->
output = payload(md5foo, md5bar, md5bar)
checksumValidate 'sendMessageBatch', input, output, true, (err, data) ->
expect(data.Successful[0].MD5OfMessageBody).toEqual(md5foo)
expect(data.Successful[1].MD5OfMessageBody).toEqual(md5bar)
expect(data.Successful[2].MD5OfMessageBody).toEqual(md5bar)
it 'raises InvalidChecksum with relevent message IDs', ->
output = payload('000', md5bar, '000')
checksumValidate 'sendMessageBatch', input, output, false, (err, data) ->
expect(err.message).toMatch('Invalid messages: a, c')
expect(err.messageIds).toEqual(['MSGID1', 'MSGID3'])
it 'ignores checksum errors if computeChecksums is false', ->
output = payload(md5foo, '000', md5bar)
sqs.config.computeChecksums = false
checksumValidate 'sendMessageBatch', input, output, true
describe 'receiveMessage', ->
md5 = 'acbd18db4cc2f85cedef654fccc4a4d8'
payload = (body, md5, id) ->
"""
#{body}
#{md5}
#{id}
"""
it 'correctly validates MD5 of operation', ->
output = payload('foo', md5, 'MSGID')
checksumValidate 'receiveMessage', {}, output, true, (err, data) ->
expect(data.Messages[0].MD5OfBody).toEqual(md5)
it 'raises InvalidChecksum with relevent message IDs', ->
output = payload('foo', '000', 'MSGID')
checksumValidate 'receiveMessage', {}, output, false, (err, data) ->
expect(err.message).toMatch('Invalid messages: MSGID')
expect(err.messageIds).toEqual(['MSGID'])
it 'ignores checksum errors if computeChecksums is false', ->
output = payload('foo', '000', 'MSGID')
sqs.config.computeChecksums = false
checksumValidate 'receiveMessage', {}, output, true, (err, data) ->
expect(data.Messages[0].MD5OfBody).not.toEqual(md5)