apiTest.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. it('should refuse a query with an invalid key', function(done) {
  14. this.timeout(5000);
  15. request({
  16. method: 'POST',
  17. url: apiUrl + '/runs',
  18. body: {
  19. url: wwwUrl + '/simple-page.html',
  20. waitForResponse: false
  21. },
  22. json: true,
  23. headers: {
  24. 'X-Api-Key': 'invalid'
  25. }
  26. }, function(error, response, body) {
  27. if (!error && response.statusCode === 401) {
  28. done();
  29. } else {
  30. done(error || response.statusCode);
  31. }
  32. });
  33. });
  34. it('should accept a query with a valid key', function(done) {
  35. this.timeout(5000);
  36. request({
  37. method: 'POST',
  38. url: apiUrl + '/runs',
  39. body: {
  40. url: wwwUrl + '/simple-page.html',
  41. waitForResponse: false
  42. },
  43. json: true,
  44. headers: {
  45. 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
  46. }
  47. }, function(error, response, body) {
  48. if (!error && response.statusCode === 200) {
  49. runId = body.runId;
  50. runId.should.be.a('string');
  51. done();
  52. } else {
  53. done(error || response.statusCode);
  54. }
  55. });
  56. });
  57. it('should accept up to 24 anonymous runs to the API', function(done) {
  58. this.timeout(15000);
  59. function launchRun() {
  60. var deferred = Q.defer();
  61. request({
  62. method: 'POST',
  63. url: apiUrl + '/runs',
  64. body: {
  65. url: wwwUrl + '/simple-page.html',
  66. waitForResponse: false
  67. },
  68. json: true
  69. }, function(error, response, body) {
  70. if (error) {
  71. deferred.reject(error);
  72. } else {
  73. deferred.resolve(response, body);
  74. }
  75. });
  76. return deferred.promise;
  77. }
  78. launchRun()
  79. .then(launchRun)
  80. .then(launchRun)
  81. .then(launchRun)
  82. .then(launchRun)
  83. .then(launchRun)
  84. .then(launchRun)
  85. .then(launchRun)
  86. .then(launchRun)
  87. .then(launchRun)
  88. .then(launchRun)
  89. .then(launchRun)
  90. .then(function(response, body) {
  91. // Here should still be ok
  92. response.statusCode.should.equal(200);
  93. launchRun()
  94. .then(launchRun)
  95. .then(launchRun)
  96. .then(launchRun)
  97. .then(launchRun)
  98. .then(launchRun)
  99. .then(launchRun)
  100. .then(launchRun)
  101. .then(launchRun)
  102. .then(launchRun)
  103. .then(launchRun)
  104. .then(launchRun)
  105. .then(launchRun)
  106. .then(launchRun)
  107. .then(launchRun)
  108. .then(function(response, body) {
  109. // It should fail now
  110. response.statusCode.should.equal(429);
  111. done();
  112. })
  113. .fail(function(error) {
  114. done(error);
  115. });
  116. }).fail(function(error) {
  117. done(error);
  118. });
  119. });
  120. });