|
| 1 | +import { isNtlmAuthHeader, handleNtlmRedirect } from './ntlm'; |
| 2 | + |
| 3 | +describe('isNtlmAuthHeader', () => { |
| 4 | + it.each(['NTLM TlRMTVNTUAAD', 'ntlm TlRMTVNTUAAD', ' NTLM TlRMTVNTUAAD'])( |
| 5 | + 'recognises %s as a credential bound to the connection it was negotiated on', |
| 6 | + (header) => { |
| 7 | + expect(isNtlmAuthHeader(header)).toBe(true); |
| 8 | + } |
| 9 | + ); |
| 10 | + |
| 11 | + it.each(['Bearer abc', 'Basic dXNlcjpwYXNz', 'Negotiate YIIF', 'AWS4-HMAC-SHA256 Credential=abc', 'NTLMish token', '', undefined, null, 42])( |
| 12 | + 'leaves %s alone', |
| 13 | + (header) => { |
| 14 | + expect(isNtlmAuthHeader(header)).toBe(false); |
| 15 | + } |
| 16 | + ); |
| 17 | +}); |
| 18 | + |
| 19 | +describe('handleNtlmRedirect', () => { |
| 20 | + const ntlmHeaders = { 'Authorization': 'NTLM TlRMTVNTUAAD', 'X-retry': 'false', 'Accept': '*/*' }; |
| 21 | + |
| 22 | + it('drops the finished message, X-retry and the adapter when the redirect stays on the origin', () => { |
| 23 | + const requestConfig = { headers: { ...ntlmHeaders }, adapter: 'ntlm' }; |
| 24 | + |
| 25 | + handleNtlmRedirect(requestConfig, 'https://a.test/x', 'https://a.test/y', false); |
| 26 | + |
| 27 | + expect(requestConfig.headers).toEqual({ Accept: '*/*' }); |
| 28 | + expect(requestConfig.adapter).toBeUndefined(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('drops the finished message but keeps the adapter when the redirect goes to another host', () => { |
| 32 | + const requestConfig = { headers: { ...ntlmHeaders }, adapter: 'ntlm' }; |
| 33 | + |
| 34 | + handleNtlmRedirect(requestConfig, 'https://a.test/x', 'https://b.test/y', false); |
| 35 | + |
| 36 | + expect(requestConfig.headers).toEqual({ Accept: '*/*' }); |
| 37 | + expect(requestConfig.adapter).toBe('ntlm'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('drops the adapter for another host too when the request forwards Authorization on redirect', () => { |
| 41 | + const requestConfig = { headers: { ...ntlmHeaders }, adapter: 'ntlm' }; |
| 42 | + |
| 43 | + handleNtlmRedirect(requestConfig, 'https://a.test/x', 'https://b.test/y', true); |
| 44 | + |
| 45 | + expect(requestConfig.headers).toEqual({ Accept: '*/*' }); |
| 46 | + expect(requestConfig.adapter).toBeUndefined(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('leaves a request that carries any other credential alone', () => { |
| 50 | + const requestConfig = { headers: { 'Authorization': 'Bearer abc', 'X-retry': 'false' }, adapter: 'ntlm' }; |
| 51 | + |
| 52 | + handleNtlmRedirect(requestConfig, 'https://a.test/x', 'https://a.test/y', true); |
| 53 | + |
| 54 | + expect(requestConfig.headers).toEqual({ 'Authorization': 'Bearer abc', 'X-retry': 'false' }); |
| 55 | + expect(requestConfig.adapter).toBe('ntlm'); |
| 56 | + }); |
| 57 | +}); |
0 commit comments