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