<commit_before># No shebang line, this module is meant to be imported
#
# Copyright 2013 <NAME>
#
# 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 re

from twisted.web.server import NOT_DONE_YET
from twisted.internet.defer import succeed
from twisted.web.test.test_web import DummyRequest as _DummyRequest
from twisted.trial.unittest import TestCase as _TestCase


def dummy_request(path="", http_method="GET", headers=None, session=None):
    """
    Wrapper around the base dummy request which does not require direct
    subclassing for different kinds of request or lists for a single
    url request.
    """
    class DummyRequest(_DummyRequest):
        method = http_method

    request = DummyRequest([path], session=session)
    if isinstance(headers, dict):
        for key, value in headers.iteritems():
            request.setHeader(key, value)
    return request
<commit_msg>expanded dummy request to handle request data<commit_after># No shebang line, this module is meant to be imported
#
# Copyright 2013 <NAME>
#
# 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 re
from StringIO import StringIO

try:
    from json import dumps
except ImportError:
    from simplejson import dumps

from twisted.web.server import NOT_DONE_YET
from twisted.internet.defer import succeed
from twisted.web.test.test_web import DummyRequest as _DummyRequest
from twisted.trial.unittest import TestCase as _TestCase


def dummy_request(path="", data=None, http_method="GET", headers=None,
                  session=None):
    """
    Wrapper around the base dummy request which does not require direct
    subclassing for different kinds of request or lists for a single
    url request.
    """
    class FakeContent(object):
        def __init__(self):
            self._content = None
            self._read = False

        def read(self):
            if self._read is False:
                self._read = True
                return self._content

        def write(self, data):
            self._content = dumps(data)

    class DummyRequest(_DummyRequest):
        def __init__(self, postpath, session=None):
            _DummyRequest.__init__(self, postpath, session=session)
            self.method = http_method
            self.requestHeaders = {}
            self.content = FakeContent()

        def getHeader(self, name):
            return self.requestHeaders.get(name)

        def setHeader(self, name, value):
            self.requestHeaders[name] = value

    request = DummyRequest([path], session=session)
    if isinstance(headers, dict):
        for key, value in headers.iteritems():
            request.setHeader(key, value)

    if data is not None:
        request.content.write(data)

    return request

{'fraction_non_alphanumeric': 0.07263468239077996, 'fraction_numerical': 0.004288394532296971, 'mean_word_length': 3.1932584269662923, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1496491', 'n_tokens_mistral': 985, 'n_tokens_neox': 956, 'n_words': 396}