Skip to content

Commit

Permalink
Move run_server out of test_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Aug 13, 2016
1 parent 4ce9207 commit 10e3a36
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
15 changes: 1 addition & 14 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
"""Utilities shared by tests."""

import asyncio
import cgi
import contextlib
import email.parser
import functools
import gc
import http.server
import io
import json
import logging
import os
import re
import socket
import ssl
import sys
import threading
import traceback
import unittest
import urllib.parse
from unittest import mock

from multidict import CIMultiDict

import aiohttp

from . import ClientSession, hdrs, helpers, server
from . import ClientSession, hdrs
from .helpers import _sentinel
from .protocol import HttpVersion, RawRequestMessage
from .signals import Signal
Expand Down
70 changes: 41 additions & 29 deletions tests/test_client_functional_oldstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@

import asyncio
import binascii
import cgi
import contextlib
import email.parser
import gc
import http.cookies
import http.server
import io
import json
import logging
import os
import os.path
import re
import ssl
import sys
import threading
import traceback
import unittest
import urllib.parse
from unittest import mock

from multidict import MultiDict

import aiohttp
from aiohttp import client, helpers, test_utils
from aiohttp import client, helpers, server, test_utils
from aiohttp.multipart import MultipartWriter
from aiohttp.test_utils import unused_port
from aiohttp.test_utils import run_briefly, unused_port


@contextlib.contextmanager
Expand Down Expand Up @@ -284,7 +296,7 @@ def tearDown(self):
gc.collect()

def test_POST_DATA_with_charset(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

form = aiohttp.FormData()
Expand All @@ -304,7 +316,7 @@ def test_POST_DATA_with_charset(self):
self.assertEqual(r.status, 200)

def test_POST_DATA_with_content_transfer_encoding(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

form = aiohttp.FormData()
Expand All @@ -325,7 +337,7 @@ def test_POST_DATA_with_content_transfer_encoding(self):
self.assertEqual(r.status, 200)

def test_POST_MULTIPART(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

with MultipartWriter('form-data') as writer:
Expand Down Expand Up @@ -353,7 +365,7 @@ def test_POST_MULTIPART(self):
r.close()

def test_POST_STREAM_DATA(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

here = os.path.dirname(__file__)
Expand Down Expand Up @@ -385,7 +397,7 @@ def stream():
content['headers']['Content-Type'])

def test_POST_StreamReader(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

here = os.path.dirname(__file__)
Expand All @@ -410,7 +422,7 @@ def test_POST_StreamReader(self):
content['headers']['Content-Length'])

def test_POST_DataQueue(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

here = os.path.dirname(__file__)
Expand All @@ -436,7 +448,7 @@ def test_POST_DataQueue(self):
content['headers']['Content-Length'])

def test_POST_ChunksQueue(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

here = os.path.dirname(__file__)
Expand Down Expand Up @@ -464,7 +476,7 @@ def test_POST_ChunksQueue(self):
content['headers']['Content-Length'])

def test_cookies(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
c = http.cookies.Morsel()
c.set('test3', '456', '456')

Expand All @@ -479,7 +491,7 @@ def test_cookies(self):
r.close()

def test_morsel_with_attributes(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
c = http.cookies.Morsel()
c.set('test3', '456', '456')
c['httponly'] = True
Expand All @@ -504,7 +516,7 @@ def test_morsel_with_attributes(self):

@mock.patch('aiohttp.client_reqrep.client_logger')
def test_set_cookies(self, m_log):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
resp = self.loop.run_until_complete(
client.request('get', httpd.url('cookies'), loop=self.loop))
self.assertEqual(resp.status, 200)
Expand All @@ -518,7 +530,7 @@ def test_set_cookies(self, m_log):
mock.ANY)

def test_broken_connection(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
r = self.loop.run_until_complete(
client.request('get', httpd.url('broken'), loop=self.loop))
self.assertEqual(r.status, 200)
Expand All @@ -533,7 +545,7 @@ def test_request_conn_error(self):
client.request('get', 'http://0.0.0.0:1', loop=self.loop))

def test_request_conn_closed(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
httpd['close'] = True
with self.assertRaises(aiohttp.ClientHttpProcessingError):
self.loop.run_until_complete(
Expand All @@ -543,7 +555,7 @@ def test_request_conn_closed(self):
def test_session_close(self):
conn = aiohttp.TCPConnector(loop=self.loop)

with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
r = self.loop.run_until_complete(
client.request(
'get', httpd.url('keepalive') + '?close=1',
Expand All @@ -564,7 +576,7 @@ def test_session_close(self):
conn.close()

def test_multidict_headers(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

data = b'sample data'
Expand Down Expand Up @@ -594,7 +606,7 @@ def go(url):
yield from r.read()
self.assertEqual(0, len(connector._conns))

with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('keepalive')
self.loop.run_until_complete(go(url))

Expand All @@ -611,7 +623,7 @@ def go(url):
self.assertEqual(1, len(connector._conns))
connector.close()

with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
url = httpd.url('keepalive')
self.loop.run_until_complete(go(url))

Expand Down Expand Up @@ -711,7 +723,7 @@ def go():

@mock.patch('aiohttp.client_reqrep.client_logger')
def test_session_cookies(self, m_log):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
session = client.ClientSession(loop=self.loop)

resp = self.loop.run_until_complete(
Expand All @@ -735,7 +747,7 @@ def test_session_cookies(self, m_log):
session.close()

def test_session_headers(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
session = client.ClientSession(
loop=self.loop, headers={
"X-Real-IP": "192.168.0.1"
Expand All @@ -753,7 +765,7 @@ def test_session_headers(self):
session.close()

def test_session_headers_merge(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
session = client.ClientSession(
loop=self.loop, headers=[
("X-Real-IP", "192.168.0.1"),
Expand All @@ -776,7 +788,7 @@ def test_session_headers_merge(self):
session.close()

def test_session_auth(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
session = client.ClientSession(
loop=self.loop, auth=helpers.BasicAuth("login", "pass"))

Expand All @@ -792,7 +804,7 @@ def test_session_auth(self):
session.close()

def test_session_auth_override(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
session = client.ClientSession(
loop=self.loop, auth=helpers.BasicAuth("login", "pass"))

Expand All @@ -810,7 +822,7 @@ def test_session_auth_override(self):
session.close()

def test_session_auth_header_conflict(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
with run_server(self.loop, router=Functional) as httpd:
session = client.ClientSession(
loop=self.loop, auth=helpers.BasicAuth("login", "pass"))

Expand All @@ -824,11 +836,11 @@ def test_session_auth_header_conflict(self):

class Functional(Router):

@test_utils.Router.define('/method/([A-Za-z]+)$')
@Router.define('/method/([A-Za-z]+)$')
def method(self, match):
self._response(self._start_response(200))

@test_utils.Router.define('/keepalive$')
@Router.define('/keepalive$')
def keepalive(self, match):
self._transport._requests = getattr(
self._transport, '_requests', 0) + 1
Expand All @@ -841,7 +853,7 @@ def keepalive(self, match):
resp, 'requests={}'.format(self._transport._requests),
headers={'CONNECTION': 'keep-alive'})

@test_utils.Router.define('/cookies$')
@Router.define('/cookies$')
def cookies(self, match):
cookies = http.cookies.SimpleCookie()
cookies['c1'] = 'cookie1'
Expand All @@ -857,7 +869,7 @@ def cookies(self, match):
'{925EC0B8-CB17-4BEB-8A35-1033813B0523}; HttpOnly; Path=/')
self._response(resp)

@test_utils.Router.define('/cookies_partial$')
@Router.define('/cookies_partial$')
def cookies_partial(self, match):
cookies = http.cookies.SimpleCookie()
cookies['c1'] = 'other_cookie1'
Expand All @@ -868,7 +880,7 @@ def cookies_partial(self, match):

self._response(resp)

@test_utils.Router.define('/broken$')
@Router.define('/broken$')
def broken(self, match):
resp = self._start_response(200)

Expand Down

0 comments on commit 10e3a36

Please sign in to comment.