Example #1
0
def test_parse_form_data_get_without_content():
    """GET requests without data, content type and length returns no data"""
    env = create_environ('/foo', 'http://example.org/', method='GET')
    del env['CONTENT_TYPE']
    del env['CONTENT_LENGTH']

    stream, form, files = parse_form_data(env)
    assert stream.read() == ""
    assert len(form) == 0
    assert len(files) == 0
Example #2
0
def test_parse_form_data_get_without_content():
    """GET requests without data, content type and length returns no data"""
    env = create_environ('/foo', 'http://example.org/', method='GET')
    del env['CONTENT_TYPE']
    del env['CONTENT_LENGTH']

    stream, form, files = parse_form_data(env)
    assert stream.read() == ""
    assert len(form) == 0
    assert len(files) == 0
Example #3
0
def test_parse_form_data_put_without_content():
    """A PUT without a Content-Type header returns empty data

    Both rfc1945 and rfc2616 (1.0 and 1.1) say "Any HTTP/[1.0/1.1] message
    containing an entity-body SHOULD include a Content-Type header field
    defining the media type of that body."  In the case where either
    headers are omitted, parse_form_data should still work.
    """
    env = create_environ('/foo', 'http://example.org/', method='PUT')
    del env['CONTENT_TYPE']
    del env['CONTENT_LENGTH']

    stream, form, files = parse_form_data(env)
    assert stream.read() == ""
    assert len(form) == 0
    assert len(files) == 0
Example #4
0
def test_environ_builder_stream_switch():
    """EnvironBuilder stream switch"""
    from cStringIO import OutputType
    from werkzeug.test import stream_encode_multipart
    from werkzeug import url_decode, MultiDict, parse_form_data

    d = MultiDict(dict(foo=u'bar', blub=u'blah', hu=u'hum'))
    for use_tempfile in False, True:
        stream, length, boundary = stream_encode_multipart(
            d, use_tempfile, threshold=150)
        assert isinstance(stream, OutputType) != use_tempfile

        form = parse_form_data({'wsgi.input': stream, 'CONTENT_LENGTH': str(length),
                                'CONTENT_TYPE': 'multipart/form-data; boundary="%s"' %
                                boundary})[1]
        assert form == d
Example #5
0
def test_parse_form_data_put_without_content():
    """A PUT without a Content-Type header returns empty data

    Both rfc1945 and rfc2616 (1.0 and 1.1) say "Any HTTP/[1.0/1.1] message
    containing an entity-body SHOULD include a Content-Type header field
    defining the media type of that body."  In the case where either
    headers are omitted, parse_form_data should still work.
    """
    env = create_environ('/foo', 'http://example.org/', method='PUT')
    del env['CONTENT_TYPE']
    del env['CONTENT_LENGTH']

    stream, form, files = parse_form_data(env)
    assert stream.read() == ""
    assert len(form) == 0
    assert len(files) == 0
Example #6
0
def test_broken_multipart():
    """Broken multipart does not break the applicaiton"""
    data = (
        '--foo\r\n'
        'Content-Disposition: form-data; name="test"; filename="test.txt"\r\n'
        'Content-Transfer-Encoding: base64\r\n'
        'Content-Type: text/plain\r\n\r\n'
        'broken base 64'
        '--foo--'
    )
    _, form, files = parse_form_data(create_environ(data=data, method='POST',
                                     content_type='multipart/form-data; boundary=foo'))
    assert not files
    assert not form

    assert_raises(ValueError, parse_form_data, create_environ(data=data, method='POST',
                  content_type='multipart/form-data; boundary=foo'),
                  silent=False)
def test_environ_builder_stream_switch():
    """EnvironBuilder stream switch"""
    from cStringIO import OutputType
    from werkzeug.test import stream_encode_multipart
    from werkzeug import url_decode, MultiDict, parse_form_data

    d = MultiDict(dict(foo=u'bar', blub=u'blah', hu=u'hum'))
    for use_tempfile in False, True:
        stream, length, boundary = stream_encode_multipart(d,
                                                           use_tempfile,
                                                           threshold=150)
        assert isinstance(stream, OutputType) != use_tempfile

        form = parse_form_data({
            'wsgi.input':
            stream,
            'CONTENT_LENGTH':
            str(length),
            'CONTENT_TYPE':
            'multipart/form-data; boundary="%s"' % boundary
        })[1]
        assert form == d
Example #8
0
def test_broken_multipart():
    """Broken multipart does not break the applicaiton"""
    data = (
        '--foo\r\n'
        'Content-Disposition: form-data; name="test"; filename="test.txt"\r\n'
        'Content-Transfer-Encoding: base64\r\n'
        'Content-Type: text/plain\r\n\r\n'
        'broken base 64'
        '--foo--')
    _, form, files = parse_form_data(
        create_environ(data=data,
                       method='POST',
                       content_type='multipart/form-data; boundary=foo'))
    assert not files
    assert not form

    assert_raises(ValueError,
                  parse_form_data,
                  create_environ(
                      data=data,
                      method='POST',
                      content_type='multipart/form-data; boundary=foo'),
                  silent=False)