Skip to content

Commit 806ca33

Browse files
committed
WIP: try capturing cookie during redirect explicitly
1 parent 4f09dd8 commit 806ca33

1 file changed

Lines changed: 101 additions & 14 deletions

File tree

serverless/lib/NameSearchProcessor.ts

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -366,24 +366,106 @@ export async function fetchCasesByName(
366366
}
367367

368368
console.log("Posting smart search");
369-
const searchResponse = await client.post(
370-
`${portalUrl}/Portal/SmartSearch/SmartSearch/SmartSearch`,
371-
searchFormData
372-
);
373369

374-
if (searchResponse.status !== 200) {
375-
const errorMessage = `Search request failed with status ${searchResponse.status}`;
370+
// Create a separate Axios instance that doesn't auto-follow redirects
371+
// so we can capture cookies from the initial 302 response
372+
const noRedirectClient = wrapper(axios).create({
373+
timeout: 20000,
374+
maxRedirects: 0, // Don't follow redirects automatically
375+
validateStatus: status => true, // Accept any status code
376+
jar: cookieJar,
377+
withCredentials: true,
378+
headers: {
379+
...PortalAuthenticator.getDefaultRequestHeaders(userAgent),
380+
Origin: portalUrl,
381+
'Content-Type': 'application/x-www-form-urlencoded',
382+
},
383+
});
384+
385+
// Step 1a: Make initial POST request and handle the 302
386+
try {
387+
const initialResponse = await noRedirectClient.post(
388+
`${portalUrl}/Portal/SmartSearch/SmartSearch/SmartSearch`,
389+
searchFormData
390+
);
391+
392+
console.log(`Initial response status: ${initialResponse.status}`);
393+
394+
// Log and handle set-cookie headers from the 302 redirect response
395+
if (initialResponse.headers && initialResponse.headers['set-cookie']) {
396+
console.log('Found Set-Cookie headers in 302 response:');
397+
console.log(JSON.stringify(initialResponse.headers['set-cookie'], null, 2));
398+
399+
// Ensure the SmartSearchCriteria cookie is in the jar
400+
const setCookieHeaders = initialResponse.headers['set-cookie'];
401+
if (Array.isArray(setCookieHeaders)) {
402+
const smartSearchCookie = setCookieHeaders.find(c => c.includes('SmartSearchCriteria='));
403+
if (smartSearchCookie) {
404+
console.log(`Found SmartSearchCriteria in redirect: ${smartSearchCookie}`);
405+
}
406+
}
407+
}
408+
409+
// Check location header to follow redirect manually if needed
410+
if (initialResponse.status === 302 && initialResponse.headers.location) {
411+
console.log(`Following redirect to: ${initialResponse.headers.location}`);
412+
413+
// Now follow the redirect with the normal client (which uses the same cookie jar)
414+
const redirectUrl = new URL(initialResponse.headers.location, portalUrl).toString();
415+
const searchResponse = await client.get(redirectUrl);
416+
417+
// Continue with normal flow using the redirected response
418+
if (searchResponse.status !== 200) {
419+
const errorMessage = `Search request (after redirect) failed with status ${searchResponse.status}`;
420+
421+
await AlertService.logError(
422+
Severity.ERROR,
423+
AlertCategory.PORTAL,
424+
'Name search request failed after redirect',
425+
new Error(errorMessage),
426+
{
427+
name,
428+
statusCode: searchResponse.status,
429+
resource: 'portal-search',
430+
}
431+
);
432+
433+
return {
434+
cases: [],
435+
error: errorMessage,
436+
};
437+
}
438+
} else if (initialResponse.status !== 200) {
439+
// Handle non-redirect error
440+
const errorMessage = `Search request failed with status ${initialResponse.status}`;
441+
442+
await AlertService.logError(
443+
Severity.ERROR,
444+
AlertCategory.PORTAL,
445+
'Name search request failed',
446+
new Error(errorMessage),
447+
{
448+
name,
449+
statusCode: initialResponse.status,
450+
resource: 'portal-search',
451+
}
452+
);
453+
454+
return {
455+
cases: [],
456+
error: errorMessage,
457+
};
458+
}
459+
} catch (redirectError) {
460+
console.error("Error during smart search with redirect handling:", redirectError);
461+
const errorMessage = `Search request error: ${(redirectError as Error).message}`;
376462

377463
await AlertService.logError(
378464
Severity.ERROR,
379465
AlertCategory.PORTAL,
380-
'Name search request failed',
381-
new Error(errorMessage),
382-
{
383-
name,
384-
statusCode: searchResponse.status,
385-
resource: 'portal-search',
386-
}
466+
'Name search request failed with exception',
467+
redirectError as Error,
468+
{ name, resource: 'portal-search' }
387469
);
388470

389471
return {
@@ -392,16 +474,21 @@ export async function fetchCasesByName(
392474
};
393475
}
394476

395-
// Check if cookies were actually added to the jar
477+
// Check if cookies were actually added to the jar after redirect handling
396478
const cookies = cookieJar.getCookiesSync(`${portalUrl}/Portal`);
397479
console.log(`Cookie jar after SmartSearch request contains ${cookies.length} cookies:`);
398480
cookies.forEach(cookie => {
399481
console.log(`- ${cookie.key}=${cookie.value} (domain=${cookie.domain}, path=${cookie.path})`);
400482
});
401483

484+
// Check specifically for SmartSearchCriteria cookie
485+
const hasSmartSearchCookie = cookies.some(c => c.key === 'SmartSearchCriteria');
486+
console.log(`Has SmartSearchCriteria cookie: ${hasSmartSearchCookie}`);
487+
402488
// Step 2: Get the search results page
403489
console.log("Getting smart search results");
404490

491+
// Continue with the rest of the flow using the same client (with updated cookie jar)
405492
const resultsResponse = await client.get(
406493
`${portalUrl}/Portal/SmartSearch/SmartSearchResults`,
407494
);

0 commit comments

Comments
 (0)