从单元测试中,我需要创建一个http连接(到Chrome的--remote-debugging-port),并需要一种方法来等待基于http的json服务可用。
我找不到支持这种情况的npm软件包,http://nodejs.org/api/http.html#http_http_request_options_callback似乎不支持连接超时,而我看到的几个示例基于:
req.on('error', function (err) {
err.code === 'ECONNREFUSED'
那是最好的方法吗?
更新1: setTimeout不起作用,因为如果目标URL不可用,我们将收到一个异常
it 'connect_To_Chrome', (done)->
url_Debug = "http://localhost:#{nodeWebKit.port_Debug}/json"
url_Debug.GET (data)->
assert_Is_Null(data)
http = require('http')
# throws error if the value is less that 60 (i.e. 60ms)
(100).eval_After -> # setTimeout
socket = http.get url_Debug, -> # create socket
console.log 'inside get' # socket was created
socket.setTimeout 400, -> # try setTimeout
console.log 'inside timeout' # trigered after 400ms
done()
在上面显示的代码示例中,如果初始超时为10(或根本不使用),我们将获得以下异常
Error: socket hang up
at createHangUpError (http.js:1476:15)
at Socket.socketCloseListener (http.js:1526:23)
at Socket.emit (events.js:117:20)
at TCP.close (net.js:465:12)
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
更新2:这是解决方案的第一拳
it 'get Chrome Remote Debugging /json', (done)->
wait_For_Http_GET = (url, timeout, callback)->
delay = 10;
try_Http_Get = (next) =>
url.GET (data) => if data is null then (delay).invoke_After next else callback(data)
run_Tests = (test_Count)=> if test_Count.empty() then url.GET (callback) else try_Http_Get ()->run_Tests(test_Count.splice(0,1))
run_Tests([0.. ~~(timeout/delay)])
url_Debug = "http://localhost:#{nodeWebKit.port_Debug}/json"
url_Debug.GET (data)->
assert_Is_Null(data)
wait_For_Http_GET url_Debug,100, (html)->
data = JSON.parse(html)
data.assert_Is_Array().assert_Size_Is(1)
data[0].description .assert_Is('')
data[0].devtoolsFrontendUrl .assert_Is("/devtools/devtools.html?ws=localhost:#{nodeWebKit.port_Debug}/devtools/page/#{data[0].id}")
data[0].id .split('-').assert_Size_Is(5)
data[0].title .assert_Is('')
data[0].type .assert_Is('page')
data[0].url .assert_Is('nw:blank')
data[0].webSocketDebuggerUrl.assert_Is("ws://localhost:#{nodeWebKit.port_Debug}/devtools/page/#{data[0].id}")
done()
我将选择我的更新2答案,因为它确实按预期工作并回答了问题
wait_For_Http_GET = (url, timeout, callback)->
delay = 10;
try_Http_Get = (next) =>
url.GET (data) => if data is null then (delay).invoke_After next else callback(data)
run_Tests = (test_Count)=> if test_Count.empty() then url.GET (callback) else try_Http_Get ()->run_Tests(test_Count.splice(0,1))
run_Tests([0.. ~~(timeout/delay)])
测试
it 'get Chrome Remote Debugging /json', (done)->
url_Debug = "http://localhost:#{nodeWebKit.port_Debug}/json"
url_Debug.GET (data)->
assert_Is_Null(data)
wait_For_Http_GET url_Debug,100, (html)->
data = JSON.parse(html)
data.assert_Is_Array().assert_Size_Is(1)
data[0].description .assert_Is('')
data[0].devtoolsFrontendUrl .assert_Is("/devtools/devtools.html?ws=localhost:#{nodeWebKit.port_Debug}/devtools/page/#{data[0].id}")
data[0].id .split('-').assert_Size_Is(5)
data[0].title .assert_Is('')
data[0].type .assert_Is('page')
data[0].url .assert_Is('nw:blank')
data[0].webSocketDebuggerUrl.assert_Is("ws://localhost:#{nodeWebKit.port_Debug}/devtools/page/#{data[0].id}")
done()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句