关键词:configHttp.py
一、配置HTTP
1)接口类型:
接口一般来说有两种,一种是程序内部的接口,一种是系统对外的接口。
(1) webservice接口:走soap协议通过http传输,请求报文和返回报文都是xml格式的,我们在测试的时候都要通过工具才能进行调用,测试。
(2) http api 接口:走http协议,通过路径来区分调用的方法,请求报文都是key-value形式的,返回报文一般都是json串,有get和post等方法。
2)接口的请求方法:get()和post()
常用的几种接口请求方式:
(1) GET:从指定资源获取数据
(2) POST:向指定的资源请求被处理的数据(例如用户登录)
(3) PUT:上传指定的URL,一般是修改,可以理解为数据库中的 update
(4) DELETE:删除指定资源
3)get()方法对应的参数:url, headers, params, timeout
对于requests提供的get方法,有几个常用的参数:
url:显而易见,就是接口的地址url啦
headers:定制请求头(headers),例如:content-type = application/x-www-form-urlencoded
params:用于传递测试接口所要用的参数,这里我们用python中的字典形式(key:value)进行参数的传递。
timeout:设置接口连接的最大时间(超过该时间会抛出超时错误)
4)post()方法对应的参数:url, headers, data, files, timeout
其中,传参的类型content-type有哪几种?
import requests
import readConfig as readConfig
from common.Log import MyLog as Log
import json
localReadConfig = readConfig.ReadConfig()
class ConfigHttp:
def __init__(self):
global scheme, host, port, timeout
scheme = localReadConfig.get_http("scheme")
host = localReadConfig.get_http("baseurl")
port = localReadConfig.get_http("port")
timeout = localReadConfig.get_http("timeout")
self.log = Log.get_log()
self.logger = self.log.get_logger()
self.headers = {}
self.params = {}
self.data = {}
self.url = None
self.files = {}
self.state = 0
def set_url(self, url):
""" set url :param: interface url :return: """
self.url = scheme+'://'+host+url
def set_headers(self, header):
""" set headers :param header: :return: """
self.headers = header
def set_params(self, param):
""" set params :param param: :return: """
self.params = param
def set_data(self, data):
""" set data :param data: :return: """
self.data = data
def set_files(self, filename):
""" set upload files :param filename: :return: """
if filename != '':
file_path = 'F:/AppTest/Test/interfaceTest/testFile/img/' + filename
self.files = {'file': open(file_path, 'rb')}
if filename == '' or filename is None:
self.state = 1
# defined http get method
def get(self):
""" defined get method :return: """
try:
response = requests.get(self.url, headers=self.headers, params=self.params, timeout=float(timeout))
# response.raise_for_status()
return response
except TimeoutError:
self.logger.error("Time out!")
return None
# defined http post method
# include get params and post data
# uninclude upload file
def post(self):
""" defined post method :return: """
try:
response = requests.post(self.url, headers=self.headers, params=self.params, data=self.data, timeout=float(timeout))
# response.raise_for_status()
return response
except TimeoutError:
self.logger.error("Time out!")
return None
# defined http post method
# include upload file
def postWithFile(self):
""" defined post method :return: """
try:
response = requests.post(self.url, headers=self.headers, data=self.data, files=self.files, timeout=float(timeout))
return response
except TimeoutError:
self.logger.error("Time out!")
return None
# defined http post method
# for json
def postWithJson(self):
""" defined post method :return: """
try:
response = requests.post(self.url, headers=self.headers, json=self.data, timeout=float(timeout))
return response
except TimeoutError:
self.logger.error("Time out!") return None
if __name__ == "__main__":
print("ConfigHTTP")