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

Correct methods to use snake_case #184

Merged
merged 2 commits into from
Oct 14, 2015
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
54 changes: 42 additions & 12 deletions library/Requests/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ public function is_expired() {
* @param Requests_IRI $uri URI to check
* @return boolean Whether the cookie is valid for the given URI
*/
public function uriMatches(Requests_IRI $uri) {
if (!$this->domainMatches($uri->host)) {
public function uri_matches(Requests_IRI $uri) {
if (!$this->domain_matches($uri->host)) {
return false;
}

if (!$this->pathMatches($uri->path)) {
if (!$this->path_matches($uri->path)) {
return false;
}

Expand All @@ -135,7 +135,7 @@ public function uriMatches(Requests_IRI $uri) {
* @param string $string Domain to check
* @return boolean Whether the cookie is valid for the given domain
*/
public function domainMatches($string) {
public function domain_matches($string) {
if (!isset($this->attributes['domain'])) {
// Cookies created manually; cookies created by Requests will set
// the domain to the requested domain
Expand Down Expand Up @@ -188,7 +188,7 @@ public function domainMatches($string) {
* @param string $request_path Path to check
* @return boolean Whether the cookie is valid for the given path
*/
public function pathMatches($request_path) {
public function path_matches($request_path) {
if (empty($request_path)) {
// Normalize empty path to root
$request_path = '/';
Expand Down Expand Up @@ -233,7 +233,7 @@ public function pathMatches($request_path) {
public function normalize() {
foreach ($this->attributes as $key => $value) {
$orig_value = $value;
$value = $this->normalizeAttribute($key, $value);
$value = $this->normalize_attribute($key, $value);
if ($value === null) {
unset($this->attributes[$key]);
continue;
Expand All @@ -256,7 +256,7 @@ public function normalize() {
* @param string|boolean $value Attribute value (string value, or true if empty/flag)
* @return mixed Value if available, or null if the attribute value is invalid (and should be skipped)
*/
protected function normalizeAttribute($name, $value) {
protected function normalize_attribute($name, $value) {
switch (strtolower($name)) {
case 'expires':
// Expiration parsing, as per RFC 6265 section 5.2.1
Expand Down Expand Up @@ -312,10 +312,20 @@ protected function normalizeAttribute($name, $value) {
*
* @return string Cookie formatted for Cookie header
*/
public function formatForHeader() {
public function format_for_header() {
return sprintf('%s=%s', $this->name, $this->value);
}

/**
* Format a cookie for a Cookie header
*
* @deprecated Use {@see Requests_Cookie::format_for_header}
* @return string
*/
public function formatForHeader() {
return $this->format_for_header();
}

/**
* Format a cookie for a Set-Cookie header
*
Expand All @@ -324,8 +334,8 @@ public function formatForHeader() {
*
* @return string Cookie formatted for Set-Cookie header
*/
public function formatForSetCookie() {
$header_value = $this->formatForHeader();
public function format_for_set_cookie() {
$header_value = $this->format_for_header();
if (!empty($this->attributes)) {
$parts = array();
foreach ($this->attributes as $key => $value) {
Expand All @@ -343,6 +353,16 @@ public function formatForSetCookie() {
return $header_value;
}

/**
* Format a cookie for a Set-Cookie header
*
* @deprecated Use {@see Requests_Cookie::format_for_set_cookie}
* @return string
*/
public function formatForSetCookie() {
return $this->format_for_set_cookie();
}

/**
* Get the cookie value
*
Expand Down Expand Up @@ -412,7 +432,7 @@ public static function parse($string, $name = '', $reference_time = null) {
* @param Requests_Response_Headers $headers
* @return array
*/
public static function parseFromHeaders(Requests_Response_Headers $headers, Requests_IRI $origin = null, $reference_time = null) {
public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $reference_time = null) {
$cookie_headers = $headers->getValues('Set-Cookie');
if (empty($cookie_headers)) {
return array();
Expand Down Expand Up @@ -458,7 +478,7 @@ public static function parseFromHeaders(Requests_Response_Headers $headers, Requ
}

// Reject invalid cookie domains
if (!empty($origin) && !$parsed->domainMatches($origin->host)) {
if (!empty($origin) && !$parsed->domain_matches($origin->host)) {
continue;
}

Expand All @@ -467,4 +487,14 @@ public static function parseFromHeaders(Requests_Response_Headers $headers, Requ

return $cookies;
}

/**
* Parse all Set-Cookie headers from request headers
*
* @deprecated Use {@see Requests_Cookie::parse_from_headers}
* @return string
*/
public static function parseFromHeaders(Requests_Response_Headers $headers) {
return self::parse_from_headers($headers);
}
}
18 changes: 14 additions & 4 deletions library/Requests/Cookie/Jar.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,24 @@ public function __construct($cookies = array()) {
* @param string|Requests_Cookie $cookie
* @return Requests_Cookie
*/
public function normalizeCookie($cookie, $key = null) {
public function normalize_cookie($cookie, $key = null) {
if ($cookie instanceof Requests_Cookie) {
return $cookie;
}

return Requests_Cookie::parse($cookie, $key);
}

/**
* Normalise cookie data into a Requests_Cookie
*
* @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie}
* @return Requests_Cookie
*/
public function normalizeCookie($cookie, $key = null) {
return $this->normalize_cookie($cookie, $key);
}

/**
* Check if the given item exists
*
Expand Down Expand Up @@ -129,15 +139,15 @@ public function before_request($url, &$headers, &$data, &$type, &$options) {
if (!empty($this->cookies)) {
$cookies = array();
foreach ($this->cookies as $key => $cookie) {
$cookie = $this->normalizeCookie($cookie, $key);
$cookie = $this->normalize_cookie($cookie, $key);

// Skip expired cookies
if ($cookie->is_expired()) {
continue;
}

if ( $cookie->domainMatches( $url->host ) ) {
$cookies[] = $cookie->formatForHeader();
if ( $cookie->domain_matches( $url->host ) ) {
$cookies[] = $cookie->format_for_header();
}
}

Expand Down
52 changes: 26 additions & 26 deletions tests/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public function testBasicCookie() {
$this->assertEquals('testvalue', $cookie->value);
$this->assertEquals('testvalue', (string) $cookie);

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

public function testCookieWithAttributes() {
Expand All @@ -19,8 +19,8 @@ public function testCookieWithAttributes() {
);
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);

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

public function testEmptyCookieName() {
Expand Down Expand Up @@ -221,7 +221,7 @@ public function testDomainExactMatch($original, $check, $matches, $domain_matche
$attributes = new Requests_Utility_CaseInsensitiveDictionary();
$attributes['domain'] = $original;
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
$this->assertEquals($matches, $cookie->domainMatches($check));
$this->assertEquals($matches, $cookie->domain_matches($check));
}

/**
Expand All @@ -234,7 +234,7 @@ public function testDomainMatch($original, $check, $matches, $domain_matches) {
'host-only' => false
);
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
$this->assertEquals($domain_matches, $cookie->domainMatches($check));
$this->assertEquals($domain_matches, $cookie->domain_matches($check));
}

public function pathMatchProvider() {
Expand Down Expand Up @@ -264,7 +264,7 @@ public function testPathMatch($original, $check, $matches) {
$attributes = new Requests_Utility_CaseInsensitiveDictionary();
$attributes['path'] = $original;
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
$this->assertEquals($matches, $cookie->pathMatches($check));
$this->assertEquals($matches, $cookie->path_matches($check));
}

public function urlMatchProvider() {
Expand Down Expand Up @@ -305,7 +305,7 @@ public function testUrlExactMatch($domain, $path, $check, $matches, $domain_matc
$attributes['path'] = $path;
$check = new Requests_IRI($check);
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
$this->assertEquals($matches, $cookie->uriMatches($check));
$this->assertEquals($matches, $cookie->uri_matches($check));
}

/**
Expand All @@ -322,7 +322,7 @@ public function testUrlMatch($domain, $path, $check, $matches, $domain_matches)
);
$check = new Requests_IRI($check);
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
$this->assertEquals($domain_matches, $cookie->uriMatches($check));
$this->assertEquals($domain_matches, $cookie->uri_matches($check));
}

public function testUrlMatchSecure() {
Expand All @@ -335,12 +335,12 @@ public function testUrlMatchSecure() {
);
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);

$this->assertTrue($cookie->uriMatches(new Requests_IRI('https://example.com/')));
$this->assertFalse($cookie->uriMatches(new Requests_IRI('http://example.com/')));
$this->assertTrue($cookie->uri_matches(new Requests_IRI('https://example.com/')));
$this->assertFalse($cookie->uri_matches(new Requests_IRI('http://example.com/')));

// Double-check host-only
$this->assertTrue($cookie->uriMatches(new Requests_IRI('https://www.example.com/')));
$this->assertFalse($cookie->uriMatches(new Requests_IRI('http://www.example.com/')));
$this->assertTrue($cookie->uri_matches(new Requests_IRI('https://www.example.com/')));
$this->assertFalse($cookie->uri_matches(new Requests_IRI('http://www.example.com/')));
}

/**
Expand All @@ -352,17 +352,17 @@ public function testUrlMatchSecure() {
*/
public function testUrlMatchManuallySet() {
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue');
$this->assertTrue($cookie->domainMatches('example.com'));
$this->assertTrue($cookie->domainMatches('example.net'));
$this->assertTrue($cookie->pathMatches('/'));
$this->assertTrue($cookie->pathMatches('/test'));
$this->assertTrue($cookie->pathMatches('/test/'));
$this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.com/')));
$this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.com/test')));
$this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.com/test/')));
$this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.net/')));
$this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.net/test')));
$this->assertTrue($cookie->uriMatches(new Requests_IRI('http://example.net/test/')));
$this->assertTrue($cookie->domain_matches('example.com'));
$this->assertTrue($cookie->domain_matches('example.net'));
$this->assertTrue($cookie->path_matches('/'));
$this->assertTrue($cookie->path_matches('/test'));
$this->assertTrue($cookie->path_matches('/test/'));
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/')));
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/test')));
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/test/')));
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/')));
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/test')));
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/test/')));
}

public static function parseResultProvider() {
Expand Down Expand Up @@ -508,7 +508,7 @@ public function testParsingHeaderObject($header, $expected, $expected_attributes
// Set the reference time to 2014-01-01 00:00:00
$reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );

$parsed = Requests_Cookie::parseFromHeaders($headers, null, $reference_time);
$parsed = Requests_Cookie::parse_from_headers($headers, null, $reference_time);
$this->assertCount(1, $parsed);

$cookie = reset($parsed);
Expand Down Expand Up @@ -629,7 +629,7 @@ public function testParsingHeaderWithOrigin($header, $origin, $expected, $expect
// Set the reference time to 2014-01-01 00:00:00
$reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );

$parsed = Requests_Cookie::parseFromHeaders($headers, $origin, $reference_time);
$parsed = Requests_Cookie::parse_from_headers($headers, $origin, $reference_time);
if (isset($expected['invalid'])) {
$this->assertCount(0, $parsed);
return;
Expand Down