Skip to content

Commit

Permalink
Fix the wp_http_supports( array( 'ssl' ) ) function (#819)
Browse files Browse the repository at this point in the history
Gets the `wp_http_supports()` function to return true when the `fetch()`
http transport is available.

## Rationale

`wp_http_supports` checks whether an HTTP tranport with the requests
capabilities is available. However, it does so using the
`_get_first_available_transport()` function which sources its knowledge
about HTTP transport from the `http_api_transports` filter. Therefore,
we must hook into that filter and also rename the
`Requests_Transport_Fetch` class to use the class name pattern expected
by `_get_first_available_transport()`, which is `Wp_Http_*`

## Testing instructions:

This PR ships a new E2E test – confirm it passes.

## Follow-up work

Turn the e2e test into a unit test once WordPress data modules are
easily available for import.
  • Loading branch information
adamziel authored Nov 29, 2023
1 parent 4c95c51 commit 7e1dee2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { updateFile } from '../common';
import { defineWpConfigConsts } from '../define-wp-config-consts';

/** @ts-ignore */
import transportFetch from './wp-content/mu-plugins/playground-includes/requests_transport_fetch.php?raw';
import transportFetch from './wp-content/mu-plugins/playground-includes/wp_http_fetch.php?raw';
/** @ts-ignore */
import transportDummy from './wp-content/mu-plugins/playground-includes/requests_transport_dummy.php?raw';
/** @ts-ignore */
Expand Down Expand Up @@ -186,7 +186,7 @@ class WordPressPatcher {

// Add fetch and dummy transports for HTTP requests
await this.php.writeFile(
`${this.wordpressPath}/wp-content/mu-plugins/playground-includes/requests_transport_fetch.php`,
`${this.wordpressPath}/wp-content/mu-plugins/playground-includes/wp_http_fetch.php`,
transportFetch
);
await this.php.mkdir(`${this.wordpressPath}/wp-content/fonts`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,24 @@ function () {
*
* This mu-plugin provides that transport. It's one of the two:
*
* * Requests_Transport_Fetch – Sends requests using browser's fetch() function.
* Only enabled when PHP was compiled with the VRZNO
* extension.
* * WP_Http_Fetch – Sends requests using browser's fetch() function.
* Only enabled when PHP was compiled with the VRZNO
* extension.
* * Requests_Transport_Dummy – Does not send any requests and only exists to keep
* the Requests class happy.
*/
if (defined('USE_FETCH_FOR_REQUESTS') && USE_FETCH_FOR_REQUESTS) {
require(__DIR__ . '/playground-includes/requests_transport_fetch.php');
Requests::add_transport('Requests_Transport_Fetch');
require(__DIR__ . '/playground-includes/wp_http_fetch.php');
Requests::add_transport('WP_Http_Fetch');

/**
* Add Fetch transport to the list of transports that WordPress
* will test for in the _get_first_available_transport() function.
*/
add_filter('http_api_transports', function ($transports) {
$transports[] = 'Fetch';
return $transports;
});
/**
* Disable signature verification as it doesn't seem to work with
* fetch requests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
* This file isn't actually used. It's just here for reference and development. The actual
* PHP code used in WordPress is hardcoded copy residing in wordpress.mjs in the _patchWordPressCode
* function.
*
* The reason for calling it Wp_Http_Fetch and not something more natural like
* Requests_Transport_Fetch is the _get_first_available_transport(). It checks for
* a class named "Wp_Http_" . $transport_name – which means we must adhere to this
* hardcoded pattern.
*/
class Requests_Transport_Fetch implements Requests_Transport
class Wp_Http_Fetch implements Requests_Transport
{
public $headers = '';

Expand Down
28 changes: 27 additions & 1 deletion packages/playground/website/cypress/e2e/app.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ describe('Query API', () => {
.find('.plugin-card')
.should('have.length.above', 4);
});

/**
* @see https://github.com/WordPress/wordpress-playground/pull/819
* @TODO: Turn this into a unit test once WordPress modules are available
* for import.
*/
it('should return true from wp_http_supports(array( "ssl" ))', () => {
const blueprint = {
landingPage: '/test.php',
features: {
networking: true,
},
steps: [
{
step: 'writeFile',
path: '/wordpress/test.php',
data: `<?php
require("/wordpress/wp-load.php");
echo wp_http_supports(array( "ssl" )) ? "true" : "false";
`,
},
],
};
cy.visit('/#' + JSON.stringify(blueprint));
cy.wordPressDocument().its('body').should('contain', 'true');
});
});

describe('option `plugin`', () => {
Expand Down Expand Up @@ -211,6 +237,7 @@ if (!Cypress.env('CI')) {
.should('contain', 'Cypress tests – site title');

// Download a zip file
const downloadsFolder = Cypress.config('downloadsFolder');
cy.get('[data-cy="dropdown-menu"]').click();
cy.get('[data-cy="download-as-zip"]').click();
cy.readFile(downloadsFolder + '/wordpress-playground.zip').should(
Expand All @@ -229,7 +256,6 @@ if (!Cypress.env('CI')) {
// Import the zip file
cy.get('[data-cy="dropdown-menu"]').click();
cy.get('[data-cy="restore-from-zip"]').click();
const downloadsFolder = Cypress.config('downloadsFolder');
cy.get('#import-select-file').selectFile(
`${downloadsFolder}/wordpress-playground.zip`
);
Expand Down

0 comments on commit 7e1dee2

Please sign in to comment.