Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'on_header_begin' callback #24

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,13 @@ def data_callback(name, remaining=False):
# Set a mark of our header field.
set_mark('header_field')

# Notify that we're starting a header if the next character is
# not a CR; a CR at the beginning of the header will cause us
# to stop parsing headers in the STATE_HEADER_FIELD state,
# below.
if c != CR:
self.callback('header_begin')

# Move to parsing header fields.
state = STATE_HEADER_FIELD
i -= 1
Expand Down
32 changes: 32 additions & 0 deletions multipart/tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,38 @@ def test_invalid_max_size_multipart(self):
with self.assertRaises(ValueError):
q = MultipartParser(b'bound', max_size='foo')

def test_header_begin_callback(self):
"""
This test verifies we call the `on_header_begin` callback.

See GitHub issue #23
"""
# Load test data.
test_file = 'single_field_single_file.http'
with open(os.path.join(http_tests_dir, test_file), 'rb') as f:
test_data = f.read()

calls = []
def on_header_begin(*args, **kwargs):
calls.append((args, kwargs))

callbacks = {
'on_header_begin': on_header_begin,
}
parser = MultipartParser('boundary', callbacks,
max_size=1000)

# Create multipart parser and feed it
i = parser.write(test_data)
parser.finalize()

# Assert we processed everything.
self.assertEqual(i, len(test_data))

# Assert that we called our 'header_begin' callbakc three times; once
# for each header in the multipart message.
self.assertEqual(len(calls), 3)


class TestHelperFunctions(unittest.TestCase):
def test_create_form_parser(self):
Expand Down