-
Notifications
You must be signed in to change notification settings - Fork 5
/
RequestInfoTest.php
133 lines (115 loc) · 5.23 KB
/
RequestInfoTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php declare(strict_types = 1);
/**
* Copyright (c) 2016 Holger Woltersdorf & Contributors
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
namespace IceHawk\IceHawk\Tests\Unit\Defaults;
use IceHawk\IceHawk\Defaults\RequestInfo;
class RequestInfoTest extends \PHPUnit_Framework_TestCase
{
public function testCanInstantiateFromEnvUsingGlobalServerVariable()
{
$requestTimeFloat = microtime( true );
$_SERVER['REQUEST_URI'] = '/unit/test';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['QUERY_STRING'] = 'unit=test';
$_SERVER['HTTP_USER_AGENT'] = 'phpunit cli';
$_SERVER['HTTP_HOST'] = 'www.icehawk.de';
$_SERVER['HTTP_REFERER'] = 'http://www.example.com';
$_SERVER['HTTP_ACCEPT'] = 'text/html; charset=utf-8';
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
$_SERVER['REMOTE_ADDR'] = '192.168.0.10';
$_SERVER['HTTP_REFERER'] = 'http://www.example.com';
$_SERVER['REQUEST_TIME_FLOAT'] = $requestTimeFloat;
$_SERVER['HTTPS'] = 'on';
$_SERVER['PHP_AUTH_USER'] = 'Unit';
$_SERVER['PHP_AUTH_PW'] = 'Test';
$_SERVER['CONTENT_LENGTH'] = '512';
$_SERVER['CONTENT_TYPE'] = 'application/json';
$requestInfo = RequestInfo::fromEnv();
$this->assertInstanceOf( RequestInfo::class, $requestInfo );
$this->assertEquals( '/unit/test', $requestInfo->getUri() );
$this->assertEquals( 'POST', $requestInfo->getMethod() );
$this->assertEquals( 'unit=test', $requestInfo->getQueryString() );
$this->assertEquals( 'phpunit cli', $requestInfo->getUserAgent() );
$this->assertEquals( 'www.icehawk.de', $requestInfo->getHost() );
$this->assertEquals( 'http://www.example.com', $requestInfo->getReferer() );
$this->assertEquals( 'text/html; charset=utf-8', $requestInfo->getAcceptedContentTypes() );
$this->assertEquals( '127.0.0.1', $requestInfo->getServerAddress() );
$this->assertEquals( '192.168.0.10', $requestInfo->getClientAddress() );
$this->assertEquals( 'http://www.example.com', $requestInfo->getReferer() );
$this->assertEquals( $requestTimeFloat, $requestInfo->getRequestTimeFloat() );
$this->assertEquals( 'Unit', $requestInfo->getAuthUser() );
$this->assertEquals( 'Test', $requestInfo->getAuthPassword() );
$this->assertEquals( $requestTimeFloat, $requestInfo->getRequestTimeFloat() );
$this->assertEquals( '512', $requestInfo->getContentLength() );
$this->assertEquals( 'application/json', $requestInfo->getContentType() );
$this->assertTrue( $requestInfo->isSecure() );
}
public function testGettersReturnEmptyValuesIfKeyIsNotSetInServerData()
{
$requestInfo = new RequestInfo( [] );
$this->assertSame( '', $requestInfo->getMethod() );
$this->assertSame( '', $requestInfo->getQueryString() );
$this->assertSame( '', $requestInfo->getUserAgent() );
$this->assertSame( '', $requestInfo->getHost() );
$this->assertSame( '', $requestInfo->getReferer() );
$this->assertSame( '', $requestInfo->getAcceptedContentTypes() );
$this->assertSame( '', $requestInfo->getServerAddress() );
$this->assertSame( '', $requestInfo->getClientAddress() );
$this->assertSame( '', $requestInfo->getReferer() );
$this->assertSame( '', $requestInfo->getAuthUser() );
$this->assertSame( '', $requestInfo->getAuthPassword() );
$this->assertSame( 0.0, $requestInfo->getRequestTimeFloat() );
}
public function testGetUriReturnSlashIfKeyIsNotSetInServerData()
{
$requestInfo = new RequestInfo( [] );
$this->assertEquals( '/', $requestInfo->getUri() );
}
public function testGetUriOnlyReturnsThePathWithoutQueryString()
{
$serverData = ['REQUEST_URI' => '/unit/test?unit=test'];
$requestInfo = new RequestInfo( $serverData );
$this->assertEquals( '/unit/test', $requestInfo->getUri() );
}
/**
* @dataProvider requestMethodProvider
*/
public function testGetMethodAlwaysReturnsRequestMethodInUpperCase( $requestMethod, $expectedRequestMethod )
{
$serverData = ['REQUEST_METHOD' => $requestMethod];
$requestInfo = new RequestInfo( $serverData );
$this->assertEquals( $expectedRequestMethod, $requestInfo->getMethod() );
}
public function requestMethodProvider()
{
return [
['get', 'GET'],
['Get', 'GET'],
['GeT', 'GET'],
['head', 'HEAD'],
['HeaD', 'HEAD'],
['post', 'POST'],
['pOSt', 'POST'],
];
}
public function testHttpsIsCheckedCaseInsensitive()
{
$requestInfo1 = new RequestInfo( ['HTTPS' => 'On'] );
$requestInfo2 = new RequestInfo( ['HTTPS' => 'on'] );
$requestInfo3 = new RequestInfo( ['HTTPS' => 'oN'] );
$requestInfo4 = new RequestInfo( ['HTTPS' => 'ON'] );
$this->assertTrue( $requestInfo1->isSecure() );
$this->assertTrue( $requestInfo2->isSecure() );
$this->assertTrue( $requestInfo3->isSecure() );
$this->assertTrue( $requestInfo4->isSecure() );
}
}