devtools/httptest.py
author Julien Cristau <julien.cristau@logilab.fr>
Tue, 06 Oct 2015 11:19:37 +0200
changeset 10781 e2ab20561932
parent 10609 e2d8e81bfe68
child 11014 9c9f5e913f9c
permissions -rw-r--r--
[devtools] make httptest module importable under py3k
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10235
684215aca046 Remove remote repository-access-through-pyro support
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     3
#
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     5
#
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     9
# any later version.
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    10
#
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    14
# details.
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    15
#
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    18
"""this module contains base classes and utilities for integration with running
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    19
http server
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    20
"""
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10363
diff changeset
    21
from __future__ import print_function
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10363
diff changeset
    22
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    23
__docformat__ = "restructuredtext en"
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    24
9136
eef0388e8fea [devtool] randomise available ports search in http test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8941
diff changeset
    25
import random
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    26
import threading
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    27
import socket
10603
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    28
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10604
diff changeset
    29
from six.moves import range, http_client
10603
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    30
from six.moves.urllib.parse import urlparse
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    31
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    32
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    33
from cubicweb.devtools.testlib import CubicWebTC
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    34
from cubicweb.devtools import ApptestConfiguration
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    35
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    36
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    37
def get_available_port(ports_scan):
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    38
    """return the first available port from the given ports range
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    39
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    40
    Try to connect port by looking for refused connection (111) or transport
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    41
    endpoint already connected (106) errors
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    42
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    43
    Raise a RuntimeError if no port can be found
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    44
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    45
    :type ports_range: list
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    46
    :param ports_range: range of ports to test
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    47
    :rtype: int
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    48
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    49
    .. see:: :func:`test.test_support.bind_port`
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    50
    """
9136
eef0388e8fea [devtool] randomise available ports search in http test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8941
diff changeset
    51
    ports_scan = list(ports_scan)
eef0388e8fea [devtool] randomise available ports search in http test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8941
diff changeset
    52
    random.shuffle(ports_scan)  # lower the chance of race condition
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    53
    for port in ports_scan:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    54
        try:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    55
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    56
            sock = s.connect(("localhost", port))
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
    57
        except socket.error as err:
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    58
            if err.args[0] in (111, 106):
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    59
                return port
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    60
        finally:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    61
            s.close()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    62
    raise RuntimeError('get_available_port([ports_range]) cannot find an available port')
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    63
6322
711e7e8c69e3 [windmill] initialize windmill registry if not already existing (possible error case in dev)
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6315
diff changeset
    64
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    65
class CubicWebServerConfig(ApptestConfiguration):
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    66
    """basic configuration class for configuring test server
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    67
6438
abae10f81a85 [js test] cleanup, move anonymous_logged handling to the test class (easier to control there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
    68
    Class attributes:
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    69
6438
abae10f81a85 [js test] cleanup, move anonymous_logged handling to the test class (easier to control there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
    70
    * `ports_range`: list giving range of http ports to test (range(7000, 8000)
abae10f81a85 [js test] cleanup, move anonymous_logged handling to the test class (easier to control there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
    71
      by default). The first port found as available in `ports_range` will be
abae10f81a85 [js test] cleanup, move anonymous_logged handling to the test class (easier to control there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
    72
      used to launch the test web server.
abae10f81a85 [js test] cleanup, move anonymous_logged handling to the test class (easier to control there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
    73
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    74
    """
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    75
    ports_range = range(7000, 8000)
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    76
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    77
    def default_base_url(self):
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    78
        port = self['port'] or get_available_port(self.ports_range)
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    79
        self.global_set_option('port', port) # force rewrite here
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    80
        return 'http://127.0.0.1:%d/' % self['port']
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    81
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    82
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    83
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    84
class CubicWebServerTC(CubicWebTC):
6438
abae10f81a85 [js test] cleanup, move anonymous_logged handling to the test class (easier to control there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
    85
    """Class for running test web server. See :class:`CubicWebServerConfig`.
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    86
6438
abae10f81a85 [js test] cleanup, move anonymous_logged handling to the test class (easier to control there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
    87
    Class attributes:
7071
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7066
diff changeset
    88
    * `anonymous_allowed`: flag telling if anonymous browsing should be allowed
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    89
    """
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
    90
    configcls = CubicWebServerConfig
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    91
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    92
    def start_server(self):
10781
e2ab20561932 [devtools] make httptest module importable under py3k
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    93
        from twisted.internet import reactor
e2ab20561932 [devtools] make httptest module importable under py3k
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    94
        from cubicweb.etwist.server import run
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    95
        # use a semaphore to avoid starting test while the http server isn't
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    96
        # fully initilialized
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    97
        semaphore = threading.Semaphore(0)
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    98
        def safe_run(*args, **kwargs):
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    99
            try:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   100
                run(*args, **kwargs)
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   101
            finally:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   102
                semaphore.release()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   103
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   104
        reactor.addSystemEventTrigger('after', 'startup', semaphore.release)
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   105
        t = threading.Thread(target=safe_run, name='cubicweb_test_web_server',
9036
cf519fd876c6 [etwist] pass the repository to the root resource
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   106
                args=(self.config, True), kwargs={'repo': self.repo})
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   107
        self.web_thread = t
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   108
        t.start()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   109
        semaphore.acquire()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   110
        if not self.web_thread.isAlive():
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   111
            # XXX race condition with actual thread death
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   112
            raise RuntimeError('Could not start the web server')
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   113
        #pre init utils connection
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6322
diff changeset
   114
        parseurl = urlparse(self.config['base-url'])
6438
abae10f81a85 [js test] cleanup, move anonymous_logged handling to the test class (easier to control there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
   115
        assert parseurl.port == self.config['port'], (self.config['base-url'], self.config['port'])
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10603
diff changeset
   116
        self._web_test_cnx = http_client.HTTPConnection(parseurl.hostname,
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10603
diff changeset
   117
                                                        parseurl.port)
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   118
        self._ident_cookie = None
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   119
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   120
    def stop_server(self, timeout=15):
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   121
        """Stop the webserver, waiting for the thread to return"""
10781
e2ab20561932 [devtools] make httptest module importable under py3k
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
   122
        from twisted.internet import reactor
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   123
        if self._web_test_cnx is None:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   124
            self.web_logout()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   125
            self._web_test_cnx.close()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   126
        try:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   127
            reactor.stop()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   128
            self.web_thread.join(timeout)
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   129
            assert not self.web_thread.isAlive()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   130
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   131
        finally:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   132
            reactor.__init__()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   133
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   134
    def web_login(self, user=None, passwd=None):
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   135
        """Log the current http session for the provided credential
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   136
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   137
        If no user is provided, admin connection are used.
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   138
        """
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   139
        if user is None:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   140
            user  = self.admlogin
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   141
            passwd = self.admpassword
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   142
        if passwd is None:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   143
            passwd = user
7434
17ef6f9efaa1 [test] fix tests broken by 7427:5338d895b891
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7075
diff changeset
   144
        response = self.web_get("login?__login=%s&__password=%s" %
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   145
                                (user, passwd))
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10603
diff changeset
   146
        assert response.status == http_client.SEE_OTHER, response.status
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   147
        self._ident_cookie = response.getheader('Set-Cookie')
6315
8ca3ee849bee [test] fix broken tests and minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5700
diff changeset
   148
        assert self._ident_cookie
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   149
        return True
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   150
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   151
    def web_logout(self, user='admin', pwd=None):
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   152
        """Log out current http user"""
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   153
        if self._ident_cookie is not None:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   154
            response = self.web_get('logout')
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   155
        self._ident_cookie = None
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   156
8727
5bca35901e9b [devtools/httptest] fix syntax error introduced by ce5ae7b80d2c
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8712
diff changeset
   157
    def web_request(self, path='', method='GET', body=None, headers=None):
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10603
diff changeset
   158
        """Return an http_client.HTTPResponse object for the specified path
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   159
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   160
        Use available credential if available.
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   161
        """
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   162
        if headers is None:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   163
            headers = {}
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   164
        if self._ident_cookie is not None:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   165
            assert 'Cookie' not in headers
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   166
            headers['Cookie'] = self._ident_cookie
8712
ce5ae7b80d2c [devtools/httptest] allow sending other types of requests besides GET
Julien Cristau <julien.cristau@logilab.fr>
parents: 8695
diff changeset
   167
        self._web_test_cnx.request(method, '/' + path, headers=headers, body=body)
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   168
        response = self._web_test_cnx.getresponse()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   169
        response.body = response.read() # to chain request
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   170
        response.read = lambda : response.body
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   171
        return response
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   172
8712
ce5ae7b80d2c [devtools/httptest] allow sending other types of requests besides GET
Julien Cristau <julien.cristau@logilab.fr>
parents: 8695
diff changeset
   173
    def web_get(self, path='', body=None, headers=None):
ce5ae7b80d2c [devtools/httptest] allow sending other types of requests besides GET
Julien Cristau <julien.cristau@logilab.fr>
parents: 8695
diff changeset
   174
        return self.web_request(path=path, body=body, headers=headers)
ce5ae7b80d2c [devtools/httptest] allow sending other types of requests besides GET
Julien Cristau <julien.cristau@logilab.fr>
parents: 8695
diff changeset
   175
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   176
    def setUp(self):
7065
6a6ea9966931 [testlib] simplify code by using a class attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6438
diff changeset
   177
        super(CubicWebServerTC, self).setUp()
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   178
        self.start_server()
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   179
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   180
    def tearDown(self):
10781
e2ab20561932 [devtools] make httptest module importable under py3k
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
   181
        from twisted.internet import error
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   182
        try:
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   183
            self.stop_server()
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   184
        except error.ReactorNotRunning as err:
5654
8bb34548be86 [web test] Add a CubicWebServerTC class to run test with a cw web serveur available.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   185
            # Server could be launched manually
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10363
diff changeset
   186
            print(err)
7065
6a6ea9966931 [testlib] simplify code by using a class attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6438
diff changeset
   187
        super(CubicWebServerTC, self).tearDown()