|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import { coreMock, httpServerMock, httpServiceMock } from 'src/core/server/mocks'; |
8 | | -import { registerLimitedConcurrencyRoutes } from './limited_concurrency'; |
| 8 | +import { |
| 9 | + createLimitedPreAuthHandler, |
| 10 | + isLimitedRoute, |
| 11 | + registerLimitedConcurrencyRoutes, |
| 12 | +} from './limited_concurrency'; |
9 | 13 | import { IngestManagerConfigType } from '../index'; |
10 | 14 |
|
11 | 15 | describe('registerLimitedConcurrencyRoutes', () => { |
@@ -35,70 +39,184 @@ describe('registerLimitedConcurrencyRoutes', () => { |
35 | 39 | }); |
36 | 40 |
|
37 | 41 | describe('preAuthHandler', () => { |
38 | | - test(`it ignores routes which don't have the correct tag`, async () => { |
39 | | - const routerMock = coreMock.createSetup().http.createRouter(); |
40 | | - const handlerMock = jest.fn(); |
41 | | - routerMock.get( |
42 | | - { |
43 | | - path: '/api/foo', |
44 | | - validate: false, |
45 | | - // options: { tags: ['ingest:limited-concurrency'] }, |
46 | | - }, |
47 | | - handlerMock |
48 | | - ); |
| 42 | + test(`ignores routes when !isMatch`, async () => { |
| 43 | + const mockMaxCounter = { |
| 44 | + increase: jest.fn(), |
| 45 | + decrease: jest.fn(), |
| 46 | + lessThanMax: jest.fn(), |
| 47 | + }; |
| 48 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 49 | + isMatch: jest.fn().mockImplementation(() => false), |
| 50 | + maxCounter: mockMaxCounter, |
| 51 | + }); |
49 | 52 |
|
50 | | - const mockSetup = coreMock.createSetup(); |
51 | | - const mockConfig = { fleet: { maxConcurrentConnections: 1 } } as IngestManagerConfigType; |
52 | | - registerLimitedConcurrencyRoutes(mockSetup, mockConfig); |
| 53 | + const mockRequest = httpServerMock.createKibanaRequest({ |
| 54 | + path: '/no/match', |
| 55 | + }); |
| 56 | + const mockResponse = httpServerMock.createResponseFactory(); |
| 57 | + const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 58 | + |
| 59 | + // @ts-ignore error re: mockPreAuthToolkit return type |
| 60 | + await preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit); |
| 61 | + |
| 62 | + expect(mockMaxCounter.increase).not.toHaveBeenCalled(); |
| 63 | + expect(mockMaxCounter.decrease).not.toHaveBeenCalled(); |
| 64 | + expect(mockMaxCounter.lessThanMax).not.toHaveBeenCalled(); |
| 65 | + expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
| 66 | + }); |
| 67 | + |
| 68 | + test(`ignores routes which don't have the correct tag`, async () => { |
| 69 | + const mockMaxCounter = { |
| 70 | + increase: jest.fn(), |
| 71 | + decrease: jest.fn(), |
| 72 | + lessThanMax: jest.fn(), |
| 73 | + }; |
| 74 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 75 | + isMatch: isLimitedRoute, |
| 76 | + maxCounter: mockMaxCounter, |
| 77 | + }); |
| 78 | + |
| 79 | + const mockRequest = httpServerMock.createKibanaRequest({ |
| 80 | + path: '/no/match', |
| 81 | + }); |
| 82 | + const mockResponse = httpServerMock.createResponseFactory(); |
| 83 | + const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
53 | 84 |
|
54 | | - const [[preAuthHandler]] = mockSetup.http.registerOnPreAuth.mock.calls; |
| 85 | + // @ts-ignore error re: mockPreAuthToolkit return type |
| 86 | + await preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit); |
| 87 | + |
| 88 | + expect(mockMaxCounter.increase).not.toHaveBeenCalled(); |
| 89 | + expect(mockMaxCounter.decrease).not.toHaveBeenCalled(); |
| 90 | + expect(mockMaxCounter.lessThanMax).not.toHaveBeenCalled(); |
| 91 | + expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + test(`processes routes which have the correct tag`, async () => { |
| 95 | + const mockMaxCounter = { |
| 96 | + increase: jest.fn(), |
| 97 | + decrease: jest.fn(), |
| 98 | + lessThanMax: jest.fn().mockImplementation(() => true), |
| 99 | + }; |
| 100 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 101 | + isMatch: isLimitedRoute, |
| 102 | + maxCounter: mockMaxCounter, |
| 103 | + }); |
55 | 104 |
|
56 | 105 | const mockRequest = httpServerMock.createKibanaRequest({ |
57 | | - method: 'get', |
58 | | - path: '/api/foo', |
59 | | - // routeTags: ['ingest:limited-concurrency'], |
| 106 | + path: '/should/match', |
| 107 | + routeTags: ['ingest:limited-concurrency'], |
60 | 108 | }); |
61 | 109 | const mockResponse = httpServerMock.createResponseFactory(); |
62 | 110 | const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
63 | 111 |
|
64 | 112 | // @ts-ignore error re: mockPreAuthToolkit return type |
65 | 113 | await preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit); |
66 | | - const [routeConfig, routeHandler] = routerMock.get.mock.calls[0]; |
67 | | - // @ts-ignore error re: missing iterator |
68 | | - // const [routeConfig, routeHandler] = routerMock.get.mock.calls.find( |
69 | | - // ([{ path }]) => path === '/api/foo' |
70 | | - // ); |
71 | | - console.log({ routeConfig, routeHandler }); |
72 | | - expect(mockResponse.notFound).not.toHaveBeenCalled(); |
| 114 | + |
| 115 | + // will call lessThanMax because isMatch succeeds |
| 116 | + expect(mockMaxCounter.lessThanMax).toHaveBeenCalledTimes(1); |
| 117 | + // will not error because lessThanMax is true |
| 118 | + expect(mockResponse.customError).not.toHaveBeenCalled(); |
73 | 119 | expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
| 120 | + }); |
| 121 | + |
| 122 | + test(`updates the counter when isMatch & lessThanMax`, async () => { |
| 123 | + const mockMaxCounter = { |
| 124 | + increase: jest.fn(), |
| 125 | + decrease: jest.fn(), |
| 126 | + lessThanMax: jest.fn().mockImplementation(() => true), |
| 127 | + }; |
| 128 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 129 | + isMatch: jest.fn().mockImplementation(() => true), |
| 130 | + maxCounter: mockMaxCounter, |
| 131 | + }); |
74 | 132 |
|
75 | | - expect(handlerMock).toHaveBeenCalled(); |
| 133 | + const mockRequest = httpServerMock.createKibanaRequest(); |
| 134 | + const mockResponse = httpServerMock.createResponseFactory(); |
| 135 | + const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 136 | + |
| 137 | + // @ts-ignore error re: mockPreAuthToolkit return type |
| 138 | + await preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit); |
| 139 | + |
| 140 | + expect(mockMaxCounter.increase).toHaveBeenCalled(); |
| 141 | + // expect(mockMaxCounter.decrease).toHaveBeenCalled(); |
| 142 | + expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
76 | 143 | }); |
77 | 144 |
|
78 | | - // test(`it processes routes which have the correct tag`, async () => { |
79 | | - // const mockSetup = coreMock.createSetup(); |
80 | | - // const mockConfig = { fleet: { maxConcurrentConnections: 1 } } as IngestManagerConfigType; |
81 | | - // registerLimitedConcurrencyRoutes(mockSetup, mockConfig); |
82 | | - |
83 | | - // const [[preAuthHandler]] = mockSetup.http.registerOnPreAuth.mock.calls; |
84 | | - |
85 | | - // const mockRequest = httpServerMock.createKibanaRequest({ |
86 | | - // method: 'get', |
87 | | - // path: '/api/foo', |
88 | | - // routeTags: ['ingest:limited-concurrency'], |
89 | | - // }); |
90 | | - // const mockResponse = httpServerMock.createResponseFactory(); |
91 | | - // const mockPreAuthToolkit = httpServiceMock.createOnPreAuthToolkit(); |
92 | | - |
93 | | - // const responses = await Promise.all([ |
94 | | - // preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit), |
95 | | - // preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit), |
96 | | - // preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit), |
97 | | - // preAuthHandler(mockRequest, mockResponse, mockPreAuthToolkit), |
98 | | - // ]); |
99 | | - // console.log('first res', responses[0]); |
100 | | - // console.log('last res', responses[3]); |
101 | | - // expect(mockResponse.notFound).not.toHaveBeenCalled(); |
102 | | - // expect(mockPreAuthToolkit.next).toHaveBeenCalledTimes(1); |
103 | | - // }); |
| 145 | + test(`lessThanMax ? next : error`, async () => { |
| 146 | + const mockMaxCounter = { |
| 147 | + increase: jest.fn(), |
| 148 | + decrease: jest.fn(), |
| 149 | + lessThanMax: jest |
| 150 | + .fn() |
| 151 | + // call 1 |
| 152 | + .mockImplementationOnce(() => true) |
| 153 | + // calls 2, 3, 4 |
| 154 | + .mockImplementationOnce(() => false) |
| 155 | + .mockImplementationOnce(() => false) |
| 156 | + .mockImplementationOnce(() => false) |
| 157 | + // calls 5+ |
| 158 | + .mockImplementationOnce(() => true) |
| 159 | + .mockImplementation(() => true), |
| 160 | + }; |
| 161 | + |
| 162 | + const preAuthHandler = createLimitedPreAuthHandler({ |
| 163 | + isMatch: isLimitedRoute, |
| 164 | + maxCounter: mockMaxCounter, |
| 165 | + }); |
| 166 | + |
| 167 | + function makeRequestExpectNext() { |
| 168 | + const request = httpServerMock.createKibanaRequest({ |
| 169 | + path: '/should/match/', |
| 170 | + routeTags: ['ingest:limited-concurrency'], |
| 171 | + }); |
| 172 | + const response = httpServerMock.createResponseFactory(); |
| 173 | + const toolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 174 | + |
| 175 | + // @ts-ignore error re: mockPreAuthToolkit return type |
| 176 | + preAuthHandler(request, response, toolkit); |
| 177 | + expect(toolkit.next).toHaveBeenCalledTimes(1); |
| 178 | + expect(response.customError).not.toHaveBeenCalled(); |
| 179 | + } |
| 180 | + |
| 181 | + function makeRequestExpectError() { |
| 182 | + const request = httpServerMock.createKibanaRequest({ |
| 183 | + path: '/should/match/', |
| 184 | + routeTags: ['ingest:limited-concurrency'], |
| 185 | + }); |
| 186 | + const response = httpServerMock.createResponseFactory(); |
| 187 | + const toolkit = httpServiceMock.createOnPreAuthToolkit(); |
| 188 | + |
| 189 | + // @ts-ignore error re: mockPreAuthToolkit return type |
| 190 | + preAuthHandler(request, response, toolkit); |
| 191 | + expect(toolkit.next).not.toHaveBeenCalled(); |
| 192 | + expect(response.customError).toHaveBeenCalledTimes(1); |
| 193 | + expect(response.customError).toHaveBeenCalledWith({ |
| 194 | + statusCode: 429, |
| 195 | + body: 'Too Many Requests', |
| 196 | + }); |
| 197 | + } |
| 198 | + |
| 199 | + // request 1 succeeds |
| 200 | + makeRequestExpectNext(); |
| 201 | + expect(mockMaxCounter.increase).toHaveBeenCalledTimes(1); |
| 202 | + // expect(mockMaxCounter.decrease).toHaveBeenCalledTimes(1); |
| 203 | + |
| 204 | + // requests 2, 3, 4 fail |
| 205 | + makeRequestExpectError(); |
| 206 | + makeRequestExpectError(); |
| 207 | + makeRequestExpectError(); |
| 208 | + |
| 209 | + // requests 5+ succeed |
| 210 | + makeRequestExpectNext(); |
| 211 | + expect(mockMaxCounter.increase).toHaveBeenCalledTimes(2); |
| 212 | + // expect(mockMaxCounter.decrease).toHaveBeenCalledTimes(2); |
| 213 | + |
| 214 | + makeRequestExpectNext(); |
| 215 | + expect(mockMaxCounter.increase).toHaveBeenCalledTimes(3); |
| 216 | + // expect(mockMaxCounter.decrease).toHaveBeenCalledTimes(3); |
| 217 | + |
| 218 | + makeRequestExpectNext(); |
| 219 | + expect(mockMaxCounter.increase).toHaveBeenCalledTimes(4); |
| 220 | + // expect(mockMaxCounter.decrease).toHaveBeenCalledTimes(4); |
| 221 | + }); |
104 | 222 | }); |
0 commit comments