api-test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. var assert = require('assert');
  2. var net = require('net');
  3. var http = require('http');
  4. var streamPair = require('stream-pair');
  5. var thing = require('handle-thing');
  6. var httpDeceiver = require('../');
  7. describe('HTTP Deceiver', function() {
  8. var handle;
  9. var pair;
  10. var socket;
  11. var deceiver;
  12. beforeEach(function() {
  13. pair = streamPair.create();
  14. handle = thing.create(pair.other);
  15. socket = new net.Socket({ handle: handle });
  16. // For v0.8
  17. socket.readable = true;
  18. socket.writable = true;
  19. deceiver = httpDeceiver.create(socket);
  20. });
  21. it('should emit request', function(done) {
  22. var server = http.createServer();
  23. server.emit('connection', socket);
  24. server.on('request', function(req, res) {
  25. assert.equal(req.method, 'PUT');
  26. assert.equal(req.url, '/hello');
  27. assert.deepEqual(req.headers, { a: 'b' });
  28. done();
  29. });
  30. deceiver.emitRequest({
  31. method: 'PUT',
  32. path: '/hello',
  33. headers: {
  34. a: 'b'
  35. }
  36. });
  37. });
  38. it('should emit response', function(done) {
  39. var agent = new http.Agent();
  40. agent.createConnection = function createConnection() {
  41. return socket;
  42. };
  43. var client = http.request({
  44. method: 'POST',
  45. path: '/ok',
  46. agent: agent
  47. }, function(res) {
  48. assert.equal(res.statusCode, 421);
  49. assert.deepEqual(res.headers, { a: 'b' });
  50. done();
  51. });
  52. process.nextTick(function() {
  53. deceiver.emitResponse({
  54. status: 421,
  55. reason: 'F',
  56. headers: {
  57. a: 'b'
  58. }
  59. });
  60. });
  61. });
  62. it('should override .execute and .finish', function(done) {
  63. var server = http.createServer();
  64. server.emit('connection', socket);
  65. server.on('request', function(req, res) {
  66. assert.equal(req.method, 'PUT');
  67. assert.equal(req.url, '/hello');
  68. assert.deepEqual(req.headers, { a: 'b' });
  69. var actual = '';
  70. req.on('data', function(chunk) {
  71. actual += chunk;
  72. });
  73. req.once('end', function() {
  74. assert.equal(actual, 'hello world');
  75. done();
  76. });
  77. });
  78. deceiver.emitRequest({
  79. method: 'PUT',
  80. path: '/hello',
  81. headers: {
  82. a: 'b'
  83. }
  84. });
  85. pair.write('hello');
  86. pair.end(' world');
  87. });
  88. it('should work with reusing parser', function(done) {
  89. var server = http.createServer();
  90. server.emit('connection', socket);
  91. function secondRequest() {
  92. pair = streamPair.create();
  93. handle = thing.create(pair.other);
  94. socket = new net.Socket({ handle: handle });
  95. // For v0.8
  96. socket.readable = true;
  97. socket.writable = true;
  98. server.emit('connection', socket);
  99. pair.end('PUT /second HTTP/1.1\r\nContent-Length:11\r\n\r\nhello world');
  100. }
  101. server.on('request', function(req, res) {
  102. var actual = '';
  103. req.on('data', function(chunk) {
  104. actual += chunk;
  105. });
  106. req.once('end', function() {
  107. assert.equal(actual, 'hello world');
  108. if (req.url === '/first')
  109. secondRequest();
  110. else
  111. done();
  112. });
  113. });
  114. deceiver.emitRequest({
  115. method: 'PUT',
  116. path: '/first',
  117. headers: {
  118. a: 'b'
  119. }
  120. });
  121. pair.write('hello');
  122. pair.end(' world');
  123. });
  124. it('should emit CONNECT request', function(done) {
  125. var server = http.createServer();
  126. server.emit('connection', socket);
  127. server.on('connect', function(req, socket, bodyHead) {
  128. assert.equal(req.method, 'CONNECT');
  129. assert.equal(req.url, '/hello');
  130. done();
  131. });
  132. deceiver.emitRequest({
  133. method: 'CONNECT',
  134. path: '/hello',
  135. headers: {
  136. }
  137. });
  138. });
  139. it('should emit Upgrade request', function(done) {
  140. var server = http.createServer();
  141. server.emit('connection', socket);
  142. server.on('upgrade', function(req, socket, bodyHead) {
  143. assert.equal(req.method, 'POST');
  144. assert.equal(req.url, '/hello');
  145. socket.on('data', function(chunk) {
  146. assert.equal(chunk + '', 'hm');
  147. done();
  148. });
  149. });
  150. deceiver.emitRequest({
  151. method: 'POST',
  152. path: '/hello',
  153. headers: {
  154. 'upgrade': 'websocket'
  155. }
  156. });
  157. pair.write('hm');
  158. });
  159. it('should emit Upgrade response', function(done) {
  160. var agent = new http.Agent();
  161. agent.createConnection = function createConnection() {
  162. return socket;
  163. };
  164. var client = http.request({
  165. method: 'POST',
  166. path: '/ok',
  167. headers: {
  168. connection: 'upgrade',
  169. upgrade: 'websocket'
  170. },
  171. agent: agent
  172. }, function(res) {
  173. assert(false);
  174. });
  175. client.on('upgrade', function(res, socket) {
  176. assert.equal(res.statusCode, 421);
  177. done();
  178. });
  179. process.nextTick(function() {
  180. deceiver.emitResponse({
  181. status: 421,
  182. reason: 'F',
  183. headers: {
  184. upgrade: 'websocket'
  185. }
  186. });
  187. });
  188. });
  189. });