25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

203 satır
5.1 KiB

  1. var server = require('./server')
  2. , events = require('events')
  3. , stream = require('stream')
  4. , assert = require('assert')
  5. , fs = require('fs')
  6. , request = require('../main.js')
  7. , path = require('path')
  8. , util = require('util')
  9. ;
  10. var s = server.createServer(3453);
  11. function ValidationStream(str) {
  12. this.str = str
  13. this.buf = ''
  14. this.on('data', function (data) {
  15. this.buf += data
  16. })
  17. this.on('end', function () {
  18. assert.equal(this.str, this.buf)
  19. })
  20. this.writable = true
  21. }
  22. util.inherits(ValidationStream, stream.Stream)
  23. ValidationStream.prototype.write = function (chunk) {
  24. this.emit('data', chunk)
  25. }
  26. ValidationStream.prototype.end = function (chunk) {
  27. if (chunk) emit('data', chunk)
  28. this.emit('end')
  29. }
  30. s.listen(s.port, function () {
  31. counter = 0;
  32. var check = function () {
  33. counter = counter - 1
  34. if (counter === 0) {
  35. console.log('All tests passed.')
  36. setTimeout(function () {
  37. process.exit();
  38. }, 500)
  39. }
  40. }
  41. // Test pipeing to a request object
  42. s.once('/push', server.createPostValidator("mydata"));
  43. var mydata = new stream.Stream();
  44. mydata.readable = true
  45. counter++
  46. var r1 = request.put({url:'http://localhost:3453/push'}, function () {
  47. check();
  48. })
  49. mydata.pipe(r1)
  50. mydata.emit('data', 'mydata');
  51. mydata.emit('end');
  52. // Test pipeing from a request object.
  53. s.once('/pull', server.createGetResponse("mypulldata"));
  54. var mypulldata = new stream.Stream();
  55. mypulldata.writable = true
  56. counter++
  57. request({url:'http://localhost:3453/pull'}).pipe(mypulldata)
  58. var d = '';
  59. mypulldata.write = function (chunk) {
  60. d += chunk;
  61. }
  62. mypulldata.end = function () {
  63. assert.equal(d, 'mypulldata');
  64. check();
  65. };
  66. s.on('/cat', function (req, resp) {
  67. if (req.method === "GET") {
  68. resp.writeHead(200, {'content-type':'text/plain-test', 'content-length':4});
  69. resp.end('asdf')
  70. } else if (req.method === "PUT") {
  71. assert.equal(req.headers['content-type'], 'text/plain-test');
  72. assert.equal(req.headers['content-length'], 4)
  73. var validate = '';
  74. req.on('data', function (chunk) {validate += chunk})
  75. req.on('end', function () {
  76. resp.writeHead(201);
  77. resp.end();
  78. assert.equal(validate, 'asdf');
  79. check();
  80. })
  81. }
  82. })
  83. s.on('/pushjs', function (req, resp) {
  84. if (req.method === "PUT") {
  85. assert.equal(req.headers['content-type'], 'text/javascript');
  86. check();
  87. }
  88. })
  89. s.on('/catresp', function (req, resp) {
  90. request.get('http://localhost:3453/cat').pipe(resp)
  91. })
  92. s.on('/doodle', function (req, resp) {
  93. if (req.headers['x-oneline-proxy']) {
  94. resp.setHeader('x-oneline-proxy', 'yup')
  95. }
  96. resp.writeHead('200', {'content-type':'image/png'})
  97. fs.createReadStream(path.join(__dirname, 'googledoodle.png')).pipe(resp)
  98. })
  99. s.on('/onelineproxy', function (req, resp) {
  100. var x = request('http://localhost:3453/doodle')
  101. req.pipe(x)
  102. x.pipe(resp)
  103. })
  104. counter++
  105. fs.createReadStream(__filename).pipe(request.put('http://localhost:3453/pushjs'))
  106. counter++
  107. request.get('http://localhost:3453/cat').pipe(request.put('http://localhost:3453/cat'))
  108. counter++
  109. request.get('http://localhost:3453/catresp', function (e, resp, body) {
  110. assert.equal(resp.headers['content-type'], 'text/plain-test');
  111. assert.equal(resp.headers['content-length'], 4)
  112. check();
  113. })
  114. var doodleWrite = fs.createWriteStream(path.join(__dirname, 'test.png'))
  115. counter++
  116. request.get('http://localhost:3453/doodle').pipe(doodleWrite)
  117. doodleWrite.on('close', function () {
  118. assert.deepEqual(fs.readFileSync(path.join(__dirname, 'googledoodle.png')), fs.readFileSync(path.join(__dirname, 'test.png')))
  119. check()
  120. })
  121. process.on('exit', function () {
  122. fs.unlinkSync(path.join(__dirname, 'test.png'))
  123. })
  124. counter++
  125. request.get({uri:'http://localhost:3453/onelineproxy', headers:{'x-oneline-proxy':'nope'}}, function (err, resp, body) {
  126. assert.equal(resp.headers['x-oneline-proxy'], 'yup')
  127. check()
  128. })
  129. s.on('/afterresponse', function (req, resp) {
  130. resp.write('d')
  131. resp.end()
  132. })
  133. counter++
  134. var afterresp = request.post('http://localhost:3453/afterresponse').on('response', function () {
  135. var v = new ValidationStream('d')
  136. afterresp.pipe(v)
  137. v.on('end', check)
  138. })
  139. s.on('/forward1', function (req, resp) {
  140. resp.writeHead(302, {location:'/forward2'})
  141. resp.end()
  142. })
  143. s.on('/forward2', function (req, resp) {
  144. resp.writeHead('200', {'content-type':'image/png'})
  145. resp.write('d')
  146. resp.end()
  147. })
  148. counter++
  149. var validateForward = new ValidationStream('d')
  150. validateForward.on('end', check)
  151. request.get('http://localhost:3453/forward1').pipe(validateForward)
  152. // Test pipe options
  153. s.once('/opts', server.createGetResponse('opts response'));
  154. var optsStream = new stream.Stream();
  155. optsStream.writable = true
  156. var optsData = '';
  157. optsStream.write = function (buf) {
  158. optsData += buf;
  159. if (optsData === 'opts response') {
  160. setTimeout(check, 10);
  161. }
  162. }
  163. optsStream.end = function () {
  164. assert.fail('end called')
  165. };
  166. counter++
  167. request({url:'http://localhost:3453/opts'}).pipe(optsStream, { end : false })
  168. })