Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add a custom_headers param to make_request #8760

Merged
merged 5 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 6 additions & 4 deletions tests/rest/client/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,12 @@ def upload_media(
image_length = len(image_data)
path = "/_matrix/media/r0/upload?filename=%s" % (filename,)
request, channel = make_request(
self.hs.get_reactor(), "POST", path, content=image_data, access_token=tok
)
request.requestHeaders.addRawHeader(
b"Content-Length", str(image_length).encode("UTF-8")
self.hs.get_reactor(),
"POST",
path,
content=image_data,
access_token=tok,
custom_headers=[(b"Content-Length", str(image_length))],
)
request.render(resource)
self.hs.get_reactor().pump([100])
Expand Down
11 changes: 10 additions & 1 deletion tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from collections import deque
from io import SEEK_END, BytesIO
from typing import Callable
from typing import Callable, Iterable, Optional, Tuple, Union

import attr
from typing_extensions import Deque
Expand Down Expand Up @@ -139,6 +139,9 @@ def make_request(
shorthand=True,
federation_auth_origin=None,
content_is_form=False,
custom_headers: Optional[
Iterable[Tuple[Union[bytes, str], Union[bytes, str]]]
] = None,
):
"""
Make a web request using the given method and path, feed it the
Expand All @@ -157,6 +160,8 @@ def make_request(
content_is_form: Whether the content is URL encoded form data. Adds the
'Content-Type': 'application/x-www-form-urlencoded' header.

custom_headers: (name, value) pairs to add as request headers

Returns:
Tuple[synapse.http.site.SynapseRequest, channel]
"""
Expand Down Expand Up @@ -211,6 +216,10 @@ def make_request(
# Assume the body is JSON
req.requestHeaders.addRawHeader(b"Content-Type", b"application/json")

if custom_headers:
for k, v in custom_headers:
req.requestHeaders.addRawHeader(k, v)

req.requestReceived(method, path, b"1.1")

return req, channel
Expand Down
15 changes: 8 additions & 7 deletions tests/storage/test_client_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from synapse.rest.client.v1 import login

from tests import unittest
from tests.server import make_request
from tests.test_utils import make_awaitable
from tests.unittest import override_config

Expand Down Expand Up @@ -408,18 +409,18 @@ def _runtest(self, headers, expected_ip, make_request_args):
# Advance to a known time
self.reactor.advance(123456 - self.reactor.seconds())

request, channel = self.make_request(
headers1 = {b"User-Agent": b"Mozzila pizza"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mozzila

This sounds like a strange cross between Mozilla and Mozzarella.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, cheesy.

headers1.update(headers)

make_request(
self.reactor,
self.site,
"GET",
"/_matrix/client/r0/admin/users/" + self.user_id,
access_token=access_token,
custom_headers=headers1.items(),
**make_request_args,
)
request.requestHeaders.addRawHeader(b"User-Agent", b"Mozzila pizza")

# Add the optional headers
for h, v in headers.items():
request.requestHeaders.addRawHeader(h, v)
self.render(request)

# Advance so the save loop occurs
self.reactor.advance(100)
Expand Down