Skip to content
Merged
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
14 changes: 10 additions & 4 deletions impit-python/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,16 @@ pub(crate) fn to_body(
None => ImpitBody::Empty,
Some(RequestBody::Bytes(bytes)) => bytes.into(),
Some(RequestBody::Form(form)) => {
headers.get_or_insert_default().insert(
"Content-Type".to_string(),
"application/x-www-form-urlencoded".to_string(),
);
let headers = headers.get_or_insert_default();
if !headers
.keys()
.any(|name| name.eq_ignore_ascii_case("content-type"))
{
headers.insert(
"Content-Type".to_string(),
"application/x-www-form-urlencoded".to_string(),
);
}
form_to_bytes(form).into()
}
Some(RequestBody::Text(text)) => text.into_bytes().into(),
Expand Down
15 changes: 15 additions & 0 deletions impit-python/test/async_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,21 @@ async def test_form_non_ascii(self, browser: Browser) -> None:
assert response.status_code == 200
assert json.loads(response.text)['form']['Impit-Test'] == '👾🕵🏻‍♂️🧑‍💻'

@pytest.mark.asyncio
async def test_form_keeps_custom_content_type(self, browser: Browser) -> None:
impit = AsyncClient(browser=browser)

with echoing_server() as port:
response = await impit.post(
f'http://localhost:{port}/',
data={'Impit-Test': 'foo'},
headers={'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'},
timeout=5,
)

assert 'content-type: application/x-www-form-urlencoded; charset=utf-8\r\n' in response.text.lower()
assert response.text.endswith('Impit-Test=foo')

@pytest.mark.asyncio
async def test_passing_binary_body(self, browser: Browser) -> None:
impit = AsyncClient(browser=browser)
Expand Down
14 changes: 14 additions & 0 deletions impit-python/test/basic_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,20 @@ def test_form_non_ascii(self, browser: Browser) -> None:
assert response.status_code == 200
assert response.json()['form']['Impit-Test'] == '👾🕵🏻‍♂️🧑‍💻'

def test_form_keeps_custom_content_type(self, browser: Browser) -> None:
impit = Client(browser=browser)

with echoing_server() as port:
response = impit.post(
f'http://localhost:{port}/',
data={'Impit-Test': 'foo'},
headers={'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'},
timeout=5,
)

assert 'content-type: application/x-www-form-urlencoded; charset=utf-8\r\n' in response.text.lower()
assert response.text.endswith('Impit-Test=foo')

def test_passing_binary_body(self, browser: Browser) -> None:
impit = Client(browser=browser)

Expand Down
Loading