Ejemplo n.º 1
0
    def test_http_headers_post_header_parse(self):
        """
        Test the META dict value for the request headers.
        """
        request_stream = open(self.request_file, 'r')
        http_request = HttpRequest(request_stream)

        #Confirm Request Headers
        http_request.parse_request_header()
        request_headers = http_request.META[MetaDict.Info.REQ_HEADERS]
        self.assertEqual("www.knowhere123.com", http_request.get_host())
        self.assertListEqual(["image/gif, image/jpeg, */*"],
                             request_headers.getlist('Accept'))
        self.assertListEqual(["en-us"],
                             request_headers.getlist('Accept-Language'))
        self.assertListEqual(["gzip, deflate"],
                             request_headers.getlist('Accept-Encoding'))
        self.assertListEqual([
            "cookie1=value1, cookie2=value2", "cookie3=value3, cookie4=value4"
        ], request_headers.getlist('Cookies'))
        self.assertListEqual(
            ["Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"],
            request_headers.getlist('User-Agent'))
        self.assertListEqual(["830543"],
                             request_headers.getlist('Content-Length'))
        self.assertListEqual(["image/gif, image/jpeg, */*"],
                             request_headers.getlist('Accept'))
        self.assertListEqual(["830543"],
                             request_headers.getlist('Content-Length'))
        self.assertEqual("multipart/form-data", http_request.content_type)
        self.assertIsNone(http_request.content_params)

        #close the file/stream
        request_stream.close()
Ejemplo n.º 2
0
 def test_httprequest_repr(self):
     request = HttpRequest()
     request.path = '/somepath/'
     request.method = 'GET'
     request.GET = {'get-key': 'get-value'}
     request.POST = {'post-key': 'post-value'}
     request.COOKIES = {'post-key': 'post-value'}
     request.META = {'post-key': 'post-value'}
     self.assertEqual(repr(request), "<HttpRequest: GET '/somepath/'>")
Ejemplo n.º 3
0
 def test_httprequest_repr_invalid_method_and_path(self):
     request = HttpRequest()
     self.assertEqual(repr(request), "<HttpRequest>")
     request = HttpRequest()
     request.method = "GET"
     self.assertEqual(repr(request), "<HttpRequest>")
     request = HttpRequest()
     request.path = ""
     self.assertEqual(repr(request), "<HttpRequest>")
Ejemplo n.º 4
0
def requestparser():
    curr_dir = "tests/request parse test files/"
    get_request1 = "get-request1.txt"
    get_request1 = curr_dir + get_request1
    get_request1 = get_abs_path(get_request1)

    stream1 = ''

    with open(get_request1, 'r') as stream1:
        try:
            http_get_request1 = HttpRequest(stream1)
            http_get_request1.parse_request_header()
            print "Method: " + http_get_request1.method
            print "Scheme is: " + http_get_request1.scheme
            print "Path is: " + http_get_request1.path
            print "Protocol info: " + http_get_request1.protocol_info
            print "Host is: " + http_get_request1.get_host()
            print "Port is: " + http_get_request1.get_port()
            print "Encoding is: " + http_get_request1.encoding
            print "Content-Type is: " + http_get_request1.content_type
            print "Request URI is: " + http_get_request1.get_uri(raw=True)
            print "GET query string dict is: "
            print http_get_request1.GET
            print "Cookies are: "
            print http_get_request1.META[MetaDict.Info.REQ_HEADERS].getlist(
                "Cookies")
            print "Request headers are: "
            print http_get_request1.META['REQUEST_HEADERS']
            print "Request is: "
            print http_get_request1

        except Exception as e:
            print "Exception is: {}".format(e)
Ejemplo n.º 5
0
    def test_http_request_line(self):
        """
        Test the request line.
        """
        request_stream = open(self.request_file, 'r')
        http_request = HttpRequest(request_stream)
        http_request.parse_request_header()

        #Request line meta data check
        self.assertEqual("PUT", http_request.get_method())
        self.assertEqual("UNKNOWN", http_request.get_scheme())
        self.assertEqual("/caf%C3%A9/upload", http_request.get_path())
        self.assertEqual("HTTP/1.1", http_request.get_protocol_info())
        self.assertEqual("65536", http_request.get_port())

        #close the file/stream
        request_stream.close()
Ejemplo n.º 6
0
 def test_httprequest_full_path(self):
     request = HttpRequest()
     request.path = '/;some/?awful/=path/foo:bar/'
     request.path_info = '/prefix' + request.path
     request.META['QUERY_STRING'] = ';some=query&+query=string'
     expected = '/%3Bsome/%3Fawful/%3Dpath/foo:bar/?;some=query&+query=string'
     self.assertEqual(request.get_full_path(), expected)
Ejemplo n.º 7
0
 def test_post_process_body_read(self):
     """
     Read body for a non text/plain request after parse_request_body().
     """
     http_request_stream = open(self.put_request_multipart_file, 'r')
     http_request = HttpRequest(http_request_stream)
     http_request.parse_request_header()
     http_request.parse_request_body()
     with self.assertRaises(RawPostDataException) as rPDE_Exception:
         body = http_request.body()
     self.assertEquals(
         "You cannot access raw body after reading from request's data stream.",
         rPDE_Exception.exception.args[0])
Ejemplo n.º 8
0
    def __init__(self, file_upload_path=None, requests=[], protocol_list=[]):
        self.sys_path = str(sys.path)
        self.sys_environ = str(os.environ)
        #print "Sys path is "+self.sys_path
        #print "OS environ dict:\n"+self.sys_environ

        self.requests = []
        index = 0
        settings = None
        if file_upload_path is not None:
            settings = Settings({Settings.Key.FILE_UPLOAD_DIR : file_upload_path})
        
        for request_bytes in requests:
            request_stream = BytesIO(request_bytes)
            http_request = HttpRequest(request_stream=request_stream, settings=settings)
            #parse the request
            http_request.parse_request_header()
            if http_request.get_scheme() == 'UNKNOWN':
                http_request.set_scheme(protocol_list[index])
            http_request.parse_request_body()
            self.requests.append(http_request)
            index += 1
Ejemplo n.º 9
0
    def test_http_request_path_metadata_reset(self):
        """
        Test (re)set of meta data post request header processing.
        """
        request_stream = open(self.request_file, 'r')
        http_request = HttpRequest(request_stream)
        http_request.parse_request_header()

        #URI/path string (excluding querys tring) set/reset test
        self.assertEqual("UNKNOWN://www.knowhere123.com/caf%C3%A9/upload",
                         http_request.get_uri())
        new_international_path = "/سلام/this%/is$*()$!@/a/new/path/Name/Müeller"
        http_request.set_path(new_international_path)
        self.assertEqual(
            "UNKNOWN://www.knowhere123.com/%D8%B3%D9%84%D8%A7%D9%85/this%/is$*()$!@/a/new/path/Name/M%C3%BCeller",
            http_request.get_uri())
        self.assertEqual(
            "UNKNOWN://www.knowhere123.com/سلام/this%/is$*()$!@/a/new/path/Name/Müeller",
            http_request.get_uri(raw=True))
        #print http_request.get_uri()

        #close the file/stream
        request_stream.close()
Ejemplo n.º 10
0
    def test_http_request_url_reconstruct(self):
        """
        Test reconstructing the original request path with meta data.
        """
        request_stream = open(self.request_file, 'r')
        http_request = HttpRequest(request_stream)
        http_request.parse_request_header()

        #URL encoded UTF-8
        self.assertEqual("UNKNOWN://www.knowhere123.com/caf%C3%A9/upload",
                         http_request.get_uri())
        #get RAW URI
        #café here is UTF-8 encoded, so when get_uri(raw=True) returns,
        #the representation of the returned value should be same as the UTF-8
        #representation of café
        self.assertEqual("UNKNOWN://www.knowhere123.com/café/upload",
                         http_request.get_uri(raw=True))
        self.assertFalse(http_request.is_ajax())
        self.assertFalse(http_request.is_secure())

        #close the file/stream
        request_stream.close()
Ejemplo n.º 11
0
    def parse_and_build_dom(self,
                            request_file_names=[],
                            type=Type.form_request,
                            target_type=TargetType.iframe,
                            auto_submit=True):
        """
        Method that takes care of the most repeated part - parse requests and generate the request DOM.
        """

        request_dom = None
        if request_file_names is None or len(request_file_names) == 0:
            return request_dom

        request_streams = []
        requests_raw = []

        #create streams out of file_names
        for index, request_file_name in enumerate(request_file_names):
            request_file_name = self.get_abs_path_file(request_file_name)
            request_file_names[index] = request_file_name
            request_stream = open(request_file_name, 'r')
            request_streams.append(request_stream)

        #create requests handle for each of the stream
        for request_stream in request_streams:
            request = HttpRequest(request_stream=request_stream)
            requests_raw.append(request)

        #start parsing each of the request
        for request in requests_raw:
            request.parse_request_header()
            request.parse_request_body()

        #generate request DOM for the combined requests
        request_dom = HtmlRequestBuilder(requests=requests_raw)

        #trigger build
        request_dom.build(type=type,
                          target_type=target_type,
                          auto_submit=auto_submit)

        #for each of the stream close them
        for request_stream in request_streams:
            request_stream.close()

        #return the built DOM ready
        #for generation
        return request_dom
Ejemplo n.º 12
0
    def test_empty_request_stream(self):
        """
        Empty request/init test.
        """
        request = HttpRequest(None)
        self.assertEqual(list(request.GET), [])
        self.assertEqual(list(request.POST), [])
        self.assertEqual(list(request.META), [])

        # .GET and .POST should be QueryDicts
        self.assertEqual(request.GET.urlencode(), '')
        self.assertEqual(request.POST.urlencode(), '')

        # and FILES should be MultiValueDict
        self.assertEqual(request.FILES.getlist('foo'), [])

        self.assertIsNone(request.method)
        self.assertIsNone(request.scheme)
        self.assertIsNone(request.host)
        self.assertIsNone(request.port)
        self.assertIsNone(request.path)
        self.assertIsNone(request.protocol_info)
        self.assertIsNone(request.content_type)
        self.assertIsNone(request.content_params)
Ejemplo n.º 13
0
    def test_request_multipart_request(self):
        """
        Test a complex request with multipart-form-data body.
        """
        multipart_request_stream = open(self.put_request_multipart_file, 'r')

        #get a request handle
        multipart_request = HttpRequest(multipart_request_stream)
        multipart_request.parse_request_header()
        multipart_request.parse_request_body()

        #request data that should be present
        _POST = QueryDict(Settings.default(), mutable=True)
        _POST['id'] = {
            'data': '123e4567-e89b-12d3-a456-426655440000',
            'content-type': 'text/plain',
            'transfer-encoding': '',
            'content-type-extra': {}
        }
        _POST['address'] = {
            'data':
            '{\r\n  \"street\": \"3, Garden St\",\r\n  \"city\": \"Hillsbery, UT\"\r\n}',
            'content-type': 'application/json',
            'transfer-encoding': '',
            'content-type-extra': {}
        }
        _GET = QueryDict(Settings.default(), mutable=True)

        #test
        self.assertDictEqual(_POST, multipart_request.POST)
        self.assertDictEqual(_GET, multipart_request.GET)
        self.assertIn('profileImage', multipart_request.FILES)
        _file = multipart_request.FILES.get('profileImage')
        #assert the transfer encoding
        self.assertEqual(u'base64', _file.transfer_encoding)
        #we create a string equivalent to base64 encoding the first 100 bytes from the raw file.
        first_100_bytes_b64 = '/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIfIiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/2wBDAQoLCw4NDg=='
        #we assert that it is equal to the same that can be obtained by reading the file
        self.assertEquals(first_100_bytes_b64,
                          base64.b64encode(_file.read(100)))

        #close it out
        multipart_request_stream.close()
Ejemplo n.º 14
0
def requestparser():
    curr_dir = "request parse test files"
    curr_dir = get_abs_path(curr_dir)

    put_multipart_request = "complex-request1.txt"
    put_multipart_request = curr_dir + put_multipart_request
    stream = ''

    with open(put_multipart_request, 'r') as stream:
        try:
            http_get_request1 = HttpRequest(stream)
            #http_get_request1.parse_request_header()
            body = http_get_request1.parse()
            print "Method: " + http_get_request1.method
            print "Scheme is: " + http_get_request1.scheme
            print "Path is: " + http_get_request1.path
            print "Protocol info: " + http_get_request1.protocol_info
            print "Host is: " + http_get_request1.get_host()
            print "Port is: " + http_get_request1.get_port()
            print "Encoding is: " + http_get_request1.encoding
            print "Content-Type is: " + http_get_request1.content_type
            print "Request URI is: " + http_get_request1.get_uri()
            print "GET query string dict is: "
            print http_get_request1.GET
            print "Cookies are: "
            print http_get_request1.META[MetaDict.Info.REQ_HEADERS].getlist(
                "Cookies")
            print "Request headers are: "
            print http_get_request1.META['REQUEST_HEADERS']
            print "Request is: "
            print http_get_request1
            files = http_get_request1.FILES
            print_files_details(files)
            #print "Body is: "+body

        except Exception as e:
            print "Exception is: {}".format(e)
Ejemplo n.º 15
0
    def test_http_request_encoding_and_bodystream_metadata_reset(self):
        """
        This test covers the encoding set/reset and the body_stream reset
        cases.
        """
        #charset/encoding reset test
        encoded_body_dir = "tests/request parse test files/encoded body"
        encoded_body_dir = get_abs_path(encoded_body_dir)

        iso_88591_1_file = "ISO-8859-1-Barca.txt"
        utf8_file = "UTF8-Barca.txt"
        utf16_BEBOM_file = "UTF16 BEBOM-Barca.txt"

        iso_88591_1_file = join(encoded_body_dir, iso_88591_1_file)
        utf8_file = join(encoded_body_dir, utf8_file)
        utf16_BEBOM_file = join(encoded_body_dir, utf16_BEBOM_file)

        iso_88591_1_encoding = "ISO-8859-1"
        utf8_encoding = "UTF-8"
        utf16_BEBOM_encoding = "UTF-16"

        #an http_request
        request_stream = open(self.request_file, 'r')
        http_request = HttpRequest(request_stream)
        http_request.parse_request_header()

        #ISO-88591-1
        #reset content-type and encoding
        iso_88591_1_body = open(iso_88591_1_file, 'r')
        http_request.content_type = "text/plain"
        http_request.encoding = iso_88591_1_encoding.lower()
        #set the request_body stream
        http_request.body_stream = iso_88591_1_body
        http_request.parse_request_body()

        #check if the request body was properly decoded as ISO-8859-1
        _body_file = open(iso_88591_1_file, 'r')
        _body_bytes = _body_file.read()
        _body_bytes = _body_bytes.decode(iso_88591_1_encoding.lower())
        http_body = http_request.body()
        #the body should be in the encoding specified in the request
        self.assertEqual(_body_bytes, http_body)

        #UTF-16
        #reset content-type and encoding
        utf16_BEBOM_body = open(utf16_BEBOM_file, 'r')
        http_request.content_type = "text/plain"
        http_request.encoding = utf16_BEBOM_encoding.lower()
        #set the request_body stream
        http_request.body_stream = utf16_BEBOM_body
        http_request.parse_request_body()

        #check if the request body was properly decoded as ISO-8859-1
        _body_file = open(utf16_BEBOM_file, 'r')
        _body_bytes = _body_file.read()
        _body_bytes = _body_bytes.decode(utf16_BEBOM_encoding.lower())
        http_body = http_request.body()
        #the body should be in the encoding specified in the request
        self.assertEqual(_body_bytes, http_body)

        #UTF-8
        #reset content-type and encoding
        utf8_body = open(utf8_file, 'r')
        http_request.content_type = "text/plain"
        http_request.encoding = utf8_encoding.lower()
        #set the request_body stream
        http_request.body_stream = utf8_body
        http_request.parse_request_body()

        #check if the request body was properly decoded as ISO-8859-1
        _body_file = open(utf8_file, 'r')
        _body_bytes = _body_file.read()
        _body_bytes = _body_bytes.decode(utf8_encoding.lower())
        http_body = http_request.body()
        #the body should be in the encoding specified in the request
        self.assertEqual(_body_bytes, http_body)

        #close the file/stream
        request_stream.close()
Ejemplo n.º 16
0
    def test_request_query_string(self):
        #get file stream
        get_request_with_query_stream = open(self.get_request_with_query, 'r')

        get_request_with_query = HttpRequest(get_request_with_query_stream)
        get_request_with_query.parse_request_header()
        get_request_with_query.parse_request_body()
        # Use of dict() had to be changed to QueryDict() as the test seem to be failing on Jython
        # where full repr(obj) seems to be used to perform equality of 2 dicts but for print calls
        # only the proper doct() level repr() call is used.
        # By using QueryDict(), we can maintain Jython compatibility
        request_GET = QueryDict(Settings.default(), mutable=True)
        request_GET['source'] = 'hp'
        request_GET['ei'] = 'H8jpXI_lN4OiswXa-oOwAw'
        request_GET['q'] = 'asdfadsf'
        request_GET['oq'] = 'asdfadsf'
        request_GET[
            'gs_l'] = 'psy-ab.12..0j0i10l3j0j0i10l5.1255.1577..2445...0.0..1.153.972.2j6......0....1..gws-wiz.....0..0i131.DPwpRijoAMc'
        self.assertDictEqual(request_GET, get_request_with_query.GET)
        self.assertDictEqual(QueryDict(Settings.default(), mutable=True),
                             get_request_with_query.POST)

        #close get request
        get_request_with_query_stream.close()

        #get file stream
        post_request_with_query_stream = open(
            self.post_request_with_query_file, 'r')

        post_request_with_query = HttpRequest(post_request_with_query_stream)
        post_request_with_query.parse_request_header()
        post_request_with_query.parse_request_body()
        request_POST = QueryDict(Settings.default(), mutable=True)
        request_POST['source'] = 'hp'
        request_POST['ei'] = 'H8jpXI_lN4OiswXa-oOwAw'
        request_POST['q'] = 'asdfadsf'
        request_POST['oq'] = 'asdfadsf'
        request_POST[
            'gs_l'] = 'psy-ab.12..0j0i10l3j0j0i10l5.1255.1577..2445...0.0..1.153.972.2j6......0....1..gws-wiz.....0..0i131.DPwpRijoAMc'
        self.assertDictEqual(request_POST, post_request_with_query.POST)
        self.assertDictEqual(QueryDict(Settings.default(), mutable=True),
                             post_request_with_query.GET)

        #close request file
        post_request_with_query_stream.close()
Ejemplo n.º 17
0
    def test_http_request_stream_set(self):
        request_stream = open(self.request_file, 'r')
        http_request = HttpRequest(request_stream)

        #Confirm Request Headers
        http_request.parse_request_header()
        request_headers = http_request.META[MetaDict.Info.REQ_HEADERS]
        self.assertEqual("www.knowhere123.com", http_request.get_host())
        self.assertListEqual(["image/gif, image/jpeg, */*"],
                             request_headers.getlist('Accept'))
        self.assertListEqual(["en-us"],
                             request_headers.getlist('Accept-Language'))
        self.assertListEqual(["gzip, deflate"],
                             request_headers.getlist('Accept-Encoding'))
        self.assertListEqual([
            "cookie1=value1, cookie2=value2", "cookie3=value3, cookie4=value4"
        ], request_headers.getlist('Cookies'))
        self.assertListEqual(
            ["Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"],
            request_headers.getlist('User-Agent'))
        self.assertListEqual(["830543"],
                             request_headers.getlist('Content-Length'))
        self.assertEqual("multipart/form-data", http_request.content_type)
        self.assertIsNone(http_request.content_params)

        #another request file
        another_test_file = "get-request1.txt"
        another_test_file = join(self.test_files_dir, another_test_file)
        another_test_file_stream = open(another_test_file, 'r')
        http_request.stream = another_test_file_stream

        #Confirm new Request Headers
        http_request.parse_request_header()
        http_request.parse_request_body()
        request_headers = http_request.META[MetaDict.Info.REQ_HEADERS]
        self.assertEqual("www.knowhere484.com", http_request.get_host())
        self.assertListEqual(["en-us"],
                             request_headers.getlist('Accept-Language'))
        self.assertListEqual(["gzip, deflate"],
                             request_headers.getlist('Accept-Encoding'))
        self.assertListEqual(["cookie3=value3, cookie4=value4"],
                             request_headers.getlist('Cookies'))
        self.assertListEqual(["Safari/4.0 (compatible; MSIE5.01; Linux Blah)"],
                             request_headers.getlist('User-Agent'))
        self.assertEqual("application/x-www-form-urlencoded",
                         http_request.content_type)
        self.assertIsNone(http_request.content_params)

        #close the file/stream
        request_stream.close()
        another_test_file_stream.close()
Ejemplo n.º 18
0
    def test_invalid_request_body(self):
        """
        Test request body.
        """
        http_request_stream = open(self.put_request_multipart_file, 'r')
        http_request = HttpRequest(http_request_stream)
        http_request.parse_request_header()

        #Change Content-Type
        request_headers = http_request.META['REQUEST_HEADERS']
        request_headers._mutable = True
        request_headers.setlist('Content-Type',
                                'application/x-www-form-urlencoded')
        with self.assertRaises(MultiPartParserError) as mPPE_Exception:
            http_request.parse_request_body()
        self.assertEquals(
            'Invalid Content-Type: application/x-www-form-urlencoded',
            mPPE_Exception.exception.args[0])

        #close the file
        http_request_stream.close()

        #get a POST request and pass it a multipart body
        http_request_stream = open(self.post_request_with_query_file, 'r')
        http_request = HttpRequest(http_request_stream)
        http_request.parse_request_header()

        #change request body
        multipart_body = "---------------------------9051914041544843365972754266\r\n"
        multipart_body += "Content-Disposition: form-data; name=\"file1\"; filename=\"a.txt\"\r\n"
        multipart_body += "Content-Type: text/plain\r\n\r\nContent of a.txt:\r\n"
        multipart_body += " abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900~!@#$%^&*()_+\r\n"
        multipart_body += "---------------------------9051914041544843365972754266--\r\n\r\n"
        multipart_body = BytesIO(multipart_body)
        http_request.body_stream = multipart_body
        http_request.parse_request_body()
        self.assertIn(' name', http_request.POST)
        self.assertEquals('"file1"', http_request.POST[' name'])

        #close the file
        http_request_stream.close()
Ejemplo n.º 19
0
    def test_request_data_too_big(self):
        """
        Tests both request.py's and multipartparser.py's RequestDataTooBig.
        """

        #text/plain test
        multipart_request_stream = open(self.put_request_multipart_file, 'r')

        #get a request handle
        multipart_request = HttpRequest(
            multipart_request_stream,
            #setting DATA_UPLOAD MAX to be 64 bytes
            Settings({Settings.Key.DATA_UPLOAD_MAX_MEMORY: 64}))
        multipart_request.parse_request_header()
        multipart_request.content_type = "text/plain"
        with self.assertRaises(RequestDataTooBig) as rqdTooBig_Exception:
            multipart_request.parse_request_body()
        self.assertEquals(
            "Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.",
            rqdTooBig_Exception.exception.args[0])
        multipart_request_stream.close()

        #x-www-form-urlencoded test
        multipart_request_stream = open(self.put_request_multipart_file, 'r')

        #get a request handle
        multipart_request = HttpRequest(
            multipart_request_stream,
            #setting DATA_UPLOAD MAX to be 64 bytes
            Settings({Settings.Key.DATA_UPLOAD_MAX_MEMORY: 64}))
        multipart_request.parse_request_header()
        multipart_request.content_type = "application/x-www-form-urlencoded"
        with self.assertRaises(RequestDataTooBig) as rqdTooBig_Exception:
            multipart_request.parse_request_body()
        self.assertEquals(
            "Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.",
            rqdTooBig_Exception.exception.args[0])
        multipart_request_stream.close()
Ejemplo n.º 20
0
 def test_httprequest_full_path_with_query_string_and_fragment(self):
     request = HttpRequest()
     request.path = '/foo#bar'
     request.path_info = '/prefix' + request.path
     request.META['QUERY_STRING'] = 'baz#quux'
     self.assertEqual(request.get_full_path(), '/foo%23bar?baz#quux')
Ejemplo n.º 21
0
    def test_invalid_request_header(self):
        #Incorrectly terminated request
        invalid_request_1 = "GET asasd\r\nHost: www.knowhere123.com\r\n"
        invalid_request_1 = BytesIO(invalid_request_1)
        invalid_http_request = HttpRequest(invalid_request_1)
        with self.assertRaises(InvalidHttpRequest) as iHR_Exception:
            invalid_http_request.parse_request_header()
        self.assertEquals("Invalid HTTP request.",
                          iHR_Exception.exception.args[0])
        self.assertEquals(400, iHR_Exception.exception.args[1])

        #reuest without any headers
        invalid_request_2 = "GET dadsadsasd HTTP/1.1\r\n\r\n\r\n"
        invalid_request_2 = BytesIO(invalid_request_2)
        invalid_http_request = HttpRequest(invalid_request_2)
        with self.assertRaises(InvalidHttpRequest) as iHR_Exception:
            invalid_http_request.parse_request_header()
        self.assertEquals("Invalid request. No request headers.",
                          iHR_Exception.exception.args[0])
        self.assertEquals(400, iHR_Exception.exception.args[1])

        #invalid request line_0
        invalid_request = "GET asasd HTTP/1.1"
        with self.assertRaises(InvalidHttpRequest) as iHR_Exception:
            parse_request_headers(invalid_request)
        self.assertEqual(
            "Invalid request. Request line terminated incorrectly.",
            iHR_Exception.exception.args[0])
        self.assertEquals(400, iHR_Exception.exception.args[1])

        #incorrect request line_1
        invalid_request_3 = "GET asasd\r\nHost: www.knowhere123.com\r\n\r\n"
        invalid_request_3 = BytesIO(invalid_request_3)
        invalid_http_request = HttpRequest(invalid_request_3)
        with self.assertRaises(InvalidHttpRequest) as iHR_Exception:
            invalid_http_request.parse_request_header()
        self.assertEquals("Invalid request line.",
                          iHR_Exception.exception.args[0])
        self.assertEquals(400, iHR_Exception.exception.args[1])

        #incorrect request line_2
        invalid_request_4 = "GET asasd asdas HTTP/1.1\r\nHost: www.knowhere123.com\r\n\r\n"
        invalid_request_4 = BytesIO(invalid_request_4)
        invalid_http_request = HttpRequest(invalid_request_4)
        with self.assertRaises(InvalidHttpRequest) as iHR_Exception:
            invalid_http_request.parse_request_header()
        self.assertEquals("Invalid request line.",
                          iHR_Exception.exception.args[0])
        self.assertEquals(400, iHR_Exception.exception.args[1])

        #incorrect request header
        invalid_request_4 = "GET asasd HTTP/1.1\r\nHost www.knowhere123.com\r\n\r\n"
        invalid_request_4 = BytesIO(invalid_request_4)
        invalid_http_request = HttpRequest(invalid_request_4)
        with self.assertRaises(InvalidHttpRequest) as iHR_Exception:
            invalid_http_request.parse_request_header()
        self.assertIn("Invalid request header",
                      iHR_Exception.exception.args[0])
        self.assertEquals(400, iHR_Exception.exception.args[1])