Skip to content

Commit

Permalink
Be able to map the checked URL
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Feb 15, 2019
1 parent 78d6570 commit cfb7e4b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc/developer/server_side.rst.mako
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ To run a specific test use the ``-k`` switch. For example:

.. prompt:: bash

./docker-compose-run py.tests -k test_catalogue geoportal/tests
./docker-compose-run py.test -k test_catalogue geoportal/tests

Profiling
---------
Expand Down
22 changes: 14 additions & 8 deletions geoportal/c2cgeoportal_geoportal/lib/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,32 @@
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.

import requests
import logging
import subprocess
from time import sleep
from urllib.parse import urlsplit, urlunsplit

import requests

log = logging.getLogger(__name__)


def build_url(name, url, request, headers=None):
settings = {
elem['netloc']: elem for elem in request.registry.settings["checker"].get("host_map", [])
}
url_fragments = urlsplit(url)
headers = _build_headers(request, headers)
if url_fragments.netloc == request.environ.get("SERVER_NAME") or \
url_fragments.netloc.startswith("localhost:"):
netloc = '*' if '*' in settings and url_fragments.netloc not in settings else url_fragments.netloc
if netloc in settings:
mapped_host = settings[netloc]
url_ = urlunsplit((
"http", "localhost", url_fragments.path, url_fragments.query, url_fragments.fragment
mapped_host['to'].get('schema', url_fragments.scheme),
mapped_host['to'].get('netloc', url_fragments.netloc),
url_fragments.path, url_fragments.query, url_fragments.fragment
))
headers["Host"] = url_fragments.netloc
if mapped_host.get('forward_host', False):
headers["Host"] = url_fragments.netloc
else:
url_ = url

Expand Down Expand Up @@ -228,9 +236,7 @@ def _phantomjs(settings, health_check):

def check(request):
url = request.route_url(route["name"], _query=route.get("params", {}))
if urlsplit(url).netloc.startswith("localhost:"):
# For Docker
url = build_url("Check", url, request)["url"]
url = build_url("Check", url, request)["url"]

cmd = [
"phantomjs", "--local-to-remote-url-access=true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ vars:
backend: dogpile.cache.memory_pickle

checker:
host_map: []
phantomjs:
routes: []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,24 @@ mapping:
type: map
required: True
mapping:
host_map:
type: seq
sequence:
- type: map
mapping:
netloc:
type: str
required: True
to:
type: map
required: True
mapping:
schema:
type: str
netloc:
type: str
forward_host:
type: bool
fulltextsearch:
type: map
required: True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ vars:

# Checker configuration
checker:
host_map:
- netloc: '*'
to:
schema: http
netloc: localhost:8080
fulltextsearch:
level: 1
search: text to search
Expand Down
33 changes: 27 additions & 6 deletions geoportal/tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,37 @@
class TestExportCSVView(TestCase):

@staticmethod
def _create_dummy_request():
def _create_dummy_request(netloc="example.com", forward_host=True):
request = DummyRequest()
request.environ = {
"SERVER_NAME": "example.com"
request.registry.settings = {
"checker": {
"host_map": [{
"netloc": netloc,
"to": {
"netloc": "localhost",
"schema": "http",
},
"forward_host": forward_host
}]
}
}
return request

def test_build_url_star(self):
self.assertEqual(
build_url(
"Test",
"http://example.com/toto?titi#tutu",
self._create_dummy_request("*", False)
),
{
"url": "http://localhost/toto?titi#tutu",
"headers": {
"Cache-Control": "no-cache",
}
}
)

def test_build_url_http(self):
self.assertEqual(
build_url(
Expand Down Expand Up @@ -94,9 +118,6 @@ def test_build_url_other(self):

def test_build_url_forward_headers(self):
request = DummyRequest()
request.environ = {
"SERVER_NAME": "example.com"
}
request.registry.settings = {
"checker": {
"forward_headers": ["Cookie"]
Expand Down

0 comments on commit cfb7e4b

Please sign in to comment.