|
1 | 1 | import nock from 'nock'; |
2 | 2 | import axios from 'axios'; |
| 3 | +import { createLogger } from '@sap-cloud-sdk/util'; |
3 | 4 | import { circuitBreaker, timeout } from '@sap-cloud-sdk/resilience'; |
4 | 5 | import { circuitBreakers } from '@sap-cloud-sdk/resilience/internal'; |
5 | 6 | import { csrf } from './csrf-token-middleware'; |
@@ -250,19 +251,42 @@ describe('CSRF middleware', () => { |
250 | 251 | ).resolves.not.toThrow(); |
251 | 252 | }); |
252 | 253 |
|
253 | | - it('fetches the token with custom method', async () => { |
254 | | - nock(host).get('/some/path/').reply(200, {}, csrfResponseHeaders); |
| 254 | + it('logs a warning when the CSRF token URL has a different host than the request URL', async () => { |
| 255 | + const csrfHost = 'http://other.example.com'; |
| 256 | + nock(csrfHost).head('/csrf').reply(200, {}, csrfResponseHeaders); |
255 | 257 | nock(host).post('/some/path').reply(200, {}); |
256 | | - await expect( |
257 | | - executeHttpRequest( |
258 | | - { url: host }, |
259 | | - { |
260 | | - method: 'POST', |
261 | | - url: 'some/path', |
262 | | - middleware: [csrf({ method: 'GET' })] |
263 | | - }, |
264 | | - { fetchCsrfToken: false } |
265 | | - ) |
266 | | - ).resolves.not.toThrow(); |
| 258 | + const logger = createLogger('csrf-middleware'); |
| 259 | + const warnSpy = jest.spyOn(logger, 'warn'); |
| 260 | + await executeHttpRequest( |
| 261 | + { url: host }, |
| 262 | + { |
| 263 | + method: 'POST', |
| 264 | + url: 'some/path', |
| 265 | + middleware: [csrf({ url: `${csrfHost}/csrf` })] |
| 266 | + }, |
| 267 | + { fetchCsrfToken: false } |
| 268 | + ); |
| 269 | + expect(warnSpy).toHaveBeenCalledWith( |
| 270 | + expect.stringContaining('different host') |
| 271 | + ); |
| 272 | + }); |
| 273 | + |
| 274 | + it('does not log a warning when the CSRF token URL has the same host as the request URL', async () => { |
| 275 | + nock(host).head('/alternative/path').reply(200, {}, csrfResponseHeaders); |
| 276 | + nock(host).post('/some/path').reply(200, {}); |
| 277 | + const logger = createLogger('csrf-middleware'); |
| 278 | + const warnSpy = jest.spyOn(logger, 'warn'); |
| 279 | + await executeHttpRequest( |
| 280 | + { url: host }, |
| 281 | + { |
| 282 | + method: 'POST', |
| 283 | + url: 'some/path', |
| 284 | + middleware: [csrf({ url: `${host}/alternative/path` })] |
| 285 | + }, |
| 286 | + { fetchCsrfToken: false } |
| 287 | + ); |
| 288 | + expect(warnSpy).not.toHaveBeenCalledWith( |
| 289 | + expect.stringContaining('different host') |
| 290 | + ); |
267 | 291 | }); |
268 | 292 | }); |
0 commit comments