simple.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. var fs = require('fs');
  2. var request = require('supertest');
  3. var should = require('should');
  4. var route = require('koa-route');
  5. var range = require('../');
  6. var Koa = require('koa');
  7. var app = new Koa();
  8. var rawbody = new Buffer(1024);
  9. var rawFileBuffer = fs.readFileSync('./README.md') + '';
  10. app.use(range);
  11. app.use(route.get('/', async function(ctx) {
  12. ctx.body = rawbody;
  13. }));
  14. app.use(route.post('/', async function(ctx) {
  15. ctx.status = 200;
  16. }));
  17. app.use(route.get('/json', async function(ctx) {
  18. ctx.body = {foo:'bar'};
  19. }));
  20. app.use(route.get('/string', async function(ctx) {
  21. ctx.body = 'koa-range';
  22. }));
  23. app.use(route.get('/stream', async function(ctx) {
  24. ctx.body = fs.createReadStream('./README.md');
  25. }));
  26. app.use(route.get('/empty', async function(ctx) {
  27. ctx.body = undefined;
  28. ctx.status = 304;
  29. }));
  30. app.on('error', function(err) {
  31. throw err;
  32. });
  33. describe('normal requests', function() {
  34. it('should return 200 without range', function(done) {
  35. request(app.listen())
  36. .get('/')
  37. .expect('Accept-Ranges', 'bytes')
  38. .expect(200)
  39. .end(done);
  40. });
  41. it('should return 200 when method not GET', function(done) {
  42. request(app.listen())
  43. .post('/')
  44. .set('range', 'bytes=0-300')
  45. .expect('Accept-Ranges', 'bytes')
  46. .expect(200)
  47. .end(done);
  48. });
  49. });
  50. describe('range requests', function() {
  51. it('should return 206 with partial content', function(done) {
  52. request(app.listen())
  53. .get('/')
  54. .set('range', 'bytes=0-299')
  55. .expect('Content-Length', '300')
  56. .expect('Accept-Ranges', 'bytes')
  57. .expect('Content-Range', 'bytes 0-299/1024')
  58. .expect(206)
  59. .end(done);
  60. });
  61. it('should return 400 with PUT', function(done) {
  62. request(app.listen())
  63. .put('/')
  64. .set('range', 'bytes=0-299')
  65. .expect('Accept-Ranges', 'bytes')
  66. .expect(400)
  67. .end(done);
  68. });
  69. it('should return 416 with invalid range', function(done) {
  70. request(app.listen())
  71. .get('/')
  72. .set('range', 'bytes=400-300')
  73. .expect('Accept-Ranges', 'bytes')
  74. .expect(416)
  75. .end(done);
  76. });
  77. it('should return 416 with invalid range', function(done) {
  78. request(app.listen())
  79. .get('/')
  80. .set('range', 'bytes=x-300')
  81. .expect('Accept-Ranges', 'bytes')
  82. .expect(416)
  83. .end(done);
  84. });
  85. it('should return 416 with invalid range', function(done) {
  86. request(app.listen())
  87. .get('/')
  88. .set('range', 'bytes=400-x')
  89. .expect('Accept-Ranges', 'bytes')
  90. .expect(416)
  91. .end(done);
  92. });
  93. it('should return 416 with invalid range', function(done) {
  94. request(app.listen())
  95. .get('/')
  96. .set('range', 'bytes')
  97. .expect('Accept-Ranges', 'bytes')
  98. .expect(416)
  99. .end(done);
  100. });
  101. });
  102. describe('range requests with stream', function() {
  103. it('should return 206 with partial content', function(done) {
  104. request(app.listen())
  105. .get('/stream')
  106. .set('range', 'bytes=0-99')
  107. .expect('Transfer-Encoding', 'chunked')
  108. .expect('Accept-Ranges', 'bytes')
  109. .expect('Content-Range', 'bytes 0-99/*')
  110. .expect(206)
  111. .end(function(err, res) {
  112. should.not.exist(err);
  113. res.text.should.equal(rawFileBuffer.slice(0, 100));
  114. done();
  115. });
  116. });
  117. it('should return 206 with open ended range', function(done) {
  118. request(app.listen())
  119. .get('/stream')
  120. .set('range', 'bytes=0-')
  121. .expect('Transfer-Encoding', 'chunked')
  122. .expect(200)
  123. .end(function(err, res) {
  124. should.not.exist(err);
  125. res.text.should.startWith(rawFileBuffer.slice(0, 300));
  126. done();
  127. });
  128. });
  129. });
  130. describe('range requests with json', function() {
  131. it('should return 206 with partial content', function(done) {
  132. request(app.listen())
  133. .get('/json')
  134. .set('range', 'bytes=0-5')
  135. .expect('Accept-Ranges', 'bytes')
  136. .expect('Content-Range', 'bytes 0-5/13')
  137. .expect('Content-Length', '6')
  138. .expect(206)
  139. .end(function(err, res) {
  140. should.not.exist(err);
  141. res.text.should.equal('{"foo"');
  142. done();
  143. });
  144. });
  145. it('should return 206 with single byte range', function(done) {
  146. request(app.listen())
  147. .get('/json')
  148. .set('range', 'bytes=2-2')
  149. .expect('Accept-Ranges', 'bytes')
  150. .expect('Content-Range', 'bytes 2-2/13')
  151. .expect('Content-Length', '1')
  152. .expect(206)
  153. .end(function(err, res) {
  154. should.not.exist(err);
  155. res.text.should.equal('f');
  156. done();
  157. });
  158. });
  159. });
  160. describe('range requests with string', function() {
  161. it('should return 206 with partial content', function(done) {
  162. request(app.listen())
  163. .get('/string')
  164. .set('range', 'bytes=0-5')
  165. .expect('Accept-Ranges', 'bytes')
  166. .expect('Content-Range', 'bytes 0-5/9')
  167. .expect('Content-Length', '6')
  168. .expect(206)
  169. .end(function(err, res) {
  170. should.not.exist(err);
  171. res.text.should.equal('koa-ra');
  172. done();
  173. });
  174. });
  175. it('should return 206 with open ended range', function(done) {
  176. request(app.listen())
  177. .get('/string')
  178. .set('range', 'bytes=3-')
  179. .expect('Accept-Ranges', 'bytes')
  180. .expect('Content-Range', 'bytes 3-8/9')
  181. .expect('Content-Length', '6')
  182. .expect(206)
  183. .end(function(err, res) {
  184. should.not.exist(err);
  185. res.text.should.equal('-range');
  186. done();
  187. });
  188. });
  189. });
  190. describe('range requests with empty body', function() {
  191. it('should return 304', function(done) {
  192. request(app.listen())
  193. .get('/empty')
  194. .set('range', 'bytes=1-')
  195. .expect(304)
  196. .end(function(err, res) {
  197. should.not.exist(err);
  198. done();
  199. });
  200. });
  201. });