Skip to content

Commit bb6745d

Browse files
committed
Modifies tests to be compatible with Node 0.10
1 parent 44fb727 commit bb6745d

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

test/metadata_service.spec.coffee

+19-26
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,28 @@ AWS = helpers.AWS
66
if AWS.util.isNode()
77
describe 'AWS.MetadataService', ->
88
describe 'loadCredentials', ->
9-
[server, port, service] = [null, 1024 + parseInt(Math.random() * 100), null]
10-
[forceTimeout, httpRequest] = [false, null]
9+
[server, port, service, forceTimeout] = [null, 1024 + parseInt(Math.random() * 100), null, null]
1110
httpClient = AWS.HttpClient.getInstance()
1211

1312
beforeEach ->
14-
[forceTimeout, httpRequest] = [false, null]
13+
forceTimeout = false
14+
helpers.spyOn(http.ClientRequest.prototype, 'setTimeout').andCallFake (timeout, cb) ->
15+
if forceTimeout
16+
process.nextTick(cb)
1517
service = new AWS.MetadataService(host: '127.0.0.1:' + port)
1618
server = http.createServer (req, res) ->
17-
if forceTimeout
18-
httpRequest.stream.emit('error', {code: 'TimeoutError'})
19-
else
20-
re = new RegExp('^/latest/meta-data/iam/security-credentials/(.*)$')
21-
match = url.parse(req.url).pathname.match(re)
22-
if match
23-
res.writeHead(200, 'Content-Type': 'text/plain')
24-
if match[1] == ''
25-
res.write('TestingRole\n')
26-
res.write('TestingRole2\n')
27-
else
28-
data = '{"Code":"Success","AccessKeyId":"KEY","SecretAccessKey":"SECRET","Token":"TOKEN"}'
29-
res.write(data)
19+
re = new RegExp('^/latest/meta-data/iam/security-credentials/(.*)$')
20+
match = url.parse(req.url).pathname.match(re)
21+
if match
22+
res.writeHead(200, 'Content-Type': 'text/plain')
23+
if match[1] == ''
24+
res.write('TestingRole\n')
25+
res.write('TestingRole2\n')
3026
else
31-
res.writeHead(404, {})
27+
data = '{"Code":"Success","AccessKeyId":"KEY","SecretAccessKey":"SECRET","Token":"TOKEN"}'
28+
res.write(data)
29+
else
30+
res.writeHead(404, {})
3231
res.end()
3332

3433
server.listen(port)
@@ -80,9 +79,7 @@ if AWS.util.isNode()
8079
firstTry = false
8180
else
8281
forceTimeout = false
83-
args = spy.calls[spy.calls.length - 1].arguments
84-
httpRequest = args[0]
85-
return spy.origMethod.apply(httpClient, args)
82+
return spy.origMethod.apply(httpClient, arguments)
8683

8784
service = new AWS.MetadataService(options)
8885
service.loadCredentials (err, data) ->
@@ -97,12 +94,8 @@ if AWS.util.isNode()
9794
host: '127.0.0.1:' + port,
9895
maxRetries: 5
9996
}
100-
101-
spy = helpers.spyOn(httpClient, 'handleRequest').andCallFake ->
102-
forceTimeout = true
103-
args = spy.calls[spy.calls.length - 1].arguments
104-
httpRequest = args[0]
105-
return spy.origMethod.apply(httpClient, args)
97+
forceTimeout = true
98+
spy = helpers.spyOn(httpClient, 'handleRequest').andCallThrough()
10699

107100
service = new AWS.MetadataService(options)
108101
service.loadCredentials (err, data) ->

test/util.spec.coffee

+19-6
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,11 @@ describe 'AWS.util.calculateRetryDelay', ->
752752

753753
if AWS.util.isNode()
754754
describe 'AWS.util.handleRequestWithTimeoutRetries', ->
755+
http = require('http')
755756
app = null
756757
httpRequest = null
757758
spy = null
759+
forceTimeout = null
758760
options = {maxRetries: 2}
759761
httpClient = AWS.HttpClient.getInstance()
760762

@@ -769,10 +771,14 @@ if AWS.util.isNode()
769771
srv.once 'close', -> cb(port)
770772
srv.close()
771773

772-
server = require('http').createServer (req, resp) ->
774+
server = http.createServer (req, resp) ->
773775
app(req, resp)
774776

775777
beforeEach (done) ->
778+
forceTimeout = false
779+
helpers.spyOn(http.ClientRequest.prototype, 'setTimeout').andCallFake (timeout, cb) ->
780+
if forceTimeout
781+
process.nextTick(cb)
776782
httpRequest = new AWS.HttpRequest('http://127.0.0.1')
777783
spy = helpers.spyOn(httpClient, 'handleRequest').andCallThrough()
778784
getport (port) ->
@@ -796,8 +802,10 @@ if AWS.util.isNode()
796802
it 'does not retry non-timeout errors', (done) ->
797803
app = (req, resp) ->
798804
httpRequest.stream.emit('error', {code: 'SomeError'})
805+
resp.write('FOOBAR')
799806
resp.end()
800807
sendRequest (err, data) ->
808+
console.log('cb was called')
801809
expect(data).to.be.undefined
802810
expect(err).to.not.be.null
803811
expect(err.code).to.equal('SomeError')
@@ -806,12 +814,15 @@ if AWS.util.isNode()
806814

807815
it 'retries for TimeoutError', (done) ->
808816
firstcall = true
809-
app = (req, resp) ->
817+
spy = helpers.spyOn(httpClient, 'handleRequest').andCallFake ->
810818
if firstcall
811-
httpRequest.stream.emit('error', {code: 'TimeoutError'})
819+
forceTimeout = true
812820
firstcall = false
813821
else
814-
resp.write('FOOBAR')
822+
forceTimeout = false
823+
return spy.origMethod.apply(httpClient, arguments)
824+
app = (req, resp) ->
825+
resp.write('FOOBAR')
815826
resp.end()
816827
sendRequest (err, data) ->
817828
expect(err).to.be.null
@@ -820,8 +831,9 @@ if AWS.util.isNode()
820831
done()
821832

822833
it 'retries up to the maxRetries specified', (done) ->
834+
forceTimeout = true
823835
app = (req, resp) ->
824-
httpRequest.stream.emit('error', {code: 'TimeoutError'})
836+
resp.write('FOOBAR')
825837
resp.end()
826838
sendRequest (err, data) ->
827839
expect(data).to.be.undefined
@@ -831,8 +843,9 @@ if AWS.util.isNode()
831843
done()
832844

833845
it 'defaults to not retrying if maxRetries not specified', (done) ->
846+
forceTimeout = true
834847
app = (req, resp) ->
835-
httpRequest.stream.emit('error', {code: 'TimeoutError'})
848+
resp.write('FOOBAR')
836849
resp.end()
837850
options = {}
838851
sendRequest (err, data) ->

0 commit comments

Comments
 (0)