Coder Social home page Coder Social logo

wp-http's Introduction

WordPress HTTP

WordPress HTTP library.

Usage

$response = Http::request( $url, $args );

$data = $response->json();

Testing

Faking Responses

$url = 'https://www.pronamic.nl/wp-json/wp/v2/types/post';

Http::fake( $url, __DIR__ . '/../http/pronamic-nl-wp-json-types-post.http' );

$response = \wp_remote_get( $url );

// or

$response = Http::get( $url );

PHPUnit

<?php

namespace YourNamespace;

use Pronamic\WordPress\Http\Factory;

class YourTest extends \WP_UnitTestCase {
	/**
	 * Setup.
	 */
	public function setUp() {
		parent::setUp();

		$this->factory = new Factory();
	}

	/**
	 * Test request.
	 */
	public function test_request() {
		$this->factory->fake( 'http://example.com/', __DIR__ . '/../http/example-com.http' );

		$result = \wp_remote_get( 'http://example.com/' );

		// asserts
	}
}

CR LF

To store fake HTTP responses in *.http files and Git, keep the following in mind:

HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements

https://tools.ietf.org/html/rfc2616#section-2.2

.gitattributes

*.http text eol=crlf

You can use a tool like unix2dos to convert the line endings to CR LF:

unix2dos *.http

If the line endings are not correct this can result in the following error:

Undefined offset: 2

wordpress/wp-includes/class-http.php:732
src/Factory.php:97
wordpress/wp-includes/class-wp-hook.php:292
wordpress/wp-includes/plugin.php:212
wordpress/wp-includes/class-http.php:257
wordpress/wp-includes/class-http.php:626
wordpress/wp-includes/http.php:162
src/Facades/Http.php:71

Inspiration

Pronamic - Work with us

wp-http's People

Contributors

remcotolsma avatar rvdsteege avatar

Stargazers

Rihards Mantejs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.