time": 100, "end-time": 200, "order-direction": "desc", "results-per-page": 2 })
           .end(function(error, result) {
              expect(error).to.equal(null);
              expect(result.statusCode).to.equal(200);
              done();
            });
        });

        it("should return 400 when page is not integer", function(done) {
          nock(scalingEngineUri)
           .get(/\/v1\/apps\/.+\/scaling_histories/)
           .reply(200, histories);
          request(publicApp)
           .get("/v1/apps/12345/scaling_histories")
           .set("Authorization",theUserToken)
           .query({ "start-time": 100, "end-time": 200, "order-direction": "desc", "page": "not-integer", "results-per-page": 2 })
           .end(function(error, result) {
              expect(error).to.equal(null);
              expect(result.statusCode).to.equal(400);
              expect(result.body).to.deep.equal({
                "error": "page must be an integer"
              });
              done();
            });
        });
      });

      context("results-per-page", function() {
        it("should return 200 when results-per-page is not provided", function(done) {
          nock(scalingEngineUri)
           .get(/\/v1\/apps\/.+\/scaling_histories/)
           .reply(200, histories);
          request(publicApp)
           .get("/v1/apps/12345/scaling_histories")
           .set("Authorization",theUserToken)
           .query({ "start-time": 100, "end-time": 200, "order-direction": "desc", "page": 1 })
           .end(function(error, result) {
              expect(error).to.equal(null);
              expect(result.statusCode).to.equal(200);

              done();
            });
        });

        it("should return 400 when results-per-page is not integer", function(done) {
          nock(scalingEngineUri)
           .get(/\/v1\/apps\/.+\/scaling_histories/)
           .reply(200, histories);
          request(publicApp)
           .get("/v1/apps/12345/scaling_histories")
           .set("Authorization",theUserToken)
           .query({ "start-time": 100, "end-time": 200, "order-direction": "desc", "page": 1, "results-per-page": "not-integer" })
           .end(function(error, result) {
              expect(error).to.equal(null);
              expect(result.statusCode).to.equal(400);
              expect(result.body).to.deep.equal({
                "error": "results-per-page must be an integer"
              });
              done();
            });
        });
      });
    });
    context("scalingEngine error", function() {
      it("should return 500 when there is error when requesting to scalingEngine", function(done) {
        nock(scalingEngineUri)
         .get(/\/v1\/apps\/.+\/scaling_histories/)
         .replyWithError({
           'message': 'Error in requests scalingEngine',
            'details': 'fake body'
          });
        request(publicApp)
         .get("/v1/apps/12345/scaling_histories")
         .set("Authorization",theUserToken)
         .query({ "start-time": 100, "end-time": 200, "order-direction": "desc", "page": 1, "results-per-page": 2 })
         .end(function(error, result) {
            expect(error).to.equal(null);
            expect(result.statusCode).to.equal(500);
            expect(result.body).to.deep.equal({
              error: 'Error in requests scalingEngine'
            });
            done();
          });
      });

      it('should return 500 when there is internal error in scalingEngine', function(done) {
        nock(scalingEngineUri)
         .get(/\/v1\/apps\/.+\/scaling_histories/)
         .reply(500, { code: 'Interal-Server-Error', message: 'Error getting scaling histories from database' });
        request(publicApp)
         .get("/v1/apps/12345/scaling_histories")
         .set("Authorization",theUserToken)
         .query({ "start-time": 100, "end-time": 200, "order-direction": "desc", "page": 1, "results-per-page": 2 })
         .end(function(error, result) {
            expect(error).to.equal(null);
            expect(result.statusCode).to.equal(500);
            expect(result.body).to.deep.equal({
              error: 'Error getting scaling histories from database'
            });
            done();
          });
      });
    });

    context("get scaling histories", function() {
      it("get the 1st page", function(done) {
        nock(scalingEngineUri)
         .get(/\/v1\/apps\/.+\/scaling_histories/)
         .reply(200, histories);
        request(publicApp)
         .get("/v1/apps/12345/scaling_histories")
         .set("Authorization",theUserToken)
         .query({ "start-time": 100, "end-time": 500, "order-direction": "desc", "page": 1, "results-per-page": 2 })
         .end(function(error, result) {
            expect(error).to.equal(null);
            expect(result.statusCode).to.equal(200);
            expect(result.body).to.deep.equal({
              total_results: 5,
              total_pages: 3,
              page: 1,
              prev_url: null,
              next_url: "/v1/apps/12345/scaling_histories?start-time=100&end-time=500&order-direction=desc&page=2&results-per-page=2",
              resources: histories.slice(0, 2)
            });
            done();
          });
      });

      it("get the 2nd page", function(done) {
        nock(scalingEngineUri)
         .get(/\/v1\/apps\/.+\/scaling_histories/)
         .reply(200, histories);
        request(publicApp)
         .get("/v1/apps/12345/scaling_histories")
         .set("Authorization",theUserToken)
         .query({ "start-time": 100, "end-time": 500, "order-direction": "desc", "page": 2, "results-per-page": 2 })
         .end(function(error, result) {
            expect(error).to.equal(null);
            expect(result.statusCode).to.equal(200);
            expect(result.body).to.deep.equal({
              total_results: 5,
              total_pages: 3,
              page: 2,
              prev_url: "/v1/apps/12345/scaling_histories?start-time=100&end-time=500&order-direction=desc&page=1&results-per-page=2",
              next_url: "/v1/apps/12345/scaling_histories?start-time=100&end-time=500&order-direction=desc&page=3&results-per-page=2",
              resources: histories.slice(2, 4)
            });
            done();
          });
      });

      it("get the 3rd page and only has one record", function(done) {
        nock(scalingEngineUri)
         .get(/\/v1\/apps\/.+\/scaling_histories/)
         .reply(200, histories);
        request(publicApp)
         .get("/v1/apps/12345/scaling_histories")
         .set("Authorization",theUserToken)
         .query({ "start-time": 100, "end-time": 500, "order-direction": "desc", "page": 3, "results-per-page": 2 })
         .end(function(error, result) {
            expect(error).to.equal(null);
            expect(result.statusCode).to.equal(200);
            expect(result.body).to.deep.equal({
              total_results: 5,
              total_pages: 3,
              page: 3,
              prev_url: "/v1/apps/12345/scaling