1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-15 05:15:13 +01:00

[Http] Making the Content-MD5 validation plugin more readable and adding more test coverage

This commit is contained in:
Michael Dowling 2011-04-07 23:27:01 -05:00
parent 85ed138b78
commit 6bff5cbd83
2 changed files with 54 additions and 37 deletions

View File

@ -55,14 +55,25 @@ class Md5ValidatorPlugin implements Observer
public function update(Subject $subject, $event, $context = null)
{
/* @var $subject EntityEnclosingRequest */
if ($event == 'request.complete' && $subject instanceof RequestInterface) {
if ($event != 'request.complete' || !($subject instanceof RequestInterface)) {
return;
}
$contentMd5 = $context->getContentMd5();
if ($contentMd5) {
if (!$contentMd5) {
return;
}
$contentEncoding = $context->getContentEncoding();
if (!$contentEncoding || $this->contentEncoded) {
if ($contentEncoding && !$this->contentEncoded) {
return false;
}
// Make sure that the request's size is under the cutoff size
$size = $context->getContentLength() ?: $context->getBody()->getSize();
if ($size && $size < $this->contentLengthCutoff) {
if (!$size || $size > $this->contentLengthCutoff) {
return;
}
switch ($contentEncoding) {
case 'gzip':
@ -94,7 +105,3 @@ class Md5ValidatorPlugin implements Observer
}
}
}
}
}
}
}

View File

@ -33,6 +33,10 @@ class Md5ValidatorPluginTest extends \Guzzle\Tests\GuzzleTestCase
), 'abc');
$request->getEventManager()->notify('request.complete', $response);
// Try again with no Content-MD5
$response->removeHeader('Content-MD5');
$request->getEventManager()->notify('request.complete', $response);
}
/**
@ -108,5 +112,11 @@ class Md5ValidatorPluginTest extends \Guzzle\Tests\GuzzleTestCase
'Content-Encoding' => 'compress'
), 'abc');
$request->getEventManager()->notify('request.complete', $response);
// Try again with encoding and disabled content-encoding checks
$request->getEventManager()->detach($plugin);
$plugin = new Md5ValidatorPlugin(false);
$request->getEventManager()->attach($plugin);
$request->getEventManager()->notify('request.complete', $response);
}
}