Skip to content

Commit

Permalink
Strip empty fields in MultipartEncoder
Browse files Browse the repository at this point in the history
This is coherent with what's done in requests
  • Loading branch information
Ruben DI BATTISTA committed Jan 25, 2022
1 parent 1112280 commit f8d7dfe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions requests_toolbelt/multipart/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ def _iter_fields(self):
file_name, file_pointer, file_type = v
else:
file_name, file_pointer, file_type, file_headers = v
elif v is None:
continue
else:
file_pointer = v

Expand Down
25 changes: 20 additions & 5 deletions tests/test_multipart_encoder.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# -*- coding: utf-8 -*-
import unittest
import io
import unittest

import requests

from requests_toolbelt.multipart.encoder import (
CustomBytesIO, MultipartEncoder, FileFromURLWrapper, FileNotSupportedError)
from requests_toolbelt._compat import filepost
from . import get_betamax
from requests_toolbelt.multipart.encoder import (
CustomBytesIO,
FileFromURLWrapper,
FileNotSupportedError,
MultipartEncoder,
)

from . import get_betamax

preserve_bytes = {'preserve_exact_body_bytes': True}

Expand Down Expand Up @@ -272,6 +275,18 @@ def test_regression_2(self):

assert read_so_far == total_size

def test_empty_files_key(self):
"""Ensure empty `fields` keys are stripped from the request"""

fields = {
"test": "this is a test",
"empty": None
}

m = MultipartEncoder(fields=fields)

assert len(m.parts) == 1

def test_handles_empty_unicode_values(self):
"""Verify that the Encoder can handle empty unicode strings.
Expand Down

0 comments on commit f8d7dfe

Please sign in to comment.