예제 #1
0
def test_set_remove():
	headers = web.HTTPHeaders()

	headers.set(test_key, test_value)

	assert headers.get(test_key) == test_value

	headers.remove(test_key)
예제 #2
0
def test_case():
	headers = web.HTTPHeaders()

	headers.set(case_key, case_value)

	assert headers.get(case_key_title) == case_value

	assert headers.retrieve(case_key_title) == case_header
예제 #3
0
def test_clear():
	headers = web.HTTPHeaders()

	headers.set(test_key, test_value)
	headers.set(poor_key, poor_value)

	headers.clear()

	assert len(headers) == 0
예제 #4
0
def test_continue():
    request_headers = web.HTTPHeaders()
    request_headers.set('Expect', '100-continue')

    headers, response, response_obj = test('PUT',
                                           headers=request_headers,
                                           return_response_obj=True)

    #Check response_obj
    assert response_obj.wfile.getvalue() == b'HTTP/1.1 100 Continue\r\n\r\n'
예제 #5
0
def test_len():
	headers = web.HTTPHeaders()

	headers.set(test_key, test_value)

	assert len(headers) == 1

	headers.set(poor_key, poor_value)

	assert len(headers) == 2
예제 #6
0
def test_error_headers():
    error_headers = web.HTTPHeaders()
    error_headers.set('Test', 'True')

    response, response_line, headers, body = test(
        web.DummyHandler, {'error': web.HTTPError(402, headers=error_headers)})

    assert response_line == 'HTTP/1.1 402 Payment Required'.encode(
        web.http_encoding)

    assert headers.get('Test') == 'True'
예제 #7
0
def test_body_too_large_continue():
    long_body = fake.FakeBytes()
    long_body.set_len(web.max_request_size + 1)

    request_headers = web.HTTPHeaders()
    request_headers.set('Expect', '100-continue')

    try:
        headers, response = test('PUT',
                                 body=long_body,
                                 headers=request_headers)
        assert False
    except web.HTTPError as error:
        assert error.code == 413
예제 #8
0
def test_check_continue():
    class NoContinueHandler(TestHandler):
        def check_continue(self):
            raise web.HTTPError(417)

    request_headers = web.HTTPHeaders()
    request_headers.set('Expect', '100-continue')

    try:
        headers, response = test('PUT',
                                 headers=request_headers,
                                 handler=NoContinueHandler)
        assert False
    except web.HTTPError as error:
        assert error.code == 417
예제 #9
0
def test_iter():
	headers = web.HTTPHeaders()

	headers.set(test_key, test_value)
	headers.set(poor_key, poor_value)
	headers.set(case_key, case_value)

	header_list = []

	for header in headers:
		header_list.append(header)

	assert test_header in header_list
	assert good_header in header_list
	assert case_header in header_list
예제 #10
0
파일: fake.py 프로젝트: trentlarge/pyrest
    def __init__(self, connection, client_address, server, request):
        self.connection = connection
        self.client_address = client_address
        self.server = server

        self.request = request

        self.wfile = io.BytesIO(b'')

        self.headers = web.HTTPHeaders()

        self.write_body = True

        self.handled = 0

        self.closed = False
예제 #11
0
def test_get_range():
	range = 2, 6
	length = range[1] - range[0] + 1

	request_headers = web.HTTPHeaders()
	request_headers.set('Range', 'bytes=' + str(range[0]) + '-' + str(range[1]))
	headers, response = test('GET', '/test', headers=request_headers)

	#Check headers
	assert int(headers.get('Content-Length')) == length
	assert headers.get('Accept-Ranges') == 'bytes'
	assert headers.get('Content-Type') == None
	assert headers.get('Content-Range') == 'bytes ' + str(range[0]) + '-' + str(range[1]) + '/' + str(len(test_string))

	#Check response
	assert response[0] == 206
	assert response[1].read(length) == test_string[range[0]:range[1]+1]
예제 #12
0
def test_get_open_range():
	lower = 2
	length = len(test_string) - lower

	request_headers = web.HTTPHeaders()
	request_headers.set('Range', 'bytes=' + str(lower) + '-')
	headers, response = test('GET', '/test', headers=request_headers)

	#Check headers
	assert int(headers.get('Content-Length')) == length
	assert headers.get('Accept-Ranges') == 'bytes'
	assert headers.get('Content-Type') == None
	assert headers.get('Content-Range') == 'bytes ' + str(lower) + '-' + str(len(test_string) - 1) + '/' + str(len(test_string))

	#Check response
	assert response[0] == 206
	assert response[1].read(length) == test_string[lower:]
예제 #13
0
파일: fake.py 프로젝트: trentlarge/pyrest
    def __init__(self,
                 connection,
                 client_address,
                 server,
                 timeout=None,
                 body=None,
                 headers=None,
                 method='GET',
                 resource='/',
                 groups=(),
                 handler=FakeHTTPHandler,
                 handler_args={},
                 response=FakeHTTPResponse,
                 keepalive_number=0):
        self.connection = connection
        self.client_address = client_address
        self.server = server

        self.timeout = timeout

        self.rfile = io.BytesIO(body)

        self.response = response(connection, client_address, server, self)

        self.keepalive = True

        self.method = method
        self.resource = resource
        self.request_line = method + ' ' + resource + ' ' + web.http_version

        if headers:
            self.headers = headers
        else:
            self.headers = web.HTTPHeaders()

        if body:
            self.headers.set('Content-Length', str(len(body)))

        self.handler = handler(self, self.response, groups, **handler_args)

        self.keepalive_number = keepalive_number

        self.initial_timeout = None
        self.handled = 0
예제 #14
0
def test(method, resource, body='', headers=web.HTTPHeaders(), handler=None, local='tmp', remote='', dir_index=False, modify=False, return_handler=False):
	if not isinstance(body, bytes):
		body = body.encode('utf-8')

	if not handler:
		route = file.new(local, remote, dir_index, modify)

		handler = list(route.values())[0]

		local = handler.local
		remote = handler.remote

	request = fake.FakeHTTPRequest(None, ('', 0), None, body=body, headers=headers, method=method, resource=resource, groups=(resource[len(remote):],), handler=handler)

	handler_obj = request.handler

	if return_handler:
		return request.response.headers, handler_obj.respond(), handler_obj
	else:
		return request.response.headers, handler_obj.respond()
예제 #15
0
def test(method,
         body='',
         headers=web.HTTPHeaders(),
         handler=TestHandler,
         handler_args={},
         return_response_obj=False):
    if not isinstance(body, bytes):
        body = body.encode('utf-8')

    request = fake.FakeHTTPRequest(None, ('', 1337),
                                   None,
                                   body=body,
                                   headers=headers,
                                   method=method,
                                   handler=handler,
                                   handler_args=handler_args)

    handler_obj = request.handler

    if return_response_obj:
        return request.response.headers, handler_obj.respond(
        ), handler_obj.response
    else:
        return request.response.headers, handler_obj.respond()
예제 #16
0
def test_set_key_nonstr():
	headers = web.HTTPHeaders()

	headers.set(nonstr_key, test_value)
예제 #17
0
def test_retrieve():
	headers = web.HTTPHeaders()

	headers.set(test_key, test_value)

	assert headers.retrieve(test_key) == test_header
예제 #18
0
def test_poor_header():
	headers = web.HTTPHeaders()

	headers.add(poor_header)

	assert headers.get(poor_key) == poor_value
예제 #19
0
def test_add_get():
	headers = web.HTTPHeaders()

	headers.add(test_header)

	assert headers.get(test_key) == test_value