1 | var server = require('./server') |
---|
2 | , assert = require('assert') |
---|
3 | , request = require('../index') |
---|
4 | ; |
---|
5 | |
---|
6 | var s = server.createServer(); |
---|
7 | |
---|
8 | var tests = |
---|
9 | { testGet : |
---|
10 | { resp : server.createGetResponse("TESTING!") |
---|
11 | , expectBody: "TESTING!" |
---|
12 | } |
---|
13 | , testGetChunkBreak : |
---|
14 | { resp : server.createChunkResponse( |
---|
15 | [ new Buffer([239]) |
---|
16 | , new Buffer([163]) |
---|
17 | , new Buffer([191]) |
---|
18 | , new Buffer([206]) |
---|
19 | , new Buffer([169]) |
---|
20 | , new Buffer([226]) |
---|
21 | , new Buffer([152]) |
---|
22 | , new Buffer([131]) |
---|
23 | ]) |
---|
24 | , expectBody: "Ωâ" |
---|
25 | } |
---|
26 | , testGetBuffer : |
---|
27 | { resp : server.createGetResponse(new Buffer("TESTING!")) |
---|
28 | , encoding: null |
---|
29 | , expectBody: new Buffer("TESTING!") |
---|
30 | } |
---|
31 | , testGetJSON : |
---|
32 | { resp : server.createGetResponse('{"test":true}', 'application/json') |
---|
33 | , json : true |
---|
34 | , expectBody: {"test":true} |
---|
35 | } |
---|
36 | , testPutString : |
---|
37 | { resp : server.createPostValidator("PUTTINGDATA") |
---|
38 | , method : "PUT" |
---|
39 | , body : "PUTTINGDATA" |
---|
40 | } |
---|
41 | , testPutBuffer : |
---|
42 | { resp : server.createPostValidator("PUTTINGDATA") |
---|
43 | , method : "PUT" |
---|
44 | , body : new Buffer("PUTTINGDATA") |
---|
45 | } |
---|
46 | , testPutJSON : |
---|
47 | { resp : server.createPostValidator(JSON.stringify({foo: 'bar'})) |
---|
48 | , method: "PUT" |
---|
49 | , json: {foo: 'bar'} |
---|
50 | } |
---|
51 | , testPutMultipart : |
---|
52 | { resp: server.createPostValidator( |
---|
53 | '--__BOUNDARY__\r\n' + |
---|
54 | 'content-type: text/html\r\n' + |
---|
55 | '\r\n' + |
---|
56 | '<html><body>Oh hi.</body></html>' + |
---|
57 | '\r\n--__BOUNDARY__\r\n\r\n' + |
---|
58 | 'Oh hi.' + |
---|
59 | '\r\n--__BOUNDARY__--' |
---|
60 | ) |
---|
61 | , method: "PUT" |
---|
62 | , multipart: |
---|
63 | [ {'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'} |
---|
64 | , {'body': 'Oh hi.'} |
---|
65 | ] |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | s.listen(s.port, function () { |
---|
70 | |
---|
71 | var counter = 0 |
---|
72 | |
---|
73 | for (i in tests) { |
---|
74 | (function () { |
---|
75 | var test = tests[i] |
---|
76 | s.on('/'+i, test.resp) |
---|
77 | //test.uri = s.url + '/' + i |
---|
78 | request(s.url + '/' + i, test, function (err, resp, body) { |
---|
79 | if (err) throw err |
---|
80 | if (test.expectBody) { |
---|
81 | assert.deepEqual(test.expectBody, body) |
---|
82 | } |
---|
83 | counter = counter - 1; |
---|
84 | if (counter === 0) { |
---|
85 | assert.notEqual(typeof test.callback, 'function') |
---|
86 | console.log(1 + Object.keys(tests).length+" tests passed.") |
---|
87 | s.close() |
---|
88 | } |
---|
89 | }) |
---|
90 | counter++ |
---|
91 | })() |
---|
92 | } |
---|
93 | }) |
---|