123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- var should = require('chai').should();
- var request = require('request');
- var Q = require('q');
- var config = {
- "authorizedKeys": {
- "1234567890": "contact@gaelmetais.com"
- }
- };
- var apiUrl = 'http://localhost:8387/api';
- var wwwUrl = 'http://localhost:8388';
- describe('api', function() {
- var runId;
- it('should refuse a query with an invalid key', function(done) {
- this.timeout(5000);
- request({
- method: 'POST',
- url: apiUrl + '/runs',
- body: {
- url: wwwUrl + '/simple-page.html',
- waitForResponse: false
- },
- json: true,
- headers: {
- 'X-Api-Key': 'invalid'
- }
- }, function(error, response, body) {
- if (!error && response.statusCode === 401) {
- done();
- } else {
- done(error || response.statusCode);
- }
- });
- });
- it('should accept a query with a valid key', function(done) {
- this.timeout(5000);
- request({
- method: 'POST',
- url: apiUrl + '/runs',
- body: {
- url: wwwUrl + '/simple-page.html',
- waitForResponse: false
- },
- json: true,
- headers: {
- 'X-Api-Key': Object.keys(config.authorizedKeys)[0]
- }
- }, function(error, response, body) {
- if (!error && response.statusCode === 200) {
- runId = body.runId;
- runId.should.be.a('string');
- done();
- } else {
- done(error || response.statusCode);
- }
- });
- });
- it('should accept up to 24 anonymous runs to the API', function(done) {
- this.timeout(15000);
- function launchRun() {
- var deferred = Q.defer();
- request({
- method: 'POST',
- url: apiUrl + '/runs',
- body: {
- url: wwwUrl + '/simple-page.html',
- waitForResponse: false
- },
- json: true
- }, function(error, response, body) {
- if (error) {
- deferred.reject(error);
- } else {
- deferred.resolve(response, body);
- }
- });
- return deferred.promise;
- }
- launchRun()
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(function(response, body) {
-
- // Here should still be ok
- response.statusCode.should.equal(200);
- launchRun()
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(launchRun)
- .then(function(response, body) {
- // It should fail now
- response.statusCode.should.equal(429);
- done();
- })
- .fail(function(error) {
- done(error);
- });
- }).fail(function(error) {
- done(error);
- });
-
- });
- });
|