Exemple #1
0
 /**
  * @param Request $request
  * @param Element $config
  * @return bool
  */
 public function match(Request $request, Element $config)
 {
     if (!count($config->getRequest()->getAllowedMethods())) {
         return true;
     }
     return in_array($request->getMethod(), $config->getRequest()->getAllowedMethods(), true);
 }
Exemple #2
0
 /**
  * @param Request $request
  * @param Element $config
  * @return boolean
  */
 public function match(Request $request, Element $config)
 {
     if (!$config->getRequest()->hasBody()) {
         return true;
     }
     if (!$this->phpMatcher->match($request->getContent(), $config->getRequest()->getBody())) {
         return false;
     }
     return true;
 }
Exemple #3
0
 /**
  * @param Request $request
  * @param Element $config
  * @return bool
  */
 public function match(Request $request, Element $config)
 {
     $pathPattern = $config->getRequest()->getPath();
     preg_match_all('#\\{\\w+\\}#', $config->getRequest()->getPath(), $placeholders, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
     foreach ($placeholders as $placeholderMatch) {
         $placeholder = $placeholderMatch[0][0];
         $pathPattern = str_replace($placeholder, '__PLACEHOLDER__', $pathPattern);
     }
     $pathPattern = '/^' . str_replace('__PLACEHOLDER__', '([^\\/]*)', preg_quote($pathPattern, '/')) . '$/i';
     return 0 !== preg_match($pathPattern, $request->getPathInfo());
 }
Exemple #4
0
 /**
  * @param Request $request
  * @param Element $config
  * @return boolean
  */
 public function match(Request $request, Element $config)
 {
     if (!$config->getRequest()->hasHeaders()) {
         return true;
     }
     foreach ($config->getRequest()->getHeaders() as $headerName => $headerValuePattern) {
         if (!$request->headers->has($headerName)) {
             return false;
         }
         if (!$this->phpMatcher->match($request->headers->get($headerName), $headerValuePattern)) {
             return false;
         }
     }
     return true;
 }
 function it_match_when_headers_from_config_are_equal_headers_from_request()
 {
     $responseConfig = Element::fromArray(['request' => ['path' => '/foo', 'headers' => ['Header' => 'Value', 'Header1' => 'Value1']]]);
     $request = Request::create('/foo');
     $request->headers->add(['Header' => 'Value', 'Header1' => 'Value1']);
     $this->match($request, $responseConfig)->shouldReturn(true);
 }
Exemple #6
0
 /**
  * @param Loader $loader
  * @param MatchingPolicy $matchingPolicy
  */
 public function __construct(Loader $loader, MatchingPolicy $matchingPolicy)
 {
     $this->configs = [];
     foreach ($loader->getResponsesArray() as $responseArrayConfig) {
         $this->configs[] = Element::fromArray($responseArrayConfig);
     }
     $this->matchingPolicy = $matchingPolicy;
 }
 function it_returns_true_when_all_policies_can_match_request_to_response_config(MatchingPolicy $positiveMatchingPolicy1, MatchingPolicy $positiveMatchingPolicy2)
 {
     $this->addMatchingPolicy($positiveMatchingPolicy1);
     $this->addMatchingPolicy($positiveMatchingPolicy2);
     $responseConfig = Element::fromArray(['request' => ['path' => '/foo']]);
     $request = Request::create('/foo');
     $positiveMatchingPolicy1->match($request, $responseConfig)->willReturn(true);
     $positiveMatchingPolicy2->match($request, $responseConfig)->willReturn(true);
     $this->match($request, $responseConfig)->shouldReturn(true);
 }
 /**
  * @param Request $request
  * @param Element $config
  * @return boolean
  */
 public function match(Request $request, Element $config)
 {
     if (!$config->getRequest()->hasBodyParameters() && !$config->getRequest()->hasQueryParameters()) {
         return true;
     }
     foreach ($config->getRequest()->getQueryParameters() as $name => $valuePattern) {
         if (!$request->query->has($name)) {
             return false;
         }
         if (!$this->phpMatcher->match($request->query->get($name), $valuePattern)) {
             return false;
         }
     }
     foreach ($config->getRequest()->getBodyParameters() as $name => $valuePattern) {
         if (!$request->request->has($name)) {
             return false;
         }
         if (!$this->phpMatcher->match($request->request->get($name), $valuePattern)) {
             return false;
         }
     }
     return true;
 }
 function it_return_false_when_request_method_is_not_in_response_config_allowed_methods()
 {
     $responseConfig = Element::fromArray(['request' => ['path' => '/foo', 'methods' => ['POST']]]);
     $request = Request::create('/foo', 'GET');
     $this->match($request, $responseConfig)->shouldReturn(false);
 }
Exemple #10
0
 function it_not_match_when_request_body_not_match_config_body()
 {
     $responseConfig = Element::fromArray(['request' => ['path' => '/foo', 'headers' => [], 'body' => 'HELLO WORLD']]);
     $request = Request::create('/foo', 'GET', [], [], [], [], 'WORLD HELLO');
     $this->match($request, $responseConfig)->shouldReturn(false);
 }
Exemple #11
0
 /**
  * @param Element $config
  * @param Request $request
  * @return Response
  */
 public function build(Element $config, Request $request)
 {
     $content = $this->twig->render($config->getResponse()->getContent(), ['request' => $request, 'path' => $this->requestParser->extractPlaceholders($request, $config->getRequest()->getPath())]);
     return new Response($content, $config->getResponse()->getStatus(), $config->getResponse()->getHeaders());
 }
 function it_does_not_match_when_request_path_is_not_matching_response_config_path()
 {
     $responseConfig = Element::fromArray(['request' => ['path' => '/foo/bar']]);
     $request = Request::create('/foo/baz');
     $this->match($request, $responseConfig)->shouldReturn(false);
 }
 function it_match_when_request_config_query_and_body_parameters_match_parameters_from_request()
 {
     $responseConfig = Element::fromArray(['request' => ['path' => '/foo', 'query' => ['foo' => 'bar'], 'request' => ['foo' => 'bar']]]);
     $request = Request::create('/foo?foo=bar', 'POST', ['foo' => 'bar']);
     $this->match($request, $responseConfig)->shouldReturn(true);
 }