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

Tests: new Cookie\FormatTest class #740

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions tests/Cookie/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ public function testBasicCookie() {

$this->assertSame('requests-testcookie', $cookie->name);
$this->assertSame('testvalue', $cookie->value);
$this->assertSame('testvalue', (string) $cookie);

$this->assertSame('requests-testcookie=testvalue', $cookie->format_for_header());
$this->assertSame('requests-testcookie=testvalue', $cookie->format_for_set_cookie());
}

public function testCookieWithAttributes() {
$attributes = [
'httponly',
'path' => '/',
];
$cookie = new Cookie('requests-testcookie', 'testvalue', $attributes);

$this->assertSame('requests-testcookie=testvalue', $cookie->format_for_header());
$this->assertSame('requests-testcookie=testvalue; httponly; path=/', $cookie->format_for_set_cookie());
}

public function testEmptyCookieName() {
Expand Down
144 changes: 144 additions & 0 deletions tests/Cookie/FormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace WpOrg\Requests\Tests\Cookie;

use WpOrg\Requests\Cookie;
use WpOrg\Requests\Tests\TestCase;

/**
* @coversDefaultClass \WpOrg\Requests\Cookie
*/
final class FormatTest extends TestCase {

/**
* Verify a Cookie is stringable.
*
* @covers ::__toString
*
* @return void
*/
public function testStringable() {
$cookie = new Cookie('requests-testcookie', 'testvalue');

$this->assertSame('testvalue', (string) $cookie);
}

/**
* Test formatting a cookie for a Cookie header.
*
* @covers ::format_for_header
*
* @dataProvider dataFormat
*
* @param string $name Cookie name.
* @param string $value Cookie value.
* @param array $expected Expected function return values.
*
* @return void
*/
public function testFormatForHeader($name, $value, $expected) {
$cookie = new Cookie($name, $value);

$this->assertSame($expected, $cookie->format_for_header());
}

/**
* Test formatting a cookie for a Set-Cookie header.
*
* @covers ::format_for_set_cookie
*
* @dataProvider dataFormat
*
* @param string $name Cookie name.
* @param string $value Cookie value.
* @param array $expected Expected function return values.
*
* @return void
*/
public function testFormatForSetCookie($name, $value, $expected) {
$cookie = new Cookie($name, $value);

$this->assertSame($expected, $cookie->format_for_set_cookie());
}

/**
* Data provider.
*
* @return array
*/
public static function dataFormat() {
return [
'Empty key, empty value' => [
'name' => '',
'value' => '',
'expected' => '=',
],
'Empty key, has value' => [
'name' => '',
'value' => 'testvalue',
'expected' => '=testvalue',
],
'Has key and value' => [
'name' => 'requests-testcookie',
'value' => 'testvalue',
'expected' => 'requests-testcookie=testvalue',
],
];
}

/**
* Test formatting a cookie with attributes for a Set-Cookie header.
*
* @covers ::format_for_set_cookie
*
* @dataProvider dataFormatWithAttributes
*
* @param array $attributes Cookie attributes.
* @param array $expected Expected function return values.
*
* @return void
*/
public function testFormatWithAttributes($attributes, $expected) {
// Set the reference time to 2022-01-01 00:00:00.
$reference_time = gmmktime(0, 0, 0, 1, 1, 2022);

$cookie = new Cookie('requests-testcookie', 'testvalue', $attributes, [], $reference_time);

$this->assertSame($expected, $cookie->format_for_set_cookie());
}

/**
* Data provider.
*
* @return array
*/
public static function dataFormatWithAttributes() {
return [
'Empty attributes array' => [
'attributes' => [],
'expected' => 'requests-testcookie=testvalue',
],
'Single attribute with key' => [
'attributes' => [
'domain' => 'example.org',
],
'expected' => 'requests-testcookie=testvalue; domain=example.org',
],
'Single attribute without key' => [
'attributes' => [
'httponly',
],
'expected' => 'requests-testcookie=testvalue; httponly',
],
'Attributes with and without key' => [
'attributes' => [
'domain' => 'example.org',
'httponly',
'path' => '/',
'max-age' => '3600',
],
'expected' => 'requests-testcookie=testvalue; domain=example.org; httponly; path=/; max-age=1640998800',
],
];
}
}