<commit_before>import asyncio
import datetime
import gc
import os
import sys
import tempfile
from unittest import mock

import pytest
from yarl import URL

from aiohttp import helpers
from aiohttp.abc import AbstractAccessLogger


# -------------------- coro guard --------------------------------


@asyncio.coroutine
def test_warn():
    with pytest.warns(DeprecationWarning) as ctx:
        helpers.deprecated_noop('Text')

    w = ctx.list[0]

    assert str(w.message) == 'Text'
    # Assert the warning points at us and not at _CoroGuard.
    assert w.filename == __file__


@asyncio.coroutine
def test_no_warn_on_await():
    with pytest.warns(None) as ctx:
        yield from helpers.deprecated_noop('Text')
    assert not ctx.list


def test_coro_guard_close():
    guard = helpers.deprecated_noop('Text')
    guard.close()
    assert not guard.gi_running


# ------------------- parse_mimetype ----------------------------------

def test_parse_mimetype_1():
    assert helpers.parse_mimetype('') == ('', '', '', {})


def test_parse_mimetype_2():
    assert helpers.parse_mimetype('*') == ('*', '*', '', {})


def test_parse_mimetype_3():
    assert (helpers.parse_mimetype('application/json') ==
            ('application', 'json', '', {}))


def test_parse_mimetype_4():
    assert (
        helpers.parse_mimetype('application/json;  charset=utf-8') ==
        ('application', 'json', '', {'charset': 'utf-8'}))


def test_parse_mimetype_5():
    assert (
        helpers.parse_mimetype('''application/json; charset=utf-8;''') ==
        ('application', 'json', '', {'charset': 'utf-8'}))


def test_parse_mimetype_6():
    assert(
        helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') ==
        ('application', 'json', '', {'charset': 'UTF-8'}))


def test_parse_mimetype_7():
    assert (
        helpers.parse_mimetype('application/rss+xml') ==
        ('application', 'rss', 'xml', {}))


def test_parse_mimetype_8():
    assert (
        helpers.parse_mimetype('text/plain;base64') ==
        ('text', 'plain', '', {'base64': ''}))


# ------------------- guess_filename ----------------------------------

def test_guess_filename_with_tempfile():
    with tempfile.TemporaryFile() as fp:
        assert (helpers.guess_filename(fp, 'no-throw') is not None)


# ------------------- BasicAuth -----------------------------------

def test_basic_auth1():
    # missing password here
    with pytest.raises(ValueError):
        helpers.BasicAuth(None)


def test_basic_auth2():
    with pytest.raises(ValueError):
        helpers.BasicAuth('nkim', None)


def test_basic_with_auth_colon_in_login():
    with pytest.raises(ValueError):
        helpers.BasicAuth('nkim:1', 'pwd')


def test_basic_auth3():
    auth = helpers.BasicAuth('nkim')
    assert auth.login == 'nkim'<commit_msg>simplify parse mimetype tests (#2344)<commit_after>import asyncio
import datetime
import gc
import os
import sys
import tempfile
from unittest import mock

import pytest
from yarl import URL

from aiohttp import helpers
from aiohttp.abc import AbstractAccessLogger


# -------------------- coro guard --------------------------------


@asyncio.coroutine
def test_warn():
    with pytest.warns(DeprecationWarning) as ctx:
        helpers.deprecated_noop('Text')

    w = ctx.list[0]

    assert str(w.message) == 'Text'
    # Assert the warning points at us and not at _CoroGuard.
    assert w.filename == __file__


@asyncio.coroutine
def test_no_warn_on_await():
    with pytest.warns(None) as ctx:
        yield from helpers.deprecated_noop('Text')
    assert not ctx.list


def test_coro_guard_close():
    guard = helpers.deprecated_noop('Text')
    guard.close()
    assert not guard.gi_running


# ------------------- parse_mimetype ----------------------------------

@pytest.mark.parametrize('mimetype, expected', [
    ('', ('', '', '', {})),
    ('*', ('*', '*', '', {})),
    ('application/json', ('application', 'json', '', {})),
    (
        'application/json;  charset=utf-8',
        ('application', 'json', '', {'charset': 'utf-8'})
    ),
    (
        '''application/json; charset=utf-8;''',
        ('application', 'json', '', {'charset': 'utf-8'})
    ),
    (
        'ApPlIcAtIoN/JSON;ChaRseT="UTF-8"',
        ('application', 'json', '', {'charset': 'UTF-8'})
    ),
    ('application/rss+xml', ('application', 'rss', 'xml', {})),
    ('text/plain;base64', ('text', 'plain', '', {'base64': ''}))
])
def test_parse_mimetype(mimetype, expected):
    assert helpers.parse_mimetype(mimetype) == expected


# ------------------- guess_filename ----------------------------------

def test_guess_filename_with_tempfile():
    with tempfile.TemporaryFile() as fp:
        assert (helpers.guess_filename(fp, 'no-throw') is not None)


# ------------------- BasicAuth -----------------------------------

def test_basic_auth1():
    # missing password here
    with pytest.raises(ValueError):
        helpers.BasicAuth(None)


def test_basic_auth2():
    with pytest.raises(ValueError):
        helpers.BasicAuth('nkim', None)


def test_basic_with_auth_colon_in_login():
    with pytest.raises(ValueError):
        helpers.BasicAuth('nkim:1', '<PASSWORD>')


def test_basic_auth3():
    auth = helpers.BasicAuth('nkim')
    assert auth.login == 'nkim'
{'fraction_non_alphanumeric': 0.22117023291043364, 'fraction_numerical': 0.007953039197121757, 'mean_word_length': 2.6103896103896105, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3910839', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1672, 'n_words': 335}