Пример #1
0
    def test_get_response(self):
        from smoketest.directives import CheckDirective
        from smoketest.settings import get_ca_path
        elem = {
            'url': 'http://www.usnews.com',
        }
        options = Mock()
        options.scheme = None
        options.port = '8999'
        options.level = 'sand14'
        options.cachebust = False
        options.dry_run = False
        directive = CheckDirective(elem, options)
        directive.session = Mock()

        url = 'http://www.usnews.com'
        extra_headers = {'a': 'b'}
        response = directive.get_response(url, extra_headers)
        directive.session.get.assert_called_once_with(
            url,
            verify=get_ca_path(),
            allow_redirects=False,
            timeout=directive.timeout,
            headers=extra_headers
        )
        self.assertEqual(response, directive.session.get.return_value)
Пример #2
0
    def get_response(self, url, extra_headers):
        if self.options.dry_run:
            response = _DummyResponse()
            return response

        response = self.session.get(
            url,
            verify=get_ca_path(),
            allow_redirects=self.follow_redirects,
            timeout=self.timeout,
            headers=extra_headers,
        )
        return response
Пример #3
0
def get_session(elem, options):
    if options.dry_run:
        return _DummySession()

    session = requests.Session()
    if options.user_agent:
        session.headers['User-Agent'] = options.user_agent

    basic_auth_instructions = elem.get('basic_auth_instructions')
    if basic_auth_instructions:
        session.auth = (
            basic_auth_instructions['username'],
            basic_auth_instructions['password'],
        )

    auth_cookie_instructions = elem.get('auth_cookie_instructions')
    if auth_cookie_instructions:
        url = transform_url_based_on_options(
            auth_cookie_instructions['url'],
            options,
        )
        data = auth_cookie_instructions['data']
        try:
            session.post(
                url,
                data=data,
                verify=get_ca_path(),
            )
        except Exception as e:
            raise _SessionError(e.message, url)

        if not bool(session.cookies.values()):
            msg = "Login attempt failed with credentials {0}".format(
                sorted(data.items())
            )
            raise _SessionError(msg, url)

    return session