How to use set_headers method in pyresttest

Best Python code snippet using pyresttest_python

apps.py

Source:apps.py Github

copy

Full Screen

...11BASE_DIR = os.path.dirname(os.path.abspath(__file__))12def admin_reg(env,set_headers):13 status = '200 OK'14 headers = [('Content-Type', 'text/html;charset=utf-8')]15 set_headers(status, headers)16 filename = os.path.join(BASE_DIR, 'static/register.html')17 with open(filename,'r') as fd:18 data = fd.read()19 return data20def admin_log(env,set_headers):21 status = '200 OK'22 headers = [('Content-Type', 'text/html;charset=utf-8')]23 set_headers(status, headers)24 filename = os.path.join(BASE_DIR, 'static/heroindex.html')25 with open(filename,'rb') as fd:26 data = fd.read().decode('utf-8')27 return data28def msg_to_dict(msg):29 lst = msg.split('&')30 del lst[-1]31 print(lst)32 mdt = defaultdict(list)33 for i in lst:34 j = i.split('=')35 print(unquote(j[1]))36 mdt.setdefault('%s'%j[0], []).append(unquote(j[1]))37 return mdt38def register(env,set_headers):39 status = '200 OK'40 headers = [('Content-Type', 'text/html;charset=utf-8')]41 set_headers(status, headers)42 msg = env.get('MSG')43 print('**************')44 print(msg)45 res = do_register(msg)46 print('**************')47 print('res=',res)48 if res == 1:49 return '您已注册成功,请返回登录页面登录'50 else:51 return '用户名已存在,请返回注册页重新输入!'52def login(env,set_headers):53 status = '200 OK'54 headers = [('Content-Type', 'text/html;charset=utf-8')]55 set_headers(status, headers)56 msg = env.get('MSG')57 mdt = msg_to_dict(msg)58 print(mdt['username'][0])59 print(mdt['passwd'][0])60 if mdt['username'][0] == '' or mdt['passwd'][0] == '':61 return '用户名或密码有误,请重新输入!!'62 else:63 print('**************')64 print(mdt)65 res = do_login(mdt)66 print('**************')67 if res:68 filename = os.path.join(BASE_DIR, 'static/hero.html')69 print(filename)70 with open(filename,'r') as fd:71 data = fd.read()72 return data73 else:74 return 'Username or password is wrong!'75 # login_do = Dbs_Work(env,set_headers)76 # login_do.run()77def do_dbswork(env,set_headers):78 status = '200 OK'79 headers = [('Content-Type', 'text/html;charset=utf-8')]80 set_headers(status, headers)81 msg = env.get('MSG')82 if msg:83 login_do = Dbs_Work(env,set_headers)84 data = login_do.run()85 return data86 else:87 filename = os.path.join(BASE_DIR, 'static/hero.html')88 with open(filename,'r') as fd:89 data = fd.read()90 return data91class Dbs_Work(object):92 def __init__(self,env,set_headers):93 self.env = env94 self.set_headers = set_headers95 def run(self):96 self.msg = self.env.get('MSG')97 order_lines = self.env.get('PATH_INFO').split('/')98 self.mydict = msg_to_dict(self.msg)99 path = order_lines[-1]100 try:101 if path == 'add':102 data = self.add()103 # data = self.delete()104 elif path == 'delete':105 data = self.delete()106 elif path == 'change':107 data = self.change()108 elif path == 'search':109 print('开始查询了')110 print(self.mydict['hinfo'][0])111 data = self.search()112 except Exception as e:113 data = e114 return data115 def add(self):116 status = '200 OK'117 headers = [('Content-Type', 'text/html;charset=utf-8')]118 self.set_headers(status, headers)119 if self.msg == '':120 data = 'Request error'121 elif self.msg[-4:] == '.txt':122 filename = self.msg123 data = workfunc2.add_all(filename)124 print(data)125 else:126 # filename = 'ff.txt'127 # workfunc2.add_all(filename)128 if self.mydict['hinfo']:129 filename = self.mydict['hinfo'][0]130 data = workfunc2.add_all(filename)131 else:132 res = workfunc2.search_hid(self.mydict)133 if res == 'Nothing':134 data = workfunc2.add_one(self.mydict)135 else:136 data = '英雄ID已存在,请重新输入!' 137 return data138 def delete(self):139 status = '200 OK'140 headers = [('Content-Type', 'text/html;charset=utf-8')]141 self.set_headers(status, headers)142 # res = workfunc2.search_hid(self.mydict)143 # print('res=res=res=res=res=res=res=res=',res)144 # if res == 'Nothing':145 # return '不存在该英雄信息,请重新输入!'146 data = workfunc2.delete(self.mydict)147 return data148 def change(self):149 status = '200 OK'150 headers = [('Content-Type', 'text/html;charset=utf-8')]151 self.set_headers(status, headers)152 data = workfunc2.change(self.mydict)153 return data154 def search(self):155 status = '200 OK'156 headers = [('Content-Type', 'text/html;charset=utf-8')]157 self.set_headers(status, headers)158 data = workfunc2.search(self.mydict)...

Full Screen

Full Screen

WebFramework.py

Source:WebFramework.py Github

copy

Full Screen

...23 except IOError:24 #代表没有找到该静态网页25 status = "404 Not Fount"26 headers = []27 set_headers(status,headers)28 return "<h1>== Sorry not found the page==<h1>"29 else:30 file_data = fd.read()31 fd.close()32 status = "200 OK"33 headers = []34 set_headers(status,headers)35 return file_data.decode("utf-8")36 else:37 for url,handler in self.urls:38 if path == url:39 return handler(env,set_headers)40 #请求的url未找到41 status = "404 Not Found"42 headers = []43 set_headers(status,headers)44 return "Sorry url not found" 45def show_time(env,set_headers):46 status = "200 OK"47 headers = []48 set_headers(status,headers)49 return time.ctime()50def say_hello(env,set_headers):51 status = "200 OK"52 headers = []53 set_headers(status,headers)54 return "hello world"55def say_bye(env,set_headers):56 status = "200 OK"57 headers = []58 set_headers(status,headers)59 return "Good Bye"60urls = [61 ('/time',show_time),62 ("/hello",say_hello),63 ("/bye",say_bye),64]...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pyresttest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful