Example #1
0
def test_format_response_unsupported_format():
    """Attempt to use an unsupported formatting flag"""
    response = Response()
    response.__setstate__({'_content': b'{"field":"value"}'})

    with pytest.raises(ValueError) as err:
        format_response('xml', response)

    assert "not a supported format" in str(err.value)
Example #2
0
def test_format_response_json():
    """Format a Requests Response object to JSON"""
    response = Response()
    response.__setstate__({
        '_content': b'{"global_counter":3251,'
                    b'"private_counter":1,'
                    b'"settings":{"title":"Yay!","header_link":"?",'
                    b'"timezone":"UTC",'
                    b'"enabled_plugins":["qrcode","token"],'
                    b'"default_private_links":false}}',
    })

    assert isinstance(response.json(), dict)

    # Ensure valid JSON is returned after formatting
    assert json.loads(format_response('json', response))
    assert json.loads(format_response('pprint', response))
Example #3
0
def test_format_response_text():
    """Format a Requests Response object to plain text"""
    response = Response()
    response.__setstate__({
        '_content': b'{"global_counter":3251,'
                    b'"private_counter":1,'
                    b'"settings":{"title":"Yay!","header_link":"?",'
                    b'"timezone":"UTC",'
                    b'"enabled_plugins":["qrcode","token"],'
                    b'"default_private_links":false}}',
    })

    assert isinstance(response.text, str)
    assert response.text == '{"global_counter":3251,' \
                            '"private_counter":1,' \
                            '"settings":{"title":"Yay!","header_link":"?",' \
                            '"timezone":"UTC",' \
                            '"enabled_plugins":["qrcode","token"],' \
                            '"default_private_links":false}}'