Skip to content

Commit

Permalink
add retry mechanism to the REST API discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderBart committed Jun 5, 2024
1 parent 9aec5fe commit d328e03
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 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,34 +23,29 @@ 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\/"/ );

console.log( response.headers() ); // eslint-disable-line no-console

try {
const html = await response.text();
console.log( html ); // eslint-disable-line no-console
} catch ( error ) {
// noop
}

try {
const json = await response.json();
console.log( json ); // eslint-disable-line no-console
} catch ( error ) {
// noop
}

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

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

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: 60_000, // 1 minute.
}
)
.not.toBeFalsy();

const [ , rootURL ] = restLink as RegExpMatchArray;

return rootURL;
}
Expand Down

0 comments on commit d328e03

Please sign in to comment.