|
1 | 1 | const RedisCacheAdapter = require('../lib/Adapters/Cache/RedisCacheAdapter').default; |
| 2 | +const request = require('../lib/request'); |
| 3 | + |
| 4 | +const headers = { |
| 5 | + 'Content-Type': 'application/json', |
| 6 | + 'X-Parse-Application-Id': 'test', |
| 7 | + 'X-Parse-REST-API-Key': 'rest', |
| 8 | +}; |
| 9 | + |
2 | 10 | describe('rate limit', () => { |
3 | 11 | it('can limit cloud functions', async () => { |
4 | 12 | Parse.Cloud.define('test', () => 'Abc'); |
@@ -487,6 +495,125 @@ describe('rate limit', () => { |
487 | 495 | }) |
488 | 496 | ).toBeRejectedWith(`Invalid rate limit option "path"`); |
489 | 497 | }); |
| 498 | + describe('batch', () => { |
| 499 | + it('should reject batch request when sub-requests exceed rate limit for a path', async () => { |
| 500 | + await reconfigureServer({ |
| 501 | + rateLimit: [ |
| 502 | + { |
| 503 | + requestPath: '/classes/*path', |
| 504 | + requestTimeWindow: 10000, |
| 505 | + requestCount: 2, |
| 506 | + errorResponseMessage: 'Too many requests', |
| 507 | + includeInternalRequests: true, |
| 508 | + }, |
| 509 | + ], |
| 510 | + }); |
| 511 | + const response = await request({ |
| 512 | + method: 'POST', |
| 513 | + headers: headers, |
| 514 | + url: 'http://localhost:8378/1/batch', |
| 515 | + body: JSON.stringify({ |
| 516 | + requests: [ |
| 517 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value1' } }, |
| 518 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value2' } }, |
| 519 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value3' } }, |
| 520 | + ], |
| 521 | + }), |
| 522 | + }).catch(e => e); |
| 523 | + expect(response.data).toEqual({ |
| 524 | + code: Parse.Error.CONNECTION_FAILED, |
| 525 | + error: 'Batch request exceeds rate limit for endpoint', |
| 526 | + }); |
| 527 | + }); |
| 528 | + |
| 529 | + it('should allow batch request when sub-requests are within rate limit', async () => { |
| 530 | + await reconfigureServer({ |
| 531 | + rateLimit: [ |
| 532 | + { |
| 533 | + requestPath: '/classes/*path', |
| 534 | + requestTimeWindow: 10000, |
| 535 | + requestCount: 5, |
| 536 | + errorResponseMessage: 'Too many requests', |
| 537 | + includeInternalRequests: true, |
| 538 | + }, |
| 539 | + ], |
| 540 | + }); |
| 541 | + const response = await request({ |
| 542 | + method: 'POST', |
| 543 | + headers: headers, |
| 544 | + url: 'http://localhost:8378/1/batch', |
| 545 | + body: JSON.stringify({ |
| 546 | + requests: [ |
| 547 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value1' } }, |
| 548 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value2' } }, |
| 549 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value3' } }, |
| 550 | + ], |
| 551 | + }), |
| 552 | + }); |
| 553 | + expect(response.data.length).toBe(3); |
| 554 | + expect(response.data[0].success).toBeDefined(); |
| 555 | + }); |
| 556 | + |
| 557 | + it('should reject batch when sub-requests for one rate-limited path exceed limit among mixed paths', async () => { |
| 558 | + await reconfigureServer({ |
| 559 | + rateLimit: [ |
| 560 | + { |
| 561 | + requestPath: '/login', |
| 562 | + requestTimeWindow: 10000, |
| 563 | + requestCount: 1, |
| 564 | + errorResponseMessage: 'Too many login requests', |
| 565 | + includeInternalRequests: true, |
| 566 | + }, |
| 567 | + ], |
| 568 | + }); |
| 569 | + await Parse.User.signUp('testuser', 'password'); |
| 570 | + const response = await request({ |
| 571 | + method: 'POST', |
| 572 | + headers: headers, |
| 573 | + url: 'http://localhost:8378/1/batch', |
| 574 | + body: JSON.stringify({ |
| 575 | + requests: [ |
| 576 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value1' } }, |
| 577 | + { method: 'POST', path: '/1/login', body: { username: 'testuser', password: 'password' } }, |
| 578 | + { method: 'POST', path: '/1/login', body: { username: 'testuser', password: 'wrong' } }, |
| 579 | + ], |
| 580 | + }), |
| 581 | + }).catch(e => e); |
| 582 | + expect(response.data).toEqual({ |
| 583 | + code: Parse.Error.CONNECTION_FAILED, |
| 584 | + error: 'Batch request exceeds rate limit for endpoint', |
| 585 | + }); |
| 586 | + }); |
| 587 | + |
| 588 | + it('should not reject batch when sub-requests target non-rate-limited paths', async () => { |
| 589 | + await reconfigureServer({ |
| 590 | + rateLimit: [ |
| 591 | + { |
| 592 | + requestPath: '/login', |
| 593 | + requestTimeWindow: 10000, |
| 594 | + requestCount: 1, |
| 595 | + errorResponseMessage: 'Too many login requests', |
| 596 | + includeInternalRequests: true, |
| 597 | + }, |
| 598 | + ], |
| 599 | + }); |
| 600 | + const response = await request({ |
| 601 | + method: 'POST', |
| 602 | + headers: headers, |
| 603 | + url: 'http://localhost:8378/1/batch', |
| 604 | + body: JSON.stringify({ |
| 605 | + requests: [ |
| 606 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value1' } }, |
| 607 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value2' } }, |
| 608 | + { method: 'POST', path: '/1/classes/MyObject', body: { key: 'value3' } }, |
| 609 | + ], |
| 610 | + }), |
| 611 | + }); |
| 612 | + expect(response.data.length).toBe(3); |
| 613 | + expect(response.data[0].success).toBeDefined(); |
| 614 | + }); |
| 615 | + }); |
| 616 | + |
490 | 617 | describe_only(() => { |
491 | 618 | return process.env.PARSE_SERVER_TEST_CACHE === 'redis'; |
492 | 619 | })('with RedisCache', function () { |
|
0 commit comments