fix #1249: catch exceptions when requesting with disabling allow_redirects

This commit is contained in:
debugtalk
2022-04-18 13:08:25 +08:00
parent 1572aced95
commit 9157401c3a
5 changed files with 76 additions and 5 deletions

View File

@@ -1,12 +1,16 @@
# Release History
## 3.1.10 (2022-04-18)
- fix #1249: catch exceptions when requesting with disabling allow_redirects
## 3.1.9 (2022-04-17)
- fix #1174: pydantic validation error when body is None
- fix #1209: only convert jmespath path for some fields in white list
- fix #1233: parse upload info with session variables
- fix #1246: catch exceptions when getting socket address failed
- fix #1247: catch exceptions caused by GA report failure
- fix #1246: catch exceptions caused by GA report failure
- fix #1247: catch exceptions when getting socket address failed
## 3.1.8 (2022-03-22)

View File

@@ -1,4 +1,4 @@
__version__ = "3.1.9"
__version__ = "3.1.10"
__description__ = "One-stop solution for HTTP(S) testing."
# import firstly for monkey patch if needed

View File

@@ -189,10 +189,11 @@ class HttpSession(requests.Session):
try:
server_ip, server_port = response.raw._connection.sock.getpeername()
except Exception:
self.data.address.server_ip = server_ip
self.data.address.server_port = server_port
logger.debug(f"server IP: {server_ip}, Port: {server_port}")
except Exception:
pass
# get length of the response content
content_size = int(dict(response.headers).get("content-length") or 0)

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "httprunner"
version = "3.1.9"
version = "3.1.10"
description = "One-stop solution for HTTP(S) testing."
license = "Apache-2.0"
readme = "docs/README.md"

View File

@@ -3,6 +3,7 @@ import unittest
from httprunner import loader
from httprunner.cli import main_run
from httprunner.client import HttpSession
from httprunner.runner import HttpRunner
@@ -38,3 +39,68 @@ class TestHttpRunner(unittest.TestCase):
self.assertTrue(os.path.exists("tests/data/debugtalk.py"))
self.assertTrue(os.path.exists("tests/data/a_b_c/T1_test.py"))
self.assertTrue(os.path.exists("tests/data/a_b_c/T2_3_test.py"))
class TestHttpSession(unittest.TestCase):
def setUp(self):
self.session = HttpSession()
def test_request_http(self):
self.session.request("get", "http://httpbin.org/get")
address = self.session.data.address
self.assertGreater(len(address.server_ip), 0)
self.assertEqual(address.server_port, 80)
self.assertGreater(len(address.client_ip), 0)
self.assertGreater(address.client_port, 10000)
def test_request_https(self):
self.session.request("get", "https://httpbin.org/get")
address = self.session.data.address
self.assertGreater(len(address.server_ip), 0)
self.assertEqual(address.server_port, 443)
self.assertGreater(len(address.client_ip), 0)
self.assertGreater(address.client_port, 10000)
def test_request_http_allow_redirects(self):
self.session.request(
"get",
"http://httpbin.org/redirect-to?url=https%3A%2F%2Fgithub.com",
allow_redirects=True)
address = self.session.data.address
self.assertNotEqual(address.server_ip, "N/A")
self.assertEqual(address.server_port, 443)
self.assertNotEqual(address.server_ip, "N/A")
self.assertGreater(address.client_port, 10000)
def test_request_https_allow_redirects(self):
self.session.request(
"get",
"https://httpbin.org/redirect-to?url=https%3A%2F%2Fgithub.com",
allow_redirects=True)
address = self.session.data.address
self.assertNotEqual(address.server_ip, "N/A")
self.assertEqual(address.server_port, 443)
self.assertNotEqual(address.server_ip, "N/A")
self.assertGreater(address.client_port, 10000)
def test_request_http_not_allow_redirects(self):
self.session.request(
"get",
"http://httpbin.org/redirect-to?url=https%3A%2F%2Fgithub.com",
allow_redirects=False)
address = self.session.data.address
self.assertEqual(address.server_ip, "N/A")
self.assertEqual(address.server_port, 0)
self.assertEqual(address.client_ip, "N/A")
self.assertEqual(address.client_port, 0)
def test_request_https_not_allow_redirects(self):
self.session.request(
"get",
"https://httpbin.org/redirect-to?url=https%3A%2F%2Fgithub.com",
allow_redirects=False)
address = self.session.data.address
self.assertEqual(address.server_ip, "N/A")
self.assertEqual(address.server_port, 0)
self.assertEqual(address.client_ip, "N/A")
self.assertEqual(address.client_port, 0)