Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2E Utils: Add retry mechanism to the REST API discovery #62331

Merged
merged 11 commits into from
Jun 5, 2024
38 changes: 26 additions & 12 deletions packages/e2e-test-utils-playwright/src/request-utils/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import * as fs from 'fs/promises';
import { dirname } from 'path';
import { expect } from '@playwright/test';
import type { APIRequestContext } from '@playwright/test';

/**
Expand All @@ -22,18 +23,31 @@ function splitRequestsToChunks( requests: BatchRequest[], chunkSize: number ) {
}

async function getAPIRootURL( request: APIRequestContext ) {
// Discover the API root url using link header.
// See https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/#link-header
const response = await request.head( WP_BASE_URL );
const links = response.headers().link;
const restLink = links?.match( /<([^>]+)>; rel="https:\/\/api\.w\.org\/"/ );

if ( ! restLink ) {
throw new Error( `Failed to discover REST API endpoint.
Link header: ${ links }` );
}

const [ , rootURL ] = restLink;
let restLink: unknown = null;

// Retry until the REST API root URL is discovered.
// See https://github.com/WordPress/gutenberg/issues/61627
await expect
.poll(
async () => {
// Discover the API root url using link header.
// See https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/#link-header
const response = await request.head( WP_BASE_URL );
const links = response.headers().link;
restLink = links?.match(
/<([^>]+)>; rel="https:\/\/api\.w\.org\/"/
);

return restLink;
},
{
message: 'Failed to discover REST API endpoint.',
timeout: 120_000, // 2 minutes.
}
)
.not.toBeNull();

const [ , rootURL ] = restLink as RegExpMatchArray;

return rootURL;
}
Expand Down
Loading