Exemplo n.º 1
0
def test_handle_http_error_response():
    """
    Test that an 'Error' response from Scrapyd handles as desired.
    """
    non_authed_client = Client()
    responses.add(responses.GET, URL, body=MALFORMED_JSON, status=500)
    with pytest.raises(ScrapydResponseError) as excinfo:
        non_authed_client.get(URL)
    assert '500 error' in str(excinfo.value)
Exemplo n.º 2
0
def test_handle_http_error_response():
    """
    Test that an 'Error' response from Scrapyd handles as desired.
    """
    non_authed_client = Client()
    responses.add(responses.GET, URL, body=MALFORMED_JSON, status=500)
    with pytest.raises(ScrapydResponseError) as excinfo:
        non_authed_client.get(URL)
    assert '500 error' in str(excinfo.value)
Exemplo n.º 3
0
def test_scrapyd_error_response():
    """
    Test that a response from Scrapyd that does not parse as
    valid JSON raises the correct exception.
    """
    non_authed_client = Client()
    responses.add(responses.GET, URL, body=ERROR_JSON, status=200)
    with pytest.raises(ScrapydResponseError) as excinfo:
        non_authed_client.get(URL)
    assert 'some-error' in str(excinfo.value)
Exemplo n.º 4
0
def test_non_or_invalid_json_response_errors():
    """
    Test that a response from Scrapyd that does not parse as
    valid JSON raises the correct exception.
    """
    non_authed_client = Client()
    responses.add(responses.GET, URL, body=MALFORMED_JSON, status=200)
    with pytest.raises(ScrapydResponseError) as excinfo:
        non_authed_client.get(URL)
    assert 'invalid JSON' in str(excinfo.value)
Exemplo n.º 5
0
def test_scrapyd_error_response():
    """
    Test that a response from Scrapyd that does not parse as
    valid JSON raises the correct exception.
    """
    non_authed_client = Client()
    responses.add(responses.GET, URL, body=ERROR_JSON, status=200)
    with pytest.raises(ScrapydResponseError) as excinfo:
        non_authed_client.get(URL)
    assert 'some-error' in str(excinfo.value)
Exemplo n.º 6
0
def test_non_or_invalid_json_response_errors():
    """
    Test that a response from Scrapyd that does not parse as
    valid JSON raises the correct exception.
    """
    non_authed_client = Client()
    responses.add(responses.GET, URL, body=MALFORMED_JSON, status=200)
    with pytest.raises(ScrapydResponseError) as excinfo:
        non_authed_client.get(URL)
    assert 'invalid JSON' in str(excinfo.value)
Exemplo n.º 7
0
def test_post_handle_ok_response():
    """
    Test that a POST request uses the requests lib properly.
    """
    non_authed_client = Client()
    responses.add(responses.POST, URL, body=OK_JSON, status=200)
    test_data = {'test': 'json'}
    non_authed_client.post(URL, data=test_data)
    assert len(responses.calls) == 1
    call = responses.calls[0]
    assert call.response.json() == json.loads(OK_JSON)
Exemplo n.º 8
0
def test_post_handle_ok_response():
    """
    Test that a POST request uses the requests lib properly.
    """
    non_authed_client = Client()
    responses.add(responses.POST, URL, body=OK_JSON, status=200)
    test_data = {'test': 'json'}
    non_authed_client.post(URL, data=test_data)
    assert len(responses.calls) == 1
    call = responses.calls[0]
    assert call.response.json() == json.loads(OK_JSON)
Exemplo n.º 9
0
def test_with_auth():
    """
    Test attaching basic auth creds results in correct headers.
    """
    authed_client = Client()
    authed_client.auth = AUTH
    # Test with just a URL call.
    responses.add(responses.GET, URL, body=OK_JSON, status=200)
    authed_client.get(URL)
    assert len(responses.calls) == 1
    call = responses.calls[0]
    assert 'Authorization' in call.request.headers
    assert 'Basic' in call.request.headers['Authorization']
Exemplo n.º 10
0
def test_with_auth():
    """
    Test attaching basic auth creds results in correct headers.
    """
    authed_client = Client()
    authed_client.auth = AUTH
    # Test with just a URL call.
    responses.add(responses.GET, URL, body=OK_JSON, status=200)
    authed_client.get(URL)
    assert len(responses.calls) == 1
    call = responses.calls[0]
    assert 'Authorization' in call.request.headers
    assert 'Basic' in call.request.headers['Authorization']
Exemplo n.º 11
0
def test_get_handle_ok_response():
    """
    Test that a GET request uses the requests lib properly.
    """
    non_authed_client = Client()
    responses.add(responses.GET, URL, body=OK_JSON, status=200)
    non_authed_client.get(URL)
    assert len(responses.calls) == 1
    call = responses.calls[0]
    assert call.request.url == URL
    assert call.response.json() == json.loads(OK_JSON)
    # Test with some query string params.
    test_params = {'test': 'params'}
    url_with_query = URL + '?test=params'
    responses.add(responses.GET, url_with_query, body=OK_JSON, status=200)
    non_authed_client.get(URL, params=test_params)
    assert len(responses.calls) == 2
    call = responses.calls[1]
    assert call.response.json() == json.loads(OK_JSON)
Exemplo n.º 12
0
def test_get_handle_ok_response():
    """
    Test that a GET request uses the requests lib properly.
    """
    non_authed_client = Client()
    responses.add(responses.GET, URL, body=OK_JSON, status=200)
    non_authed_client.get(URL)
    assert len(responses.calls) == 1
    call = responses.calls[0]
    assert call.request.url == URL
    assert call.response.json() == json.loads(OK_JSON)
    # Test with some query string params.
    test_params = {'test': 'params'}
    url_with_query = URL + '?test=params'
    responses.add(responses.GET, url_with_query, body=OK_JSON, status=200)
    non_authed_client.get(URL, params=test_params)
    assert len(responses.calls) == 2
    call = responses.calls[1]
    assert call.response.json() == json.loads(OK_JSON)