Exemple #1
0
def _decode_result(response, decoders, force_codec=False):
    """
    Given an HTTP response, return the decoded Core API document.
    """
    if response.content:
        # Content returned in response. We should decode it.
        if force_codec:
            codec = decoders[0]
        else:
            content_type = response.headers.get('content-type')
            codec = utils.negotiate_decoder(decoders, content_type)

        options = {'base_url': response.url}
        if 'content-type' in response.headers:
            options['content_type'] = response.headers['content-type']
        if 'content-disposition' in response.headers:
            options['content_disposition'] = response.headers[
                'content-disposition']

        result = codec.load(response.content, **options)
    else:
        # No content returned in response.
        result = None

    # Coerce 4xx and 5xx codes into errors.
    is_error = response.status_code >= 400 and response.status_code <= 599
    if is_error and not isinstance(result, Error):
        default_title = '%d %s' % (response.status_code, response.reason)
        result = _coerce_to_error(result, default_title=default_title)

    return result
Exemple #2
0
def test_get_unsupported_decoder():
    with pytest.raises(NoCodecAvailable):
        negotiate_decoder([CoreJSONCodec()], 'application/csv')
Exemple #3
0
def test_get_supported_decoder():
    codec = negotiate_decoder([CoreJSONCodec()],
                              'application/vnd.coreapidr+json')
    assert isinstance(codec, CoreJSONCodec)
Exemple #4
0
def test_get_supported_decoder_with_parameters():
    codec = negotiate_decoder([CoreJSONCodec()],
                              'application/vnd.coreapidr+json; verison=1.0')
    assert isinstance(codec, CoreJSONCodec)
Exemple #5
0
def test_get_default_decoder():
    codec = negotiate_decoder([CoreJSONCodec()])
    assert isinstance(codec, CoreJSONCodec)