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