使用Slim测试PUT参数值的问题

时间:2022-10-15 20:29:24

I don't exactly understand how it goes with PUT variables and Slim.

我不完全理解它与PUT变量和Slim的关系。

Please consider the following route:

请考虑以下路线:

$app->put('/users/me',function() use ($app){
    $app->response->headers->set('Content-Type', 'application/json');
    $userData = $app->request()->put();
    var_dump($userData);
});

This route basically accepts a PUT request and returns whatever variables came with it. When I test it with curl it seems to be working as expected:

此路由基本上接受PUT请求并返回随附的任何变量。当我用curl测试它时,似乎按预期工作:

oris@oris:~/slim-api$ curl -i -H "Accept: application/json" -X PUT -d "hello=there" http://slim-api.com/users/me
HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Thu, 06 Feb 2014 09:53:11 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.6-1ubuntu1.5
Set-Cookie: PHPSESSID=2r6ta2fg6vdk7; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

array(1) {
    ["hello"]=>
    string(5) "there"
}

I wrote a little PHPUnit test for this route with this bootstrap.php file:

我用这个bootstrap.php文件为这个路径写了一个小的PHPUnit测试:

 public function testPutUsersMe()
{
    $this->put('/users/me',array("myKey=myValue"));
    $userResponse = $this->response->body();
    var_dump($userResponse);
}

I have also added a tiny addition to this bootstrap file to update the superglobals on each request:

我还在这个bootstrap文件中添加了一个小小的附加内容来更新每个请求的超级全局:

//sets php super globals so other packages would have access to them on tests context
public function setRequestGlobals($method,$path,$options = array())
{
    $_SERVER['REQUEST_METHOD'] = $method;
    if (!empty($options)){
        if ($method === "GET"){
            foreach($options as $key => $value){
                $_GET[$key] = $value;
            }
        }
        if ($method === "POST"){
            foreach($options as $key => $value){
                $_POST[$key] = $value;
            }
        }
        if ($method === "PUT"){
            foreach($options as $key => $value){
                $_POST[$key] = $value;
            }
        }
    }

}

I call $this->setRequestGlobals($method,$path,$options); inside the request method. But this causes the following response which isn't quite right:

我叫$ this-> setRequestGlobals($ method,$ path,$ options);在请求方法中。但这导致以下响应不太正确:

oris@oris:~/slim-api$ phpunit
PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from /home/oris/slim-api/phpunit.xml

...........string(44) "Array
(
    [slim.input] => myKey=myValue
)
"


Time: 119 ms, Memory: 7.75Mb

OK (11 tests, 85 assertions)
oris@oris:~/slim-api$ 

What I mean by "isn't quite right" is that the [slim.input] is now the parameter value, and the key and the value are the value. If I change the route (and the test) method to POST, I get the expected desired output:

我的意思是“不太正确”是[slim.input]现在是参数值,键和值是值。如果我将路由(和测试)方法更改为POST,我会得到预期的所需输出:

oris@oris:~/slim-api$ phpunit
PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from /home/oris/slim-api/phpunit.xml

...........string(33) "Array
(
    [myKey] => myValue
)
"


Time: 128 ms, Memory: 7.75Mb

OK (11 tests, 85 assertions)
oris@oris:~/slim-api$ 

Questions:

问题:

  • Generally, How would I pass an array with curl to this route instead of flat key1=value1, key2=value2 ?

    通常,我如何将带有curl的数组传递给此路由而不是flat key1 = value1,key2 = value2?

  • Why is this change in the test output when the only thing which is changed is the request method?

    当唯一改变的是请求方法时,为什么测试输出会发生这种变化?

Oddly, when I dig in a little into Slim code, on /vendor/slim/slim/Slim/Http/Request.php I see that the put function is just an alias for post:

奇怪的是,当我深入研究Slim代码时,在/vendor/slim/slim/Slim/Http/Request.php上我看到put函数只是post的别名:

    /**
 * Fetch PUT data (alias for \Slim\Http\Request::post)
 * @param  string           $key
 * @param  mixed            $default Default return value when key does not exist
 * @return array|mixed|null
 */
public function put($key = null, $default = null)
{
    return $this->post($key, $default);
}

1 个解决方案

#1


0  

as far as i remember, slim needs to configure the header to accept 'PUT' method first OR you need to add params of '_METHOD=PUT' along with the data array, and you send the request as 'POST'. the passed params might look like this : array("_METHOD"=>"PUT","data"=>array("myKey"=>"myValue"))

我记得,苗条需要首先配置标头接受'PUT'方法,或者你需要在数据数组中添加'_METHOD = PUT'参数,然后将请求发送为'POST'。传递的参数可能如下所示:array(“_ METHOD”=>“PUT”,“data”=> array(“myKey”=>“myValue”))

#1


0  

as far as i remember, slim needs to configure the header to accept 'PUT' method first OR you need to add params of '_METHOD=PUT' along with the data array, and you send the request as 'POST'. the passed params might look like this : array("_METHOD"=>"PUT","data"=>array("myKey"=>"myValue"))

我记得,苗条需要首先配置标头接受'PUT'方法,或者你需要在数据数组中添加'_METHOD = PUT'参数,然后将请求发送为'POST'。传递的参数可能如下所示:array(“_ METHOD”=>“PUT”,“data”=> array(“myKey”=>“myValue”))