1<?php
2/**
3 * Slim - a micro PHP 5 framework
4 *
5 * @author      Josh Lockhart <info@slimframework.com>
6 * @copyright   2011-2017 Josh Lockhart
7 * @link        http://www.slimframework.com
8 * @license     http://www.slimframework.com/license
9 * @version     2.6.3
10 *
11 * MIT LICENSE
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33/**
34 * We use a mock application, instead of a Slim application.
35 * so that we may easily test the Method Override middleware
36 * in isolation.
37 */
38class CustomAppMethod
39{
40    protected $environment;
41
42    public function __construct()
43    {
44        $this->environment = \Slim\Environment::getInstance();
45    }
46
47    public function &environment() {
48        return $this->environment;
49    }
50
51    public function call()
52    {
53        //Do nothing
54    }
55}
56
57class MethodOverrideTest extends PHPUnit_Framework_TestCase
58{
59    /**
60     * Test overrides method as POST
61     */
62    public function testOverrideMethodAsPost()
63    {
64        \Slim\Environment::mock(array(
65            'REQUEST_METHOD' => 'POST',
66            'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
67            'CONTENT_LENGTH' => 11,
68            'slim.input' => '_METHOD=PUT'
69        ));
70        $app = new CustomAppMethod();
71        $mw = new \Slim\Middleware\MethodOverride();
72        $mw->setApplication($app);
73        $mw->setNextMiddleware($app);
74        $mw->call();
75        $env =& $app->environment();
76        $this->assertEquals('PUT', $env['REQUEST_METHOD']);
77        $this->assertTrue(isset($env['slim.method_override.original_method']));
78        $this->assertEquals('POST', $env['slim.method_override.original_method']);
79    }
80
81    /**
82     * Test does not override method if not POST
83     */
84    public function testDoesNotOverrideMethodIfNotPost()
85    {
86        \Slim\Environment::mock(array(
87            'REQUEST_METHOD' => 'GET',
88            'slim.input' => ''
89        ));
90        $app = new CustomAppMethod();
91        $mw = new \Slim\Middleware\MethodOverride();
92        $mw->setApplication($app);
93        $mw->setNextMiddleware($app);
94        $mw->call();
95        $env =& $app->environment();
96        $this->assertEquals('GET', $env['REQUEST_METHOD']);
97        $this->assertFalse(isset($env['slim.method_override.original_method']));
98    }
99
100    /**
101     * Test does not override method if no method override parameter
102     */
103    public function testDoesNotOverrideMethodAsPostWithoutParameter()
104    {
105        \Slim\Environment::mock(array(
106            'REQUEST_METHOD' => 'POST',
107            'REMOTE_ADDR' => '127.0.0.1',
108            'SCRIPT_NAME' => '/foo/index.php', //<-- Physical
109            'PATH_INFO' => '/bar', //<-- Virtual
110            'QUERY_STRING' => 'foo=bar',
111            'SERVER_NAME' => 'slim',
112            'SERVER_PORT' => 80,
113            'slim.url_scheme' => 'http',
114            'slim.input' => '',
115            'slim.errors' => fopen('php://stderr', 'w')
116        ));
117        $app = new CustomAppMethod();
118        $mw = new \Slim\Middleware\MethodOverride();
119        $mw->setApplication($app);
120        $mw->setNextMiddleware($app);
121        $mw->call();
122        $env =& $app->environment();
123        $this->assertEquals('POST', $env['REQUEST_METHOD']);
124        $this->assertFalse(isset($env['slim.method_override.original_method']));
125    }
126
127    /**
128     * Test overrides method with X-Http-Method-Override header
129     */
130    public function testOverrideMethodAsHeader()
131    {
132        \Slim\Environment::mock(array(
133            'REQUEST_METHOD' => 'POST',
134            'CONTENT_TYPE' => 'application/json',
135            'CONTENT_LENGTH' => 0,
136            'slim.input' => '',
137            'HTTP_X_HTTP_METHOD_OVERRIDE' => 'DELETE'
138        ));
139        $app = new CustomAppMethod();
140        $mw = new \Slim\Middleware\MethodOverride();
141        $mw->setApplication($app);
142        $mw->setNextMiddleware($app);
143        $mw->call();
144        $env =& $app->environment();
145        $this->assertEquals('DELETE', $env['REQUEST_METHOD']);
146        $this->assertTrue(isset($env['slim.method_override.original_method']));
147        $this->assertEquals('POST', $env['slim.method_override.original_method']);
148    }
149}
150