private function request($method, $path, $data = array(), $optionalHeaders = array()) { //Make method uppercase $method = strtoupper($method); $options = array('REQUEST_METHOD' => $method, 'REQUEST_URI' => $path); if ($method === 'GET') { $options['QUERY_STRING'] = http_build_query($data); } else { $params = json_encode($data); } // Prepare a mock environment $env = Environment::mock(array_merge($options, $optionalHeaders)); $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = $this->cookies; $serverParams = $env->all(); $body = new RequestBody(); // Attach JSON request if (isset($params)) { $headers->set('Content-Type', 'application/json;charset=utf8'); $body->write($params); } $this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); $response = new Response(); // Invoke request $app = $this->app; $this->response = $app($this->request, $response); // Return the application output. return (string) $this->response->getBody(); }
/** * Setup for the XML POST requests. * * @param string $xml The XML to use to mock the body of the request. */ public function setUpXmlPost($xml) { $uri = Uri::createFromString('https://example.com:443/foo'); $headers = new Headers(); $headers->set('Content-Type', 'application/xml;charset=utf8'); $cookies = []; $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'POST']); $serverParams = $env->all(); $body = new RequestBody(); $body->write($xml); $this->request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); $this->response = new Response(); }
/** * @param $input * @return Body */ protected function buildBody($input) { $getContent = function () use($input) { if (is_array($input)) { return http_build_query($input); } return $input; }; $content = $getContent(); $body = new RequestBody(); $body->write($content); $body->rewind(); return $body; }
private function prepareRequest($method, $url, array $requestParameters) { $env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => $url, 'REQUEST_METHOD' => $method]); $parts = explode('?', $url); if (isset($parts[1])) { $env['QUERY_STRING'] = $parts[1]; } $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = []; $serverParams = $env->all(); $body = new RequestBody(); $body->write(json_encode($requestParameters)); $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); return $request->withHeader('Content-Type', 'application/json'); }
public function testGetParametersWithBodyPriority() { $body = new RequestBody(); $body->write('foo=bar&abc=xyz'); $body->rewind(); $request = $this->requestFactory()->withBody($body)->withHeader('Content-Type', 'application/x-www-form-urlencoded'); $this->assertEquals(['abc' => 'xyz', 'foo' => 'bar'], $request->getParams()); }
public function testGetContentsDetachedThrowsRuntimeException() { $this->body->detach(); $this->setExpectedException('\\RuntimeException'); $this->body->getContents(); }
public function testGetParsedBodyXmlWithTextXMLMediaType() { $method = 'GET'; $uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', ''); $headers = new Headers(); $headers->set('Content-Type', 'text/xml'); $cookies = []; $serverParams = []; $body = new RequestBody(); $body->write('<person><name>Josh</name></person>'); $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); $this->assertEquals('Josh', $request->getParsedBody()->name); }