Пример #1
0
def test_update_fkey():
    """
    Tests that the correct chat fkey is retrived, using a mock response
    with a copy of a real response from /chats/join/favorite
    """
    with only_httmock(favorite_with_test_fkey):
        browser = Browser()
        browser.host = 'stackexchange.com'

        assert browser.chat_fkey == TEST_FKEY
Пример #2
0
def test_update_fkey():
    """
    Tests that the correct chat fkey is retrived, using a mock response
    with a copy of a real response from /chats/join/favorite
    """
    with only_httmock(favorite_with_test_fkey):
        browser = Browser()
        browser.host = 'stackexchange.com'

        assert browser.chat_fkey == TEST_FKEY
def test_api_errors_raised():
    """
    Confirms that error responses from the API result in raised APIErrors.
    """

    with only_httmock(
        sites_returning_stackoverflow,
        throttle_violation_for_questions
    ):
        stack_exchange = stackexchange.StackExchange()

        with pytest.raises(ThrottleViolation):
            stack_exchange._request('questions', site='stackoverflow')
Пример #4
0
def test_stackexchange_init_sites():
    """
    Tests that the StackExchange class will correctly initialize a list
    of Site objects based on a request to /sites, when instantiated.
    """
    with only_httmock(sites_returning_stackoverflow):
        stack_exchange = stackexchange.StackExchange()

        assert len(stack_exchange.sites) == 1

        with pytest.raises(ValueError):
            stack_exchange.get_site("this is a no\x00t a site")

        stack_overflow = stack_exchange.get_site("Stack Overflow")

        assert stack_overflow == stack_exchange.get_site('stackoverflow')

        assert stack_overflow.se == stack_exchange
        assert stack_overflow.name == "Stack Overflow"
        assert stack_overflow.site_type == 'main_site'
        assert stack_overflow.site_state == 'normal'
Пример #5
0
def test_user_agent():
    """
    Tests that HTTP requests made from a Browser use the intended
    User-Agent.

    WebSocket connections are not tested.
    """
    good_requests = []

    @httmock.all_requests
    def verify_user_agent(url, request):
        assert request.headers['user-agent'] == Browser.user_agent
        good_requests.append(request)
        return '<!doctype html><html><head><title>Hello<body>World'

    with only_httmock(verify_user_agent):
        browser = Browser()

        browser.get_soup('http://example.com/', with_chat_root=False)
        browser.get_soup('http://example.com/2', with_chat_root=False)

        assert len(good_requests) == 2, "Unexpected number of requests"
Пример #6
0
def test_user_agent():
    """
    Tests that HTTP requests made from a Browser use the intended
    User-Agent.

    WebSocket connections are not tested.
    """
    good_requests = []

    @httmock.all_requests
    def verify_user_agent(url, request):
        assert request.headers['user-agent'] == Browser.user_agent
        good_requests.append(request)
        return '<!doctype html><html><head><title>Hello<body>World'

    with only_httmock(verify_user_agent):
        browser = Browser()

        browser.get_soup('http://example.com/', with_chat_root=False)
        browser.get_soup('http://example.com/2', with_chat_root=False)

        assert len(good_requests) == 2, "Unexpected number of requests"