|
| 1 | +/* SPDX-FileCopyrightText: 2024 Greenbone AG |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import {describe, test, expect, testing, beforeEach} from '@gsa/testing'; |
| 7 | + |
| 8 | +import Http from 'gmp/http/http'; |
| 9 | +import Rejection from '../rejection'; |
| 10 | +import {vi} from 'vitest'; |
| 11 | + |
| 12 | +const mockGetFeedAccessStatusMessage = testing.fn(); |
| 13 | +const mockFindActionInXMLString = testing.fn(); |
| 14 | + |
| 15 | +vi.mock('gmp/http/utils', async () => { |
| 16 | + return { |
| 17 | + getFeedAccessStatusMessage: () => mockGetFeedAccessStatusMessage(), |
| 18 | + findActionInXMLString: () => mockFindActionInXMLString(), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +global.XMLHttpRequest = testing.fn(() => ({ |
| 23 | + open: testing.fn(), |
| 24 | + send: testing.fn(), |
| 25 | + setRequestHeader: testing.fn(), |
| 26 | + status: 0, |
| 27 | + responseText: '', |
| 28 | + onreadystatechange: null, |
| 29 | + readyState: 0, |
| 30 | +})); |
| 31 | + |
| 32 | +describe('Http', () => { |
| 33 | + describe('handleResponseError', () => { |
| 34 | + let instance; |
| 35 | + let reject; |
| 36 | + let resolve; |
| 37 | + let xhr; |
| 38 | + let options; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + instance = new Http(); |
| 42 | + resolve = testing.fn(); |
| 43 | + reject = testing.fn(); |
| 44 | + xhr = {status: 500}; |
| 45 | + options = {}; |
| 46 | + testing.clearAllMocks(); |
| 47 | + }); |
| 48 | + test('should handle response error without error handlers', async () => { |
| 49 | + await instance.handleResponseError(xhr, reject, resolve, options); |
| 50 | + expect(reject).toHaveBeenCalledWith(expect.any(Rejection)); |
| 51 | + }); |
| 52 | + |
| 53 | + test('401 error should call error handler', async () => { |
| 54 | + xhr.status = 401; |
| 55 | + await instance.handleResponseError(resolve, reject, xhr, options); |
| 56 | + expect(reject).toHaveBeenCalledWith(expect.any(Rejection)); |
| 57 | + expect(reject.mock.calls[0][0].reason).toBe( |
| 58 | + Rejection.REASON_UNAUTHORIZED, |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test('404 error should append additional message', async () => { |
| 63 | + xhr.status = 404; |
| 64 | + const additionalMessage = 'Additional feed access status message'; |
| 65 | + mockGetFeedAccessStatusMessage.mockResolvedValue(additionalMessage); |
| 66 | + mockFindActionInXMLString.mockReturnValue(true); |
| 67 | + |
| 68 | + await instance.handleResponseError(resolve, reject, xhr, options); |
| 69 | + expect(mockGetFeedAccessStatusMessage).toHaveBeenCalled(); |
| 70 | + |
| 71 | + expect(reject).toHaveBeenCalledWith(expect.any(Rejection)); |
| 72 | + const rejectedResponse = reject.mock.calls[0][0]; |
| 73 | + expect(rejectedResponse.message).toContain(additionalMessage); |
| 74 | + }); |
| 75 | + |
| 76 | + test('404 error should not append additional message', async () => { |
| 77 | + xhr.status = 404; |
| 78 | + mockFindActionInXMLString.mockReturnValue(false); |
| 79 | + |
| 80 | + await instance.handleResponseError(resolve, reject, xhr, options); |
| 81 | + expect(mockGetFeedAccessStatusMessage).not.toHaveBeenCalled(); |
| 82 | + |
| 83 | + expect(reject).toHaveBeenCalledWith(expect.any(Rejection)); |
| 84 | + const rejectedResponse = reject.mock.calls[0][0]; |
| 85 | + expect(rejectedResponse.message).toContain('Unknown Error'); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments