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