2018-09-13 17:15:51 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from mock import Mock
|
|
|
|
|
2019-05-13 21:05:06 +03:00
|
|
|
from netaddr import IPSet
|
|
|
|
|
2019-01-22 02:29:47 +03:00
|
|
|
from twisted.internet import defer
|
2018-09-13 17:15:51 +03:00
|
|
|
from twisted.internet.defer import TimeoutError
|
|
|
|
from twisted.internet.error import ConnectingCancelledError, DNSLookupError
|
2019-01-18 15:07:38 +03:00
|
|
|
from twisted.test.proto_helpers import StringTransport
|
2018-09-13 17:15:51 +03:00
|
|
|
from twisted.web.client import ResponseNeverReceived
|
2018-09-18 20:17:15 +03:00
|
|
|
from twisted.web.http import HTTPChannel
|
2018-09-13 17:15:51 +03:00
|
|
|
|
2019-01-08 14:04:28 +03:00
|
|
|
from synapse.api.errors import RequestSendFailed
|
2018-09-18 20:17:15 +03:00
|
|
|
from synapse.http.matrixfederationclient import (
|
|
|
|
MatrixFederationHttpClient,
|
|
|
|
MatrixFederationRequest,
|
|
|
|
)
|
2020-03-24 17:45:33 +03:00
|
|
|
from synapse.logging.context import SENTINEL_CONTEXT, LoggingContext, current_context
|
2018-09-13 17:15:51 +03:00
|
|
|
|
2018-09-18 20:17:15 +03:00
|
|
|
from tests.server import FakeTransport
|
2018-09-13 17:15:51 +03:00
|
|
|
from tests.unittest import HomeserverTestCase
|
|
|
|
|
|
|
|
|
2019-01-22 02:29:47 +03:00
|
|
|
def check_logcontext(context):
|
2020-03-24 17:45:33 +03:00
|
|
|
current = current_context()
|
2019-01-22 02:29:47 +03:00
|
|
|
if current is not context:
|
2019-05-10 08:12:11 +03:00
|
|
|
raise AssertionError("Expected logcontext %s but was %s" % (context, current))
|
2019-01-22 02:29:47 +03:00
|
|
|
|
|
|
|
|
2018-09-13 17:15:51 +03:00
|
|
|
class FederationClientTests(HomeserverTestCase):
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
hs = self.setup_test_homeserver(reactor=reactor, clock=clock)
|
|
|
|
return hs
|
|
|
|
|
|
|
|
def prepare(self, reactor, clock, homeserver):
|
2019-02-11 21:03:30 +03:00
|
|
|
self.cl = MatrixFederationHttpClient(self.hs, None)
|
2018-09-13 17:15:51 +03:00
|
|
|
self.reactor.lookups["testserv"] = "1.2.3.4"
|
|
|
|
|
2019-01-22 02:29:47 +03:00
|
|
|
def test_client_get(self):
|
|
|
|
"""
|
|
|
|
happy-path test of a GET request
|
|
|
|
"""
|
2019-05-10 08:12:11 +03:00
|
|
|
|
2019-01-22 02:29:47 +03:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def do_request():
|
|
|
|
with LoggingContext("one") as context:
|
|
|
|
fetch_d = self.cl.get_json("testserv:8008", "foo/bar")
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(fetch_d)
|
|
|
|
|
|
|
|
# should have reset logcontext to the sentinel
|
2020-03-24 17:45:33 +03:00
|
|
|
check_logcontext(SENTINEL_CONTEXT)
|
2019-01-22 02:29:47 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
fetch_res = yield fetch_d
|
2019-07-23 16:00:55 +03:00
|
|
|
return fetch_res
|
2019-01-22 02:29:47 +03:00
|
|
|
finally:
|
|
|
|
check_logcontext(context)
|
|
|
|
|
|
|
|
test_d = do_request()
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-22 02:29:47 +03:00
|
|
|
self.assertEqual(port, 8008)
|
|
|
|
|
|
|
|
# complete the connection and wire it up to a fake transport
|
|
|
|
protocol = factory.buildProtocol(None)
|
|
|
|
transport = StringTransport()
|
|
|
|
protocol.makeConnection(transport)
|
|
|
|
|
|
|
|
# that should have made it send the request to the transport
|
|
|
|
self.assertRegex(transport.value(), b"^GET /foo/bar")
|
2019-01-25 15:38:16 +03:00
|
|
|
self.assertRegex(transport.value(), b"Host: testserv:8008")
|
2019-01-22 02:29:47 +03:00
|
|
|
|
|
|
|
# Deferred is still without a result
|
|
|
|
self.assertNoResult(test_d)
|
|
|
|
|
|
|
|
# Send it the HTTP response
|
2019-06-20 12:32:02 +03:00
|
|
|
res_json = '{ "a": 1 }'.encode("ascii")
|
2019-01-22 02:29:47 +03:00
|
|
|
protocol.dataReceived(
|
|
|
|
b"HTTP/1.1 200 OK\r\n"
|
|
|
|
b"Server: Fake\r\n"
|
|
|
|
b"Content-Type: application/json\r\n"
|
|
|
|
b"Content-Length: %i\r\n"
|
|
|
|
b"\r\n"
|
|
|
|
b"%s" % (len(res_json), res_json)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
res = self.successResultOf(test_d)
|
|
|
|
|
|
|
|
# check the response is as expected
|
|
|
|
self.assertEqual(res, {"a": 1})
|
|
|
|
|
2018-09-13 17:15:51 +03:00
|
|
|
def test_dns_error(self):
|
|
|
|
"""
|
2019-01-18 15:07:38 +03:00
|
|
|
If the DNS lookup returns an error, it will bubble up.
|
2018-09-13 17:15:51 +03:00
|
|
|
"""
|
2018-09-18 20:17:15 +03:00
|
|
|
d = self.cl.get_json("testserv2:8008", "foo/bar", timeout=10000)
|
2018-09-13 17:15:51 +03:00
|
|
|
self.pump()
|
|
|
|
|
|
|
|
f = self.failureResultOf(d)
|
2019-01-08 14:04:28 +03:00
|
|
|
self.assertIsInstance(f.value, RequestSendFailed)
|
|
|
|
self.assertIsInstance(f.value.inner_exception, DNSLookupError)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
2019-01-22 02:29:47 +03:00
|
|
|
def test_client_connection_refused(self):
|
|
|
|
d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(d)
|
|
|
|
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(host, port, factory, _timeout, _bindAddress) = clients[0]
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(host, "1.2.3.4")
|
2019-01-22 02:29:47 +03:00
|
|
|
self.assertEqual(port, 8008)
|
|
|
|
e = Exception("go away")
|
|
|
|
factory.clientConnectionFailed(None, e)
|
|
|
|
self.pump(0.5)
|
|
|
|
|
|
|
|
f = self.failureResultOf(d)
|
|
|
|
|
|
|
|
self.assertIsInstance(f.value, RequestSendFailed)
|
|
|
|
self.assertIs(f.value.inner_exception, e)
|
|
|
|
|
2018-09-13 17:15:51 +03:00
|
|
|
def test_client_never_connect(self):
|
|
|
|
"""
|
|
|
|
If the HTTP request is not connected and is timed out, it'll give a
|
2018-09-24 18:51:59 +03:00
|
|
|
ConnectingCancelledError or TimeoutError.
|
2018-09-13 17:15:51 +03:00
|
|
|
"""
|
2018-09-18 20:17:15 +03:00
|
|
|
d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# Nothing happened yet
|
2019-01-18 15:07:38 +03:00
|
|
|
self.assertNoResult(d)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(clients[0][0], "1.2.3.4")
|
2018-09-13 17:15:51 +03:00
|
|
|
self.assertEqual(clients[0][1], 8008)
|
|
|
|
|
|
|
|
# Deferred is still without a result
|
2019-01-18 15:07:38 +03:00
|
|
|
self.assertNoResult(d)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
# Push by enough to time it out
|
|
|
|
self.reactor.advance(10.5)
|
|
|
|
f = self.failureResultOf(d)
|
|
|
|
|
2019-01-08 14:04:28 +03:00
|
|
|
self.assertIsInstance(f.value, RequestSendFailed)
|
|
|
|
self.assertIsInstance(
|
2019-05-10 08:12:11 +03:00
|
|
|
f.value.inner_exception, (ConnectingCancelledError, TimeoutError)
|
2019-01-08 14:04:28 +03:00
|
|
|
)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
def test_client_connect_no_response(self):
|
|
|
|
"""
|
|
|
|
If the HTTP request is connected, but gets no response before being
|
|
|
|
timed out, it'll give a ResponseNeverReceived.
|
|
|
|
"""
|
2018-09-18 20:17:15 +03:00
|
|
|
d = self.cl.get_json("testserv:8008", "foo/bar", timeout=10000)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# Nothing happened yet
|
2019-01-18 15:07:38 +03:00
|
|
|
self.assertNoResult(d)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
# Make sure treq is trying to connect
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
2019-06-20 12:32:02 +03:00
|
|
|
self.assertEqual(clients[0][0], "1.2.3.4")
|
2018-09-13 17:15:51 +03:00
|
|
|
self.assertEqual(clients[0][1], 8008)
|
|
|
|
|
|
|
|
conn = Mock()
|
|
|
|
client = clients[0][2].buildProtocol(None)
|
|
|
|
client.makeConnection(conn)
|
|
|
|
|
|
|
|
# Deferred is still without a result
|
2019-01-18 15:07:38 +03:00
|
|
|
self.assertNoResult(d)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
# Push by enough to time it out
|
|
|
|
self.reactor.advance(10.5)
|
|
|
|
f = self.failureResultOf(d)
|
|
|
|
|
2019-01-08 14:04:28 +03:00
|
|
|
self.assertIsInstance(f.value, RequestSendFailed)
|
|
|
|
self.assertIsInstance(f.value.inner_exception, ResponseNeverReceived)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
2019-05-13 21:05:06 +03:00
|
|
|
def test_client_ip_range_blacklist(self):
|
|
|
|
"""Ensure that Synapse does not try to connect to blacklisted IPs"""
|
|
|
|
|
|
|
|
# Set up the ip_range blacklist
|
2019-06-20 12:32:02 +03:00
|
|
|
self.hs.config.federation_ip_range_blacklist = IPSet(
|
|
|
|
["127.0.0.0/8", "fe80::/64"]
|
|
|
|
)
|
2019-05-13 21:05:06 +03:00
|
|
|
self.reactor.lookups["internal"] = "127.0.0.1"
|
|
|
|
self.reactor.lookups["internalv6"] = "fe80:0:0:0:0:8a2e:370:7337"
|
|
|
|
self.reactor.lookups["fine"] = "10.20.30.40"
|
|
|
|
cl = MatrixFederationHttpClient(self.hs, None)
|
|
|
|
|
|
|
|
# Try making a GET request to a blacklisted IPv4 address
|
|
|
|
# ------------------------------------------------------
|
|
|
|
# Make the request
|
|
|
|
d = cl.get_json("internal:8008", "foo/bar", timeout=10000)
|
|
|
|
|
|
|
|
# Nothing happened yet
|
|
|
|
self.assertNoResult(d)
|
|
|
|
|
|
|
|
self.pump(1)
|
|
|
|
|
|
|
|
# Check that it was unable to resolve the address
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 0)
|
|
|
|
|
|
|
|
f = self.failureResultOf(d)
|
|
|
|
self.assertIsInstance(f.value, RequestSendFailed)
|
|
|
|
self.assertIsInstance(f.value.inner_exception, DNSLookupError)
|
|
|
|
|
|
|
|
# Try making a POST request to a blacklisted IPv6 address
|
|
|
|
# -------------------------------------------------------
|
|
|
|
# Make the request
|
|
|
|
d = cl.post_json("internalv6:8008", "foo/bar", timeout=10000)
|
|
|
|
|
|
|
|
# Nothing has happened yet
|
|
|
|
self.assertNoResult(d)
|
|
|
|
|
|
|
|
# Move the reactor forwards
|
|
|
|
self.pump(1)
|
|
|
|
|
|
|
|
# Check that it was unable to resolve the address
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 0)
|
|
|
|
|
|
|
|
# Check that it was due to a blacklisted DNS lookup
|
|
|
|
f = self.failureResultOf(d, RequestSendFailed)
|
|
|
|
self.assertIsInstance(f.value.inner_exception, DNSLookupError)
|
|
|
|
|
|
|
|
# Try making a GET request to a non-blacklisted IPv4 address
|
|
|
|
# ----------------------------------------------------------
|
|
|
|
# Make the request
|
|
|
|
d = cl.post_json("fine:8008", "foo/bar", timeout=10000)
|
|
|
|
|
|
|
|
# Nothing has happened yet
|
|
|
|
self.assertNoResult(d)
|
|
|
|
|
|
|
|
# Move the reactor forwards
|
|
|
|
self.pump(1)
|
|
|
|
|
|
|
|
# Check that it was able to resolve the address
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertNotEqual(len(clients), 0)
|
|
|
|
|
|
|
|
# Connection will still fail as this IP address does not resolve to anything
|
|
|
|
f = self.failureResultOf(d, RequestSendFailed)
|
|
|
|
self.assertIsInstance(f.value.inner_exception, ConnectingCancelledError)
|
|
|
|
|
2018-09-13 17:15:51 +03:00
|
|
|
def test_client_gets_headers(self):
|
|
|
|
"""
|
|
|
|
Once the client gets the headers, _request returns successfully.
|
|
|
|
"""
|
2018-09-18 20:17:15 +03:00
|
|
|
request = MatrixFederationRequest(
|
2019-05-10 08:12:11 +03:00
|
|
|
method="GET", destination="testserv:8008", path="foo/bar"
|
2018-09-18 20:17:15 +03:00
|
|
|
)
|
|
|
|
d = self.cl._send_request(request, timeout=10000)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
conn = Mock()
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
client = clients[0][2].buildProtocol(None)
|
|
|
|
client.makeConnection(conn)
|
|
|
|
|
|
|
|
# Deferred does not have a result
|
2019-01-18 15:07:38 +03:00
|
|
|
self.assertNoResult(d)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
# Send it the HTTP response
|
|
|
|
client.dataReceived(b"HTTP/1.1 200 OK\r\nServer: Fake\r\n\r\n")
|
|
|
|
|
|
|
|
# We should get a successful response
|
|
|
|
r = self.successResultOf(d)
|
|
|
|
self.assertEqual(r.code, 200)
|
|
|
|
|
|
|
|
def test_client_headers_no_body(self):
|
|
|
|
"""
|
|
|
|
If the HTTP request is connected, but gets no response before being
|
|
|
|
timed out, it'll give a ResponseNeverReceived.
|
|
|
|
"""
|
|
|
|
d = self.cl.post_json("testserv:8008", "foo/bar", timeout=10000)
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
conn = Mock()
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
client = clients[0][2].buildProtocol(None)
|
|
|
|
client.makeConnection(conn)
|
|
|
|
|
|
|
|
# Deferred does not have a result
|
2019-01-18 15:07:38 +03:00
|
|
|
self.assertNoResult(d)
|
2018-09-13 17:15:51 +03:00
|
|
|
|
|
|
|
# Send it the HTTP response
|
|
|
|
client.dataReceived(
|
2019-05-10 08:12:11 +03:00
|
|
|
(
|
|
|
|
b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
|
|
|
|
b"Server: Fake\r\n\r\n"
|
|
|
|
)
|
2018-09-13 17:15:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# Push by enough to time it out
|
|
|
|
self.reactor.advance(10.5)
|
|
|
|
f = self.failureResultOf(d)
|
|
|
|
|
|
|
|
self.assertIsInstance(f.value, TimeoutError)
|
2018-09-18 20:17:15 +03:00
|
|
|
|
2019-03-14 00:21:03 +03:00
|
|
|
def test_client_requires_trailing_slashes(self):
|
|
|
|
"""
|
|
|
|
If a connection is made to a client but the client rejects it due to
|
|
|
|
requiring a trailing slash. We need to retry the request with a
|
2019-03-20 14:27:18 +03:00
|
|
|
trailing slash. Workaround for Synapse <= v0.99.3, explained in #3622.
|
2019-03-14 00:21:03 +03:00
|
|
|
"""
|
2019-05-10 08:12:11 +03:00
|
|
|
d = self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
|
2019-03-14 00:21:03 +03:00
|
|
|
|
2019-03-20 13:50:44 +03:00
|
|
|
# Send the request
|
2019-03-14 00:21:03 +03:00
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# there should have been a call to connectTCP
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(_host, _port, factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
|
|
|
|
# complete the connection and wire it up to a fake transport
|
|
|
|
client = factory.buildProtocol(None)
|
|
|
|
conn = StringTransport()
|
|
|
|
client.makeConnection(conn)
|
|
|
|
|
|
|
|
# that should have made it send the request to the connection
|
|
|
|
self.assertRegex(conn.value(), b"^GET /foo/bar")
|
|
|
|
|
2019-03-20 13:50:44 +03:00
|
|
|
# Clear the original request data before sending a response
|
|
|
|
conn.clear()
|
|
|
|
|
2019-03-14 00:21:03 +03:00
|
|
|
# Send the HTTP response
|
|
|
|
client.dataReceived(
|
|
|
|
b"HTTP/1.1 400 Bad Request\r\n"
|
|
|
|
b"Content-Type: application/json\r\n"
|
|
|
|
b"Content-Length: 59\r\n"
|
|
|
|
b"\r\n"
|
|
|
|
b'{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}'
|
|
|
|
)
|
|
|
|
|
2019-03-20 13:50:44 +03:00
|
|
|
# We should get another request with a trailing slash
|
2019-03-14 00:21:03 +03:00
|
|
|
self.assertRegex(conn.value(), b"^GET /foo/bar/")
|
|
|
|
|
|
|
|
# Send a happy response this time
|
|
|
|
client.dataReceived(
|
|
|
|
b"HTTP/1.1 200 OK\r\n"
|
|
|
|
b"Content-Type: application/json\r\n"
|
|
|
|
b"Content-Length: 2\r\n"
|
|
|
|
b"\r\n"
|
2019-06-20 12:32:02 +03:00
|
|
|
b"{}"
|
2019-03-14 00:21:03 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# We should get a successful response
|
|
|
|
r = self.successResultOf(d)
|
|
|
|
self.assertEqual(r, {})
|
|
|
|
|
2019-03-20 17:00:39 +03:00
|
|
|
def test_client_does_not_retry_on_400_plus(self):
|
|
|
|
"""
|
|
|
|
Another test for trailing slashes but now test that we don't retry on
|
|
|
|
trailing slashes on a non-400/M_UNRECOGNIZED response.
|
|
|
|
|
|
|
|
See test_client_requires_trailing_slashes() for context.
|
|
|
|
"""
|
2019-05-10 08:12:11 +03:00
|
|
|
d = self.cl.get_json("testserv:8008", "foo/bar", try_trailing_slash_on_400=True)
|
2019-03-20 17:00:39 +03:00
|
|
|
|
|
|
|
# Send the request
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# there should have been a call to connectTCP
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(_host, _port, factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
|
|
|
|
# complete the connection and wire it up to a fake transport
|
|
|
|
client = factory.buildProtocol(None)
|
|
|
|
conn = StringTransport()
|
|
|
|
client.makeConnection(conn)
|
|
|
|
|
|
|
|
# that should have made it send the request to the connection
|
|
|
|
self.assertRegex(conn.value(), b"^GET /foo/bar")
|
|
|
|
|
|
|
|
# Clear the original request data before sending a response
|
|
|
|
conn.clear()
|
|
|
|
|
|
|
|
# Send the HTTP response
|
|
|
|
client.dataReceived(
|
|
|
|
b"HTTP/1.1 404 Not Found\r\n"
|
|
|
|
b"Content-Type: application/json\r\n"
|
|
|
|
b"Content-Length: 2\r\n"
|
|
|
|
b"\r\n"
|
|
|
|
b"{}"
|
|
|
|
)
|
|
|
|
|
|
|
|
# We should not get another request
|
|
|
|
self.assertEqual(conn.value(), b"")
|
|
|
|
|
|
|
|
# We should get a 404 failure response
|
2019-03-20 17:08:57 +03:00
|
|
|
self.failureResultOf(d)
|
2019-03-20 17:00:39 +03:00
|
|
|
|
2018-09-18 20:17:15 +03:00
|
|
|
def test_client_sends_body(self):
|
2019-05-10 08:12:11 +03:00
|
|
|
self.cl.post_json("testserv:8008", "foo/bar", timeout=10000, data={"a": "b"})
|
2018-09-18 20:17:15 +03:00
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
client = clients[0][2].buildProtocol(None)
|
|
|
|
server = HTTPChannel()
|
|
|
|
|
|
|
|
client.makeConnection(FakeTransport(server, self.reactor))
|
|
|
|
server.makeConnection(FakeTransport(client, self.reactor))
|
|
|
|
|
|
|
|
self.pump(0.1)
|
|
|
|
|
|
|
|
self.assertEqual(len(server.requests), 1)
|
|
|
|
request = server.requests[0]
|
|
|
|
content = request.content.read()
|
|
|
|
self.assertEqual(content, b'{"a":"b"}')
|
2019-01-18 15:07:38 +03:00
|
|
|
|
|
|
|
def test_closes_connection(self):
|
|
|
|
"""Check that the client closes unused HTTP connections"""
|
|
|
|
d = self.cl.get_json("testserv:8008", "foo/bar")
|
|
|
|
|
|
|
|
self.pump()
|
|
|
|
|
|
|
|
# there should have been a call to connectTCP
|
|
|
|
clients = self.reactor.tcpClients
|
|
|
|
self.assertEqual(len(clients), 1)
|
|
|
|
(_host, _port, factory, _timeout, _bindAddress) = clients[0]
|
|
|
|
|
|
|
|
# complete the connection and wire it up to a fake transport
|
|
|
|
client = factory.buildProtocol(None)
|
|
|
|
conn = StringTransport()
|
|
|
|
client.makeConnection(conn)
|
|
|
|
|
|
|
|
# that should have made it send the request to the connection
|
|
|
|
self.assertRegex(conn.value(), b"^GET /foo/bar")
|
|
|
|
|
|
|
|
# Send the HTTP response
|
|
|
|
client.dataReceived(
|
|
|
|
b"HTTP/1.1 200 OK\r\n"
|
|
|
|
b"Content-Type: application/json\r\n"
|
|
|
|
b"Content-Length: 2\r\n"
|
|
|
|
b"\r\n"
|
|
|
|
b"{}"
|
|
|
|
)
|
|
|
|
|
|
|
|
# We should get a successful response
|
|
|
|
r = self.successResultOf(d)
|
|
|
|
self.assertEqual(r, {})
|
|
|
|
|
|
|
|
self.assertFalse(conn.disconnecting)
|
|
|
|
|
|
|
|
# wait for a while
|
|
|
|
self.pump(120)
|
|
|
|
|
|
|
|
self.assertTrue(conn.disconnecting)
|