You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.1 KiB

8 years ago
  1. var assert = require('assert'),
  2. path = require('path'),
  3. fs = require('fs'),
  4. vows = require('vows'),
  5. request = require('request'),
  6. httpServer = require('../lib/http-server');
  7. var root = path.join(__dirname, 'fixtures', 'root');
  8. vows.describe('http-server').addBatch({
  9. 'When http-server is listening on 8080': {
  10. topic: function () {
  11. var server = httpServer.createServer({
  12. root: root,
  13. robots: true,
  14. headers: {
  15. 'Access-Control-Allow-Origin': '*',
  16. 'Access-Control-Allow-Credentials': 'true'
  17. }
  18. });
  19. server.listen(8080);
  20. this.callback(null, server);
  21. },
  22. 'it should serve files from root directory': {
  23. topic: function () {
  24. request('http://127.0.0.1:8080/file', this.callback);
  25. },
  26. 'status code should be 200': function (res) {
  27. assert.equal(res.statusCode, 200);
  28. },
  29. 'and file content': {
  30. topic: function (res, body) {
  31. var self = this;
  32. fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
  33. self.callback(err, data, body);
  34. });
  35. },
  36. 'should match content of served file': function (err, file, body) {
  37. assert.equal(body.trim(), file.trim());
  38. }
  39. }
  40. },
  41. 'when requesting non-existent file': {
  42. topic: function () {
  43. request('http://127.0.0.1:8080/404', this.callback);
  44. },
  45. 'status code should be 404': function (res) {
  46. assert.equal(res.statusCode, 404);
  47. }
  48. },
  49. 'when requesting /': {
  50. topic: function () {
  51. request('http://127.0.0.1:8080/', this.callback);
  52. },
  53. 'should respond with index': function (err, res, body) {
  54. assert.equal(res.statusCode, 200);
  55. assert.include(body, '/file');
  56. assert.include(body, '/canYouSeeMe');
  57. }
  58. },
  59. 'when robots options is activated': {
  60. topic: function () {
  61. request('http://127.0.0.1:8080/', this.callback);
  62. },
  63. 'should respond with status code 200 to /robots.txt': function (res) {
  64. assert.equal(res.statusCode, 200);
  65. }
  66. },
  67. 'and options include custom set http-headers': {
  68. topic: function () {
  69. request('http://127.0.0.1:8080/', this.callback);
  70. },
  71. 'should respond with headers set in options': function (err, res) {
  72. assert.equal(res.headers['access-control-allow-origin'], '*');
  73. assert.equal(res.headers['access-control-allow-credentials'], 'true');
  74. }
  75. },
  76. 'When http-server is proxying from 8081 to 8080': {
  77. topic: function () {
  78. var proxyServer = httpServer.createServer({
  79. proxy: 'http://127.0.0.1:8080/',
  80. root: path.join(__dirname, 'fixtures')
  81. });
  82. proxyServer.listen(8081);
  83. this.callback(null, proxyServer);
  84. },
  85. 'it should serve files from the proxy server root directory': {
  86. topic: function () {
  87. request('http://127.0.0.1:8081/root/file', this.callback);
  88. },
  89. 'status code should be the enpoint code 200': function (res) {
  90. assert.equal(res.statusCode, 200);
  91. },
  92. 'and file content': {
  93. topic: function (res, body) {
  94. var self = this;
  95. fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
  96. self.callback(err, data, body);
  97. });
  98. },
  99. 'should match content of the served file': function (err, file, body) {
  100. assert.equal(body.trim(), file.trim());
  101. }
  102. }
  103. },
  104. 'it should fallback to the proxied server': {
  105. topic: function () {
  106. request('http://127.0.0.1:8081/file', this.callback);
  107. },
  108. 'status code should be the enpoint code 200': function (res) {
  109. assert.equal(res.statusCode, 200);
  110. },
  111. 'and file content': {
  112. topic: function (res, body) {
  113. var self = this;
  114. fs.readFile(path.join(root, 'file'), 'utf8', function (err, data) {
  115. self.callback(err, data, body);
  116. });
  117. },
  118. 'should match content of the proxied served file': function (err, file, body) {
  119. assert.equal(body.trim(), file.trim());
  120. }
  121. }
  122. }
  123. }
  124. },
  125. 'When cors is enabled': {
  126. topic: function () {
  127. var server = httpServer.createServer({
  128. root: root,
  129. cors: true,
  130. corsHeaders: 'X-Test'
  131. });
  132. server.listen(8082);
  133. this.callback(null, server);
  134. },
  135. 'and given OPTIONS request': {
  136. topic: function () {
  137. request({
  138. method: 'OPTIONS',
  139. uri: 'http://127.0.0.1:8082/',
  140. headers: {
  141. 'Access-Control-Request-Method': 'GET',
  142. Origin: 'http://example.com',
  143. 'Access-Control-Request-Headers': 'Foobar'
  144. }
  145. }, this.callback);
  146. },
  147. 'status code should be 204': function (err, res) {
  148. assert.equal(res.statusCode, 204);
  149. },
  150. 'response Access-Control-Allow-Headers should contain X-Test': function (err, res) {
  151. assert.ok(res.headers['access-control-allow-headers'].split(/\s*,\s*/g).indexOf('X-Test') >= 0, 204);
  152. }
  153. }
  154. }
  155. }).export(module);