Send HTTP Request

Within unit tests you can easily emulate a HTTP request to the system using the built-in http() method. Below are some examples of this.

Simple Example

Below will simply send a GET request to /test_page and check whether or not the page title is equal to "This is a Test".

public function testSomeRequest(): void
{

    // Send http request
    $this->http('test_page');
    $this->assertPageTitle('This is a Test Page');
}

POST Example

Below sends a POST request to /contact checked the page title contains "Thank You", checks the resulting page contains a certain callout, and ensures the correct entry ended up in the database.

public function testContactPost(): void
{

    // Set post variables
    $post = [
        'from_name' => 'Joohn Doe',
        'from_email' => 'john.doe@example.com',
        'subject' => 'Test Subject',
        'contents' => 'This is my message'
    ];

    // Send request
    $this->http('contact', 'POST', $post):

    // Assertions
    $this->assertPageTitleContains('Thank You');
    $this->assertHasCallout('success', "Thank you, your message has been received.");
    $this->assertHasDbRow("SELECT * FROM contact_messages WHERE email = %s", 'john.doe@example.com');
}