apiTest.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var should = require('chai').should();
  2. var request = require('request');
  3. var Q = require('q');
  4. var config = {
  5. "authorizedKeys": {
  6. "1234567890": "contact@gaelmetais.com"
  7. }
  8. };
  9. var apiUrl = 'http://localhost:8387/api';
  10. var wwwUrl = 'http://localhost:8388';
  11. describe('api', function() {
  12. var runId;
  13. var apiServer;
  14. before(function(done) {
  15. apiServer = require('../../bin/server.js');
  16. apiServer.startTests = done;
  17. });
  18. it('should refuse a query with an invalid key', function(done) {
  19. this.timeout(5000);
  20. request({
  21. method: 'POST',
  22. url: apiUrl + '/runs',
  23. body: {
  24. url: wwwUrl + '/simple-page.html',
  25. waitForResponse: false
  26. },
  27. json: true,
  28. headers: {
  29. 'X-Api-Key': 'invalid'
  30. }
  31. }, function(error, response, body) {
  32. if (!error && response.statusCode === 401) {
  33. done();
  34. } else {
  35. done(error || response.statusCode);
  36. }
  37. });
  38. });
  39. it('should accept a query with a valid key', function(done) {
  40. this.timeout(5000);
  41. request({
  42. method: 'POST',
  43. url: apiUrl + '/runs',
  44. body: {
  45. url: wwwUrl + '/simple-page.html',
  46. waitForResponse: false
  47. },
  48. json: true,
  49. headers: {
  50. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  51. }
  52. }, function(error, response, body) {
  53. if (!error && response.statusCode === 200) {
  54. runId = body.runId;
  55. runId.should.be.a('string');
  56. done();
  57. } else {
  58. done(error || response.statusCode);
  59. }
  60. });
  61. });
  62. it('should accept up to 10 anonymous runs to the API', function(done) {
  63. this.timeout(5000);
  64. function launchRun() {
  65. var deferred = Q.defer();
  66. request({
  67. method: 'POST',
  68. url: apiUrl + '/runs',
  69. body: {
  70. url: wwwUrl + '/simple-page.html',
  71. waitForResponse: false
  72. },
  73. json: true
  74. }, function(error, response, body) {
  75. if (error) {
  76. deferred.reject(error);
  77. } else {
  78. deferred.resolve(response, body);
  79. }
  80. });
  81. return deferred.promise;
  82. }
  83. launchRun()
  84. .then(launchRun)
  85. .then(launchRun)
  86. .then(launchRun)
  87. .then(launchRun)
  88. .then(function(response, body) {
  89. // Here should still be ok
  90. response.statusCode.should.equal(200);
  91. launchRun()
  92. .then(launchRun)
  93. .then(launchRun)
  94. .then(launchRun)
  95. .then(launchRun)
  96. .then(launchRun)
  97. .then(function(response, body) {
  98. // It should fail now
  99. response.statusCode.should.equal(429);
  100. done();
  101. })
  102. .fail(function(error) {
  103. done(error);
  104. });
  105. }).fail(function(error) {
  106. done(error);
  107. });
  108. });
  109. after(function() {
  110. console.log('Closing the server');
  111. apiServer.close();
  112. });
  113. });