4
4
*
5
5
* @package gamajo\codeception-redirects
6
6
* @author Gary Jones
7
- * @copyright 2016 Gamajo Tech
7
+ * @copyright 2016 Gamajo
8
8
* @license MIT
9
9
*/
10
10
@@ -49,73 +49,135 @@ public function followRedirects($followRedirects = true)
49
49
}
50
50
51
51
/**
52
- * Check that a 200 HTTP Status is returned with the correct Location URL .
52
+ * Check if redirections are being followed or not .
53
53
*
54
- * Should be HTTP.
54
+ * @since 0.2.0
55
55
*
56
- * @since 0.1.0
57
- *
58
- * @param string $url Relative or absolute URL of redirect destination.
56
+ * @return bool True if redirects are being followed, false otherwise.
59
57
*/
60
- public function seePermanentRedirectToHttpFor ( $ url )
58
+ protected function isFollowingRedirects ( )
61
59
{
62
- $ this ->permanentRedirectForProtocol ( $ url , self :: PROTOCOL_HTTP );
60
+ return $ this ->getModule ( ' PhpBrowser ' )-> client -> isFollowingRedirects ( );
63
61
}
64
62
65
63
/**
66
64
* Check that a 301 HTTP Status is returned with the correct Location URL.
67
65
*
68
- * @since 0.1 .0
66
+ * @since 0.2 .0
69
67
*
70
- * @param string $url Relative or absolute URL of redirect destination .
71
- * @param string $checkDestinationExists Optional. Whether to check if destination URL is 200 OK .
68
+ * @param string $oldUrl Relative or absolute URL that should be redirected .
69
+ * @param string $newUrl Relative or absolute URL of redirect destination .
72
70
*/
73
- public function seePermanentRedirectTo ( $ url , $ checkDestinationExists = true )
71
+ public function seePermanentRedirectBetween ( $ oldUrl , $ newUrl )
74
72
{
75
- $ followsRedirects = $ this ->getModule ('PhpBrowser ' )->client ->isFollowingRedirects ();
73
+ // We must follow all redirects, so save current situation, force follow redirects, and revert at the end.
74
+ $ followsRedirects = $ this ->isFollowingRedirects ();
75
+ $ this ->followRedirects (false );
76
76
77
- /** @var Response $response */
78
- $ response = $ this ->getModule ('PhpBrowser ' )->client ->getInternalResponse ();
77
+ $ response = $ this ->sendHeadAndGetResponse ($ oldUrl );
79
78
$ responseCode = $ response ->getStatus ();
80
79
$ locationHeader = $ response ->getHeader ('Location ' , true );
81
80
82
81
// Check for 301 response code.
83
- $ this ->assertEquals (301 , $ responseCode );
82
+ $ this ->assertEquals (301 , $ responseCode, ' Response code was not 301. ' );
84
83
85
84
// Check location header URL contains submitted URL.
86
- $ this ->assertContains ($ url , $ locationHeader );
85
+ $ this ->assertContains ($ newUrl , $ locationHeader, ' Redirect destination not found in Location header. ' );
87
86
88
- if ($ checkDestinationExists && 'false ' !== $ checkDestinationExists ) {
89
- $ this ->followRedirects ( true );
90
- $ this ->urlExists ( $ url );
91
- $ this ->followRedirects ( $ followsRedirects );
92
- }
87
+ $ this ->followRedirects ($ followsRedirects );
93
88
}
94
89
95
90
/**
96
- * Check that a 200 HTTP Status is returned with the correct Location URL .
91
+ * Check that a 200 HTTP Status is returned and the URL has no redirects .
97
92
*
98
- * Should be HTTPS.
93
+ * @since 0.1.3
94
+ * @since 0.2.0 Renamed method, made public.
95
+ *
96
+ * @param string $url Relative or absolute URL of redirect destination.
97
+ */
98
+ public function urlDoesNotRedirect ($ url )
99
+ {
100
+ // We must follow all potential immediate redirects, so save current situation, force follow redirects, and revert at the end.
101
+ $ followsRedirects = $ this ->isFollowingRedirects ();
102
+ $ this ->followRedirects (true );
103
+
104
+ $ response = $ this ->sendHeadAndGetResponse ($ url );
105
+ $ responseCode = $ response ->getStatus ();
106
+ $ locationHeader = $ response ->getHeader ('Location ' , true );
107
+
108
+ // Check for 200 response code.
109
+ $ this ->assertEquals (200 , $ responseCode , 'Response code was not 200. ' );
110
+
111
+ // Check that destination URL does not try to redirect.
112
+ // Somewhat redundant, as this should never appear with a 200 HTTP Status code anyway.
113
+ $ this ->assertNull ($ locationHeader , 'Location header was found when it should not exist. ' );
114
+
115
+ $ this ->followRedirects ($ followsRedirects );
116
+ }
117
+
118
+ /**
119
+ * Use REST Module to send HEAD request and return the response.
120
+ *
121
+ * @since 0.2.0
122
+ *
123
+ * @param string $url
124
+ *
125
+ * @return null|Response
126
+ */
127
+ protected function sendHeadAndGetResponse ($ url ) {
128
+ /** @var REST $rest */
129
+ $ rest = $ this ->getModule ('REST ' );
130
+ $ rest ->sendHEAD ($ url );
131
+
132
+ return $ rest ->client ->getInternalResponse ();
133
+ }
134
+
135
+ /**
136
+ * Check that a 200 HTTP Status is eventually returned with the HTTP protocol.
137
+ *
138
+ * Should be HTTP. The URL should be the same - only the protocol is different.
99
139
*
100
140
* @since 0.1.0
141
+ * @since 0.2.0 Method renamed.
101
142
*
102
143
* @param string $url Relative or absolute URL of redirect destination.
103
144
*/
104
- public function seePermanentRedirectToHttpsFor ($ url )
145
+ public function seeHttpProtocolAlwaysUsedFor ($ url )
105
146
{
106
- $ this ->permanentRedirectForProtocol ($ url , self ::PROTOCOL_HTTPS );
147
+ $ this ->checkUrlAndProtocolAreCorrect ($ url , self ::PROTOCOL_HTTP );
107
148
}
108
149
109
150
/**
110
- * Check that a 200 HTTP Status is returned with the correct Location URL.
151
+ * Check that a 200 HTTP Status is eventually returned with the HTTPS protocol.
152
+ *
153
+ * Should be HTTPS. The URL should be the same - only the protocol is different.
111
154
*
112
155
* @since 0.1.0
156
+ * @since 0.2.0 Method renamed.
157
+ *
158
+ * @param string $url Relative or absolute URL of redirect destination.
159
+ */
160
+ public function seeHttpsProtocolAlwaysUsedFor ($ url )
161
+ {
162
+ $ this ->checkUrlAndProtocolAreCorrect ($ url , self ::PROTOCOL_HTTPS );
163
+ }
164
+
165
+ /**
166
+ * For a given URL, check that a 200 HTTP Status is returned, the protocol matches given value,
167
+ * and URL (except maybe protocol) has not changed.
168
+ *
169
+ * @since 0.1.0
170
+ * @since 0.2.0 Method renamed.
113
171
*
114
172
* @param string $url Relative or absolute URL of redirect destination.
115
173
* @param string $protocol Protocol: 'http' or 'https'.
116
174
*/
117
- protected function permanentRedirectForProtocol ($ url , $ protocol )
175
+ protected function checkUrlAndProtocolAreCorrect ($ url , $ protocol )
118
176
{
177
+ // We must follow all redirects, so save current situation, force follow redirects, and revert at the end.
178
+ $ followsRedirects = $ this ->isFollowingRedirects ();
179
+ $ this ->followRedirects (true );
180
+
119
181
$ url = ltrim ($ url , '/ ' );
120
182
121
183
/** @var REST $rest */
@@ -129,36 +191,14 @@ protected function permanentRedirectForProtocol($url, $protocol)
129
191
$ scheme = parse_url ($ responseUri , PHP_URL_SCHEME );
130
192
131
193
// Check for 200 response code.
132
- $ this ->assertEquals (200 , $ responseCode );
194
+ $ this ->assertEquals (200 , $ responseCode, ' Response code was not 200. ' );
133
195
134
196
// Check that destination URL contains submitted URL part.
135
- $ this ->assertContains ($ url , $ responseUri );
197
+ $ this ->assertContains ($ url , $ responseUri, ' Destination URL does not contain the original URL. ' );
136
198
137
199
// Check for submitted http/https value matches destination URL.
138
- $ this ->assertEquals ($ protocol , $ scheme );
139
- }
140
-
141
- /**
142
- * Check that a 200 HTTP Status is returned and the final URL has no more redirects.
143
- *
144
- * @since 0.1.3
145
- *
146
- * @param string $url Relative or absolute URL of redirect destination.
147
- * @param string $protocol Protocol: 'http' or 'https'.
148
- */
149
- protected function urlExists ($ url )
150
- {
151
- $ url = ltrim ($ url , '/ ' );
200
+ $ this ->assertEquals ($ protocol , $ scheme , 'Protocol at destination URL does not match expected value. ' );
152
201
153
- /** @var REST $rest */
154
- $ rest = $ this ->getModule ('REST ' );
155
- $ rest ->sendHEAD ($ url );
156
- $ responseCode = $ rest ->client ->getInternalResponse ()->getStatus ();
157
- $ locationHeader = $ rest ->client ->getInternalResponse ()->getHeader ('Location ' );
158
-
159
- // Check for 200 response code.
160
- $ this ->assertEquals (200 , $ responseCode );
161
- // Check that destination URL does not try to redirect.
162
- $ this ->assertNull ($ locationHeader );
202
+ $ this ->followRedirects ($ followsRedirects );
163
203
}
164
204
}
0 commit comments