/** * @dataProvider provideValidHttpMethod */ public function testCreateRequestInstance($method) { $request = new Request(Request::GET, '/', Request::HTTP, '1.1', ['Host' => 'http://wikipedia.com', 'User-Agent' => 'Mozilla/Firefox']); $this->assertSame(Request::GET, $request->getMethod()); $this->assertSame('/', $request->getPath()); $this->assertSame(Request::HTTP, $request->getScheme()); $this->assertSame('1.1', $request->getSchemeVersion()); $this->assertCount(2, $request->getHeaders()); $this->assertSame(['host' => 'http://wikipedia.com', 'user-agent' => 'Mozilla/Firefox'], $request->getHeaders()); $this->assertSame('http://wikipedia.com', $request->getHeader('Host')); $this->assertSame('Mozilla/Firefox', $request->getHeader('User-Agent')); $this->assertEmpty($request->getBody()); }
/** * @dataProvider provideValidHttpMethod * @param $method * @param $path * @param $http */ public function testCreateRequestInstance($method, $path, $http) { $request = new Request($method, $path, $http, "1.1", ["Host" => "http://www.google.com", "Content-Type" => "text/html", "Accept" => "application/json"]); $this->assertSame($method, $request->getMethod(), "Assert of request method"); $this->assertSame($path, $request->getPath(), "Assert of path"); $this->assertSame($http, $request->getScheme(), "Assert of scheme"); $this->assertSame("1.1", $request->getSchemeVersion(), "Assert of scheme version"); $this->assertCount(3, $request->getHeaders()); $this->assertSame(["host" => "http://www.google.com", "content-type" => "text/html", "accept" => "application/json"], $request->getHeaders()); $this->assertSame("http://www.google.com", $request->getHeader("Host")); $this->assertSame("application/json", $request->getHeader("Accept")); $this->assertSame("text/html", $request->getHeader("Content-Type")); $this->assertEmpty($request->getBody(), "Assert of body"); }
/** * @param Request $request * * @return Route * * @throws ClassNotExistsException * @throws NotFoundException * @throws \Exception */ public function handle(Request $request) { $this->request = $request; $controller = null; $matches = []; foreach ($this->routes as $route) { if ($route->match($request->getUrl(), $request->getMethod(), $matches)) { $this->current = $route; $controller = $this->getCallable($route->getHandler()); break; } } if ($controller == null) { throw new NotFoundException(); } $arguments = array_merge([$request], array_slice($matches, 1)); $response = call_user_func_array($controller, $arguments); if ($response == null) { throw new \Exception("Response can't be null"); } return $response; }
/** * @dataProvider provideRequestParameters */ public function testCreateRequestInstance($method, $path) { $body = '{ "foo": "bar" }'; $headers = ['Host' => 'http://wikipedia.com', 'User-Agent' => 'Mozilla/Firefox', 'Content-Type' => 'application/json']; $request = new Request($method, $path, Request::HTTP, '1.1', $headers, $body); $this->assertSame($method, $request->getMethod()); $this->assertSame($path, $request->getPath()); $this->assertSame(Request::HTTP, $request->getScheme()); $this->assertSame('1.1', $request->getSchemeVersion()); $this->assertSame($body, $request->getBody()); $this->assertCount(3, $request->getHeaders()); $this->assertSame(['host' => 'http://wikipedia.com', 'user-agent' => 'Mozilla/Firefox', 'content-type' => 'application/json'], $request->getHeaders()); $this->assertSame('http://wikipedia.com', $request->getHeader('Host')); $this->assertSame('Mozilla/Firefox', $request->getHeader('User-Agent')); $this->assertSame('application/json', $request->getHeader('Content-Type')); }
/** * @param Request $request * * @return \Symfony\Component\HttpFoundation\Request */ private function convertRequest(Request $request) { return \Symfony\Component\HttpFoundation\Request::create($request->getUrl(), $request->getMethod(), $request->atributes->all(), $request->cookies->all(), $request->files->all(), []); }