Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libs/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ let middlewares: Middleware[] = [];
const latestApplyAttemptByRequest = new WeakMap<WeakKey, number>();

function makeXHR<TKey extends OnyxKey>(request: Request<TKey>): Promise<Response<TKey> | void> {
const finalParameters = enhanceParameters(request.command, request?.data ?? {});
return hasReadRequiredDataFromStorage().then((): Promise<Response<TKey> | void> => {
// Add the session only once storage has it, otherwise the first request of a launch goes without one
const finalParameters = enhanceParameters(request.command, request?.data ?? {});
Comment thread
allgandalf marked this conversation as resolved.
Resolved
return HttpUtils.xhr(request.command, finalParameters, request.type, request.shouldUseSecure, request.initiatedOffline);
});
}
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/RequestTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import Log from '@src/libs/Log';
import * as Request from '@src/libs/Request';
import type {Middleware} from '@src/libs/Request';
import {endSpan, endSpanWithAttributes, startSpan} from '@src/libs/telemetry/activeSpans';
import type ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';

import type Onyx from 'react-native-onyx';

import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

Expand Down Expand Up @@ -156,3 +159,35 @@ test('Request.processWithMiddleware() keeps startup on the StartupData span name
expect(startedSpanIds.some((spanId) => spanId.startsWith('SearchData.'))).toBe(false);
});
});

test('Request.processWithMiddleware() sends the saved session on a request made as the request code first loads', async () => {
let sentParameters: Record<string, unknown> | undefined;
await jest.isolateModulesAsync(async () => {
jest.doMock('@libs/HttpUtils', () => ({
__esModule: true,
default: {
xhr: (command: string, parameters: Record<string, unknown>) => {
sentParameters = parameters;
return Promise.resolve({jsonCode: 200});
},
cancelPendingRequests: jest.fn(),
processHTTPRequest: jest.fn(),
},
}));

// Given a session saved on the device, read by a fresh Onyx before any network code is loaded
const isolatedOnyx = jest.requireActual<{default: typeof Onyx}>('react-native-onyx').default;
const isolatedOnyxKeys = jest.requireActual<{default: typeof ONYXKEYS}>('@src/ONYXKEYS').default;
isolatedOnyx.init({keys: isolatedOnyxKeys});
await isolatedOnyx.set(isolatedOnyxKeys.SESSION, {authToken: 'savedToken', email: 'member@saml.com', accountID: 1});
await isolatedOnyx.set(isolatedOnyxKeys.CREDENTIALS, {autoGeneratedLogin: 'login', autoGeneratedPassword: 'password'});
await waitForBatchedUpdates();

// When a request is made in the same moment the request code loads, the way the phone app loads code on first use
const isolatedRequest = jest.requireActual<typeof Request>('@src/libs/Request');
await isolatedRequest.processWithMiddleware({command: WRITE_COMMANDS.RECONNECT_APP, data: {}});
});

// Then the request carries the saved session instead of going out without one
expect(sentParameters?.authToken).toBe('savedToken');
});
Loading