Welcome Guest, Not a member yet? Register   Sign In
Issue with HTTP Feature Testing
#2

(This post was last modified: 05-27-2021, 03:59 AM by stopz.)

Alright i found an aswer for my own question. I think it has to do with

Quote:The $params array does not make sense for every HTTP verb, but is included for consistency.

as written in the manual that not all request types support $params. Maybe GET is one of them. But for sake of consistency i wrote my own tiny solution to request endpoints in testing:

PHP Code:
/**
 * Request endpoint.
 * @method req
 */
public function req(string $type 'GET'string $url '', array $params = []):object
{
    # Prepare parameters & type.
    $params http_build_query($params);
    $type  strtoupper($type);

    # Base url with get parameters.
    $base_url rtrim(config('app')->baseURL'/') . '/' ltrim($url'/') . (
        ($type == 'GET')
            '?' $params
            
''
    );
    $contents file_get_contents($base_urlcontextstream_context_create([
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false
        
],
        'http' => [
            'method'  => $type,
            'header'  => 'Content-type: application/x-www-form-urlencoded' "\r\n" .
                        'Content-Length: ' strlen($params) . "\r\n",
            'content' => $params,
            'ignore_errors' => true
        
]
    ]));

    # http headers.
    $httphead $http_response_header;
    $head = [];

    # Assoc headers.
    foreach ($httphead as $n => $value)
    {
        $explode explode(':'$value);
        $key = ($n == || count($explode) == 1)
            $n
            
strtolower(reset($explode)) ;

        # Set header key and value.
        $head[$key] = $value;
    }

    # Proper status and url.
    $head['url']    $base_url;
    $head['status'] = $head[0];

    # Remove numeric status representation.
    unset($head[0]);

    # Extract response code.
    $head['code'] = (int) explode(' '$head['status'])[1];

    # Return headers.
    return (object) [
        'header' => (object) $head,
        'body' => $contents
    
];


What helped alot is that CodeIgniter actually got my request by:

PHP Code:
$this->request->getBody(); 

Hope this helps someone!
Reply


Messages In This Thread
Issue with HTTP Feature Testing - by stopz - 05-26-2021, 11:14 AM
RE: Issue with HTTP Feature Testing - by stopz - 05-27-2021, 03:40 AM
RE: Issue with HTTP Feature Testing - by MGatner - 05-27-2021, 08:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB