Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 68fab74

Browse files
committed
Merge branch 'hotfix/122'
Close #122
2 parents 68c015a + 576a963 commit 68fab74

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#122](https://github.com/zendframework/zend-diactoros/pull/122) updates the
22+
`ServerRequestFactory` to retrieve the HTTP protocol version and inject it in
23+
the generated `ServerRequest`, which previously was not performed.
2224

2325
## 1.3.1 - 2015-12-16
2426

src/ServerRequestFactory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Http\Message\MessageInterface;
1515
use Psr\Http\Message\UploadedFileInterface;
1616
use stdClass;
17+
use UnexpectedValueException;
1718

1819
/**
1920
* Class for marshaling a request object from the current PHP environment.
@@ -70,6 +71,7 @@ public static function fromGlobals(
7071
);
7172

7273
return $request
74+
->withProtocolVersion(static::marshalProtocolVersion($server))
7375
->withCookieParams($cookies ?: $_COOKIE)
7476
->withQueryParams($query ?: $_GET)
7577
->withParsedBody($body ?: $_POST);
@@ -455,4 +457,26 @@ private static function normalizeNestedFileSpec(array $files = [])
455457
}
456458
return $normalizedFiles;
457459
}
460+
461+
/**
462+
* Return HTTP protocol version (X.Y)
463+
*
464+
* @param array $server
465+
* @return string
466+
*/
467+
private static function marshalProtocolVersion(array $server)
468+
{
469+
if (! isset($server['SERVER_PROTOCOL'])) {
470+
return '1.1';
471+
}
472+
473+
if (! preg_match('#^(HTTP/)?(?P<version>[1-9]\d*\.\d)$#', $server['SERVER_PROTOCOL'], $matches)) {
474+
throw new UnexpectedValueException(sprintf(
475+
'Unrecognized protocol version (%s)',
476+
$server['SERVER_PROTOCOL']
477+
));
478+
}
479+
480+
return $matches['version'];
481+
}
458482
}

test/ServerRequestFactoryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
namespace ZendTest\Diactoros;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13+
use ReflectionMethod;
1314
use ReflectionProperty;
15+
use UnexpectedValueException;
1416
use Zend\Diactoros\ServerRequest;
1517
use Zend\Diactoros\ServerRequestFactory;
1618
use Zend\Diactoros\UploadedFile;
@@ -372,6 +374,7 @@ public function testCanCreateServerRequestViaFromGlobalsMethod()
372374
$this->assertEquals($body, $request->getParsedBody());
373375
$this->assertEquals($expectedFiles, $request->getUploadedFiles());
374376
$this->assertEmpty($request->getAttributes());
377+
$this->assertEquals('1.1', $request->getProtocolVersion());
375378
}
376379

377380
public function testNormalizeServerUsesMixedCaseAuthorizationHeaderFromApacheWhenPresent()
@@ -434,4 +437,28 @@ public function testNormalizeFilesReturnsOnlyActualFilesWhenOriginalFilesContain
434437

435438
$this->assertCount(1, $normalizedFiles['fooFiles']);
436439
}
440+
441+
public function testMarshalProtocolVersionReturnsHttpVersion()
442+
{
443+
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
444+
$method->setAccessible(true);
445+
$version = $method->invoke(null, ['SERVER_PROTOCOL' => 'HTTP/1.0']);
446+
$this->assertEquals('1.0', $version);
447+
}
448+
449+
public function testMarshalProtocolVersionRisesExceptionIfVersionIsNotRecognized()
450+
{
451+
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
452+
$method->setAccessible(true);
453+
$this->setExpectedException('UnexpectedValueException');
454+
$method->invoke(null, ['SERVER_PROTOCOL' => 'dadsa/1.0']);
455+
}
456+
457+
public function testMarshalProtocolReturnsDefaultValueIfHeaderIsNotPresent()
458+
{
459+
$method = new ReflectionMethod(ServerRequestFactory::class, 'marshalProtocolVersion');
460+
$method->setAccessible(true);
461+
$version = $method->invoke(null, []);
462+
$this->assertEquals('1.1', $version);
463+
}
437464
}

0 commit comments

Comments
 (0)