build url with base_url

This commit is contained in:
httprunner
2018-11-22 19:53:09 +08:00
parent f05f190d24
commit c9a97d3e1c
3 changed files with 37 additions and 23 deletions
+2 -14
View File
@@ -1,20 +1,17 @@
# encoding: utf-8 # encoding: utf-8
import re
import time import time
import requests import requests
import urllib3 import urllib3
from httprunner import logger from httprunner import logger
from httprunner.exceptions import ParamsError from httprunner.utils import build_url
from requests import Request, Response from requests import Request, Response
from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema, from requests.exceptions import (InvalidSchema, InvalidURL, MissingSchema,
RequestException) RequestException)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
absolute_http_url_regexp = re.compile(r"^https?://", re.I)
class ApiResponse(Response): class ApiResponse(Response):
@@ -42,15 +39,6 @@ class HttpSession(requests.Session):
self.base_url = base_url if base_url else "" self.base_url = base_url if base_url else ""
self.init_meta_data() self.init_meta_data()
def _build_url(self, path):
""" prepend url with hostname unless it's already an absolute URL """
if absolute_http_url_regexp.match(path):
return path
elif self.base_url:
return "{}/{}".format(self.base_url.rstrip("/"), path.lstrip("/"))
else:
raise ParamsError("base url missed!")
def init_meta_data(self): def init_meta_data(self):
""" initialize meta_data, it will store detail data of request and response """ initialize meta_data, it will store detail data of request and response
""" """
@@ -125,7 +113,7 @@ class HttpSession(requests.Session):
self.meta_data["request"]["start_timestamp"] = time.time() self.meta_data["request"]["start_timestamp"] = time.time()
# prepend url with hostname unless it's already an absolute URL # prepend url with hostname unless it's already an absolute URL
url = self._build_url(url) url = build_url(self.base_url, url)
kwargs.setdefault("timeout", 120) kwargs.setdefault("timeout", 120)
response = self._send_request_safe_mode(method, url, **kwargs) response = self._send_request_safe_mode(method, url, **kwargs)
+21 -9
View File
@@ -609,9 +609,12 @@ def _extend_with_api(teststep_dict, api_def_dict):
# TODO: merge & override request # TODO: merge & override request
teststep_dict["request"] = api_def_dict.pop("request", {}) teststep_dict["request"] = api_def_dict.pop("request", {})
# base_url # base_url
base_url = teststep_dict.pop("base_url", None) if "base_url" in teststep_dict:
if base_url: base_url = teststep_dict.pop("base_url")
teststep_dict["request"]["url"] = "{}/{}".format(base_url.rstrip("/"), teststep_dict["request"]["url"].lstrip("/")) teststep_dict["request"]["url"] = utils.build_url(
base_url,
teststep_dict["request"]["url"]
)
# verify # verify
if "verify" in teststep_dict: if "verify" in teststep_dict:
@@ -744,8 +747,9 @@ def __parse_teststeps(teststeps, config, project_mapping):
for teststep in teststeps: for teststep in teststeps:
# priority teststep > config # base_url & verify: priority teststep > config
teststep.setdefault("base_url", config_base_url) if config_base_url:
teststep.setdefault("base_url", config_base_url)
teststep.setdefault("verify", config_verify) teststep.setdefault("verify", config_verify)
if "testcase_def" in teststep: if "testcase_def" in teststep:
@@ -776,6 +780,10 @@ def __parse_teststeps(teststeps, config, project_mapping):
_parse_testcase(teststep, project_mapping) _parse_testcase(teststep, project_mapping)
else: else:
# teststep is API test, has two cases.
# (1) teststep has API reference
# (2) teststep is defined directly
# 1, config => teststeps # 1, config => teststeps
# override teststep variables # override teststep variables
teststep["variables"] = utils.extend_variables( teststep["variables"] = utils.extend_variables(
@@ -794,14 +802,18 @@ def __parse_teststeps(teststeps, config, project_mapping):
pass pass
if "api_def" in teststep: if "api_def" in teststep:
# case (1)
# 2, teststep => api # 2, teststep => api
api_def_dict = teststep.pop("api_def") api_def_dict = teststep.pop("api_def")
_extend_with_api(teststep, api_def_dict) _extend_with_api(teststep, api_def_dict)
else: else:
# base_url # case (2)
base_url = teststep.pop("base_url", None) if "base_url" in teststep:
if base_url: base_url = teststep.pop("base_url")
teststep["request"]["url"] = "{}/{}".format(base_url.rstrip("/"), teststep["request"]["url"].lstrip("/")) teststep["request"]["url"] = utils.build_url(
base_url,
teststep["request"]["url"]
)
def _parse_testcase(testcase, project_mapping): def _parse_testcase(testcase, project_mapping):
+14
View File
@@ -6,11 +6,15 @@ import io
import itertools import itertools
import json import json
import os.path import os.path
import re
import string import string
from datetime import datetime from datetime import datetime
from httprunner import exceptions, logger from httprunner import exceptions, logger
from httprunner.compat import OrderedDict, basestring, is_py2 from httprunner.compat import OrderedDict, basestring, is_py2
from httprunner.exceptions import ParamsError
absolute_http_url_regexp = re.compile(r"^https?://", re.I)
def set_os_environ(variables_mapping): def set_os_environ(variables_mapping):
@@ -48,6 +52,16 @@ def get_os_environ(variable_name):
raise exceptions.EnvNotFound(variable_name) raise exceptions.EnvNotFound(variable_name)
def build_url(base_url, path):
""" prepend url with hostname unless it's already an absolute URL """
if absolute_http_url_regexp.match(path):
return path
elif base_url:
return "{}/{}".format(base_url.rstrip("/"), path.lstrip("/"))
else:
raise ParamsError("base url missed!")
def query_json(json_content, query, delimiter='.'): def query_json(json_content, query, delimiter='.'):
""" Do an xpath-like query with json_content. """ Do an xpath-like query with json_content.
@param (dict/list/string) json_content @param (dict/list/string) json_content