<commit_before># Copyright 2014, 2015 Red Hat
#
# Authors: <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json
import os
import re

import jsonpath_rw
import testtools
from six.moves.urllib import parse as urlparse


class HTTPTestCase(testtools.TestCase):
    """Encapsulate a single HTTP request as a TestCase.

    If the test is a member of a sequence of requests, ensure that prior
    tests are run.

    To keep the test harness happy we need to make sure the setUp and
    tearDown are only run once.
    """

    def setUp(self):
        if not self.has_run:
            super(HTTPTestCase, self).setUp()

    def tearDown(self):
        if not self.has_run:
            super(HTTPTestCase, self).tearDown()
        self.has_run = True

    def test_request(self):
        """Run this request if it has not yet run.

        If there is a prior test in the sequence, run it first.
        """
        if self.has_run:
            return
        if self.prior and not self.prior.has_run:
            self.prior.run()
        self._run_test()

    def _assert_response(self, response, content, status, headers=None,
                         expected=None, json_paths=None):
        """Compare the response with expected data."""

        # Never accept a 500
        if response['status'] == '500':
            raise ServerError(content)

        # Always test status
        self._test_status(status, response['status'])

        if headers:
            self._test_headers(headers, response)

        decoded_output = self._decode_content(response, content)

        # Compare strings in response body
        if expected:
            for expect in expected:
                self.assertIn(expect, decoded_output)

        # Decode body as JSON and compare.
        # NOTE(chdent): This just here for now to see if it is workable.
        if json_paths:
            response_data = json.loads(decoded_output)
            self.json_data = response_data
            for path in json_paths:
                match = self._extract_json_path_value(response_data, path)
                self.assertEqual(json_paths[path], match)

    def _decode_content(self, response, content):
        """Decode content to a proper string."""
        content_type = response.get('content-type',
                                    'application/binary').lower()
        if ';' in content_type:
            content_type, charset = (attr.strip() for attr in
                                     content_type.split(';'))
            charset = charset.split('=')[1].strip()
        else:
            charset = 'utf-8'

        if self._not_binary(content_type):
            return content.decode(charset)
        else:
            return content

    @staticmethod
    def _extract_json_path_value(data, path):
        """Extract the value at JSON Path path from the data.

        The input data is a Python datastructre, not a JSON string.
        """
        path_expr = jsonpath_rw.parse(path)
        matches = [match.value for match in path_expr.find(data)]
        return matches[0]

    def _load_data_file(self, filename):
        """Read a file from the current test directory."""
        path = os.path.join(self.test_directory, filename)
        with open(path, mode='rb') as data_file:
            return data_file.read()

    @staticmethod
    def _not_binary(content_type):
        """Decide if something is content we'd like to treat as a string."""
        return (content_type.startswith('text/')
                or content_type.endswith('+xml')
                or content_type.endswith('+json')
                or content_type == 'application/javascript'
                or content_type == 'application/json')

    def _parse_url(self, url, ssl=False):
        """Create a url from test data.

        If provided with a full URL, just return that. If SSL is requested
        set the scheme appropriately.

        Scheme and netloc are saved for later use in comparisons.
        """
        parsed_url = urlparse.urlsplit(url)<commit_msg>Constrain the data file being loaded via yaml.<commit_after># Copyright 2014, 2015 Red Hat
#
# Authors: <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json
import os
import re

import jsonpath_rw
import testtools
from six.moves.urllib import parse as urlparse


class HTTPTestCase(testtools.TestCase):
    """Encapsulate a single HTTP request as a TestCase.

    If the test is a member of a sequence of requests, ensure that prior
    tests are run.

    To keep the test harness happy we need to make sure the setUp and
    tearDown are only run once.
    """

    def setUp(self):
        if not self.has_run:
            super(HTTPTestCase, self).setUp()

    def tearDown(self):
        if not self.has_run:
            super(HTTPTestCase, self).tearDown()
        self.has_run = True

    def test_request(self):
        """Run this request if it has not yet run.

        If there is a prior test in the sequence, run it first.
        """
        if self.has_run:
            return
        if self.prior and not self.prior.has_run:
            self.prior.run()
        self._run_test()

    def _assert_response(self, response, content, status, headers=None,
                         expected=None, json_paths=None):
        """Compare the response with expected data."""

        # Never accept a 500
        if response['status'] == '500':
            raise ServerError(content)

        # Always test status
        self._test_status(status, response['status'])

        if headers:
            self._test_headers(headers, response)

        decoded_output = self._decode_content(response, content)

        # Compare strings in response body
        if expected:
            for expect in expected:
                self.assertIn(expect, decoded_output)

        # Decode body as JSON and compare.
        # NOTE(chdent): This just here for now to see if it is workable.
        if json_paths:
            response_data = json.loads(decoded_output)
            self.json_data = response_data
            for path in json_paths:
                match = self._extract_json_path_value(response_data, path)
                self.assertEqual(json_paths[path], match)

    def _decode_content(self, response, content):
        """Decode content to a proper string."""
        content_type = response.get('content-type',
                                    'application/binary').lower()
        if ';' in content_type:
            content_type, charset = (attr.strip() for attr in
                                     content_type.split(';'))
            charset = charset.split('=')[1].strip()
        else:
            charset = 'utf-8'

        if self._not_binary(content_type):
            return content.decode(charset)
        else:
            return content

    @staticmethod
    def _extract_json_path_value(data, path):
        """Extract the value at JSON Path path from the data.

        The input data is a Python datastructre, not a JSON string.
        """
        path_expr = jsonpath_rw.parse(path)
        matches = [match.value for match in path_expr.find(data)]
        return matches[0]

    def _load_data_file(self, filename):
        """Read a file from the current test directory."""
        path = os.path.join(self.test_directory, os.path.basename(filename))
        with open(path, mode='rb') as data_file:
            return data_file.read()

    @staticmethod
    def _not_binary(content_type):
        """Decide if something is content we'd like to treat as a string."""
        return (content_type.startswith('text/')
                or content_type.endswith('+xml')
                or content_type.endswith('+json')
                or content_type == 'application/javascript'
                or content_type == 'application/json')

    def _parse_url(self, url, ssl=False):
        """Create a url from test data.

        If provided with a full URL, just return that. If SSL is requested
        set the scheme appropriately.

        Scheme and netloc are saved for later use in comparisons.
        """
        parsed_url = urlparse.urlsplit(url)
{'fraction_non_alphanumeric': 0.08419562139200523, 'fraction_numerical': 0.004574665069164579, 'mean_word_length': 2.2548741580999647, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5672912', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2390, 'n_words': 878}