node.js
1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require([
'require',
'doh/main',
'dojo/request',
'dojo/node!http',
'dojo/node!url',
'dojo/Deferred'
], function(require, doh, request, http, url, Deferred){
var serverPort = 8142,
serverUrl = 'http://localhost:8124';
var responseDataMap = {
'fooBar': '{ "foo": "bar" }',
'invalidJson': '<not>JSON</not>'
};
function getRequestUrl(dataKey){
return serverUrl + '?dataKey=' + dataKey;
}
function getResponseData(request){
var parseQueryString = true;
var urlInfo = url.parse(request.url, parseQueryString);
return responseDataMap[urlInfo.query.dataKey];
}
var server = http.createServer(function(request, response){
var body = getResponseData(request);
response.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'application/json'
});
response.write(body);
response.end();
});
function setUp(){ /* Do nothing */ }
function tearDown(){ server.close(); }
server.on('listening', function(){
var tests = [
{
name: 'test',
runTest: function(t){
var d = new doh.Deferred();
request.get(getRequestUrl('fooBar'), {
handleAs: 'json'
}).then(d.getTestCallback(function(data){
t.is({ foo: 'bar' }, data);
}), function(err){
d.errback(err);
});
return d;
}
},
{
name: 'test-handler-exception',
runTest: function(t){
var d = new doh.Deferred();
request.get(getRequestUrl('invalidJson'), {
handleAs: 'json'
}).then(function(){
d.errback(new Error('Expected a handler exception.'));
}, d.getTestCallback(function(err){
doh.assertTrue(err instanceof SyntaxError);
}));
return d;
}
}
];
doh.register('tests.request.node', tests, setUp, tearDown);
doh.run();
});
server.listen(8124);
});