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