Ejemplo n.º 1
0
 public function testRoundRobin()
 {
     // given
     $expected = array(Url::fromHostAndPort("foo1.fliglio.com", 8001), Url::fromHostAndPort("foo2.fliglio.com", 8002));
     $stubResolver = StubResolver::createDoubleResult();
     // when
     $lb = new ConsulLoadBalancer($stubResolver, new RoundRobinLoadBalancerStrategy(), "foo");
     $foundA = array($lb->next(), $lb->next());
     $foundB = array($lb->next(), $lb->next());
     // then
     // round robin behavior
     $this->assertEquals($foundA[0]->getHost(), $foundB[0]->getHost());
     $this->assertEquals($foundA[0]->getPort(), $foundB[0]->getPort());
     $this->assertEquals($foundA[1]->getHost(), $foundB[1]->getHost());
     $this->assertEquals($foundA[1]->getPort(), $foundB[1]->getPort());
     // sort by target since order isn't deterministic
     usort($foundA, function ($a, $b) {
         return strcmp($a->getHost(), $b->getHost());
     });
     // matches expected
     $this->assertEquals($expected[0]->getHost(), $foundA[0]->getHost());
     $this->assertEquals($expected[0]->getPort(), $foundA[0]->getPort());
     $this->assertEquals($expected[1]->getHost(), $foundA[1]->getHost());
     $this->assertEquals($expected[1]->getPort(), $foundA[1]->getPort());
 }
Ejemplo n.º 2
0
 public function resolve($name, $type)
 {
     $record = dns_get_record($name, $type);
     $mapped = array();
     foreach ($record as $address) {
         $mapped[] = Url::fromHostAndPort($address['target'], $address['port']);
     }
     return $mapped;
 }
Ejemplo n.º 3
0
 public function testLoadBalancerLookup()
 {
     // given
     $expected = Url::fromHostAndPort("foo1.fliglio.com", 8001);
     $stubResolver = StubResolver::createSingleResult();
     // when
     $lb = new ConsulLoadBalancer($stubResolver, new RoundRobinLoadBalancerStrategy(), "foo");
     $found = $lb->next();
     // then
     $this->assertEquals($expected->getHost(), $found->getHost());
     $this->assertEquals($expected->getPort(), $found->getPort());
 }
Ejemplo n.º 4
0
 public function testAddressProviderFactory()
 {
     // given
     $expected = Url::fromHostAndPort("foo1.fliglio.com", 8001);
     $stubResolver = StubResolver::createSingleResult();
     // when
     $fac = new AddressProviderFactory($stubResolver);
     $ap = $fac->createConsulAddressProvider("foo");
     $found = $ap->getAddress();
     // then
     $this->assertEquals($expected->getHost(), $found->getHost());
     $this->assertEquals($expected->getPort(), $found->getPort());
 }
Ejemplo n.º 5
0
 public function testRetainQueryString()
 {
     // given
     $app = new UrlLintApp(new StubApp());
     $ctx = $this->createContext(Url::fromString('/foo/bar/'));
     $ctx->getRequest()->setGetParams(['foo' => 'bar', 'baz' => 'bin']);
     // when
     $loc = '';
     try {
         $app->call($ctx);
     } catch (RedirectException $e) {
         $loc = $e->getLocation();
     }
     //then
     $this->assertEquals('/foo/bar?foo=bar&baz=bin', (string) $loc);
 }
Ejemplo n.º 6
0
 public function testSRVLookup()
 {
     // given
     $expected = array(Url::fromHostAndPort("foo1.fliglio.com", 8001), Url::fromHostAndPort("foo2.fliglio.com", 8002));
     // when
     $resv = new DnsResolver();
     $found = $resv->resolve("foo.service.fliglio.com", DNS_SRV);
     // sort by target since order isn't deterministic
     usort($found, function ($a, $b) {
         return strcmp($a->getHost(), $b->getHost());
     });
     // then
     $this->assertEquals($expected[0]->getHost(), $found[0]->getHost());
     $this->assertEquals($expected[1]->getHost(), $found[1]->getHost());
     $this->assertEquals(count($expected), count($found));
 }
Ejemplo n.º 7
0
 public function call(Context $context)
 {
     $currentUrl = $context->getRequest()->getUrl();
     $currentMethod = $context->getRequest()->getHttpMethod();
     if ($currentMethod == Http::METHOD_GET) {
         $lintedPath = $this->lintPath($currentUrl);
         if ((string) $currentUrl != $lintedPath) {
             $protocol = $context->getRequest()->getProtocol();
             $host = $context->getRequest()->getHost();
             if ($this->redirect) {
                 $url = Url::fromParts(['scheme' => $protocol, 'host' => $host, 'path' => $lintedPath, 'query' => $this->arrayToQuery($context->getRequest()->getGetParams())]);
                 throw new RedirectException("Linting Url and redirecting browser", 301, $url);
             } else {
                 $context->getRequest()->setUrl($lintedPath);
             }
         }
     }
     $this->wrappedApp->call($context);
 }
Ejemplo n.º 8
0
 public function call(Context $context)
 {
     $currentHost = $context->getRequest()->getHost();
     $currentProtocol = $context->getRequest()->getProtocol();
     $currentUrl = $context->getRequest()->getUrl();
     $currentMethod = $context->getRequest()->getHttpMethod();
     // Identify current Command; register RouteMap & params with Context
     $route;
     try {
         $route = $this->routeMap->getRoute($context->getRequest());
     } catch (RouteException $e) {
         throw new NotFoundException(sprintf("Route not found for request: %s %s://%s%s", $currentMethod, $currentProtocol, $currentHost, $currentUrl));
     }
     $context->setProp(self::CURRENT_ROUTE, $route);
     // Force pages to their designated protocol if specified
     if ($route->getProtocol() != null) {
         if ($currentProtocol != $route->getProtocol()) {
             $url = Url::fromString(sprintf("%s://%s/", $route->getProtocol(), $currentHost))->join($currentUrl)->addParams($_GET);
             throw new RedirectException('Change Protocol', 301, $url);
         }
     }
     $this->wrappedApp->call($context);
 }
Ejemplo n.º 9
0
 public static function createDoubleResult()
 {
     $stubSrv = array(Url::fromHostAndPort("foo1.fliglio.com", 8001), Url::fromHostAndPort("foo2.fliglio.com", 8002));
     return new self($stubSrv);
 }
Ejemplo n.º 10
0
 private static function getCurrentUrl()
 {
     $urlParts = parse_url($_SERVER['REQUEST_URI']);
     $str = '/' . ltrim($urlParts['path'], '/');
     return Url::fromString($str);
 }