??????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????pithy????????????????????????????????????????μ????
????????????
???????????????????????
??????????????????????????????£??????????????????????????????????????й???????????顣????????????????????????????????????????????????????????????
???????????д??????
???????requets + unittest?????????????????????????requests??api???????????????????????????????????????????????????????????Щ???ù??????????????????????????дЧ???
???????????????
???????????????python2.7????汾???????????
????pip install flask
????pip install requests
????????????????flaskд??????????????????requests??????
????????????????
???????????flask???????http?????????????????????????飬?????????????????????demo.py??????????????windows???±??????????????copy?????????楨????
??????????
????#!/usr/bin/python
????# coding=utf-8
????from flask import Flask?? request?? session?? jsonify
????USERNAME = 'admin'
????PASSWORD = '123456'
????app = Flask(__name__)
????app.secret_key = 'pithy'
????@app.route('/login'?? methods=['GET'?? 'POST'])
????def login():
????error = None
????if request.method == 'POST':
????if request.form['username'] != USERNAME:
????error = 'Invalid username'
????elif request.form['password'] != PASSWORD:
????error = 'Invalid password'
????else:
????session['logged_in'] = True
????return jsonify({'code': 200?? 'msg': 'success'})
????return jsonify({'code': 401?? 'msg': error})?? 401
????@app.route('/info'?? methods=['get'])
????def info():
????if not session.get('logged_in'):
????return jsonify({'code': 401?? 'msg': 'please login !!'})
????return jsonify({'code': 200?? 'msg': 'success'?? 'data': 'info'})
????if __name__ == '__main__':
????app.run(debug=True)
?????????????????
????python demo.py
??????????£?
????* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
????* Restarting with stat
???????????????????????????
??????????
??????????
????????url
????/login
??????????
????post
????????????
????| ???????? | ???????? | ??????? | | :--------: | :-----: | :----: | | username | String | ??????? | | password | String | ??????? |
??????????
????| ???????? | ???????? | ??????? | | :--------: | :-----: | :----: | | code | Integer | ???code | | msg | String | ?????? |
??????????
????????url
????/info
??????????
????get
????????cookies
????| ???????? | ???????? | ??????? | | :--------: | :-----: | :----: | | session | String | session |
??????????
????| ???????? | ???????? | ??????? | | :--------: | :-----: | :----: | | code | Integer | ???code | | msg | String | ?????? | | data | String | ??????? |
?????塢??д??????
?????????·
???????requests [???????] ???????HTTP????
???????python???????unittestд????case??
??????????
#!/usr/bin/python
????# coding=utf-8
????import requests
????import unittest
????class TestLogin(unittest.TestCase):
????@classmethod
????def setUpClass(cls):
????cls.login_url = 'http://127.0.0.1:5000/login'
????cls.info_url = 'http://127.0.0.1:5000/info'
????cls.username = 'admin'
????cls.password = '123456'
????def test_login(self):
????"""
??????????
????"""
????data = {
????'username': self.username??
????'password': self.password
????}
????response = requests.post(self.login_url?? data=data).json()
????assert response['code'] == 200
????assert response['msg'] == 'success'
????def test_info(self):
????"""
????????info???
????"""
????data = {
????'username': self.username??
????'password': self.password
????}
????response_cookies = requests.post(self.login_url?? data=data).cookies
????session = response_cookies.get('session')
????assert session
????info_cookies = {
????'session': session
????}
????response = requests.get(self.info_url?? cookies=info_cookies).json()
????assert response['code'] == 200
????assert response['msg'] == 'success'
????assert response['data'] == 'info'
???????????
?????????????
????д?????????????????????????????????????????????????????????Σ??????ζ????д?????????????? ???????????????????????????????????????÷?????????????????ò?????????????????????£?
#!/usr/bin/python
????# coding=utf-8
????import requests
????import unittest
????try:
????from urlparse import urljoin
????except ImportError:
????from urllib.parse import urljoin
????class DemoApi(object):
????def __init__(self?? base_url):
????self.base_url = base_url
????def login(self?? username?? password):
????"""
??????????
????:param username: ?????
????:param password: ????
????"""
????url = urljoin(self.base_url?? 'login')
????data = {
????'username': username??
????'password': password
????}
????return requests.post(url?? data=data).json()
????def get_cookies(self?? username?? password):
????"""
??????????cookies
????"""
????url = urljoin(self.base_url?? 'login')
????data = {
????'username': username??
????'password': password
????}
????return requests.post(url?? data=data).cookies
????def info(self?? cookies):
????"""
??????????
????"""
????url = urljoin(self.base_url?? 'info')
????return requests.get(url?? cookies=cookies).json()
????class TestLogin(unittest.TestCase):
????@classmethod
????def setUpClass(cls):
????cls.base_url = 'http://127.0.0.1:5000'
????cls.username = 'admin'
????cls.password = '123456'
????cls.app = DemoApi(cls.base_url)
????def test_login(self):
????"""
??????????
????"""
????response = self.app.login(self.username?? self.password)
????assert response['code'] == 200
????assert response['msg'] == 'success'
????def test_info(self):
????"""
?????????????????
????"""
????cookies = self.app.get_cookies(self.username?? self.password)
????response = self.app.info(cookies)
????assert response['code'] == 200
????assert response['msg'] == 'success'
????assert response['data'] == 'info'
????OK??????????汾?У???????????????????÷??????????????????????????????????host??self.base_url??????????????????????????????????????http??????session?? cookie?????set?????????????????????session????????У?????????ù????У??????????????????????????????????????
?????e??????????????檔
????????cookies&????log???
???????requests?????????Session????(???????????Session ?????????????????????? cookie)???????????????????????????£?
#!/usr/bin/python
????# coding=utf-8
????import unittest
????from pprint import pprint
????from requests.sessions import Session
????try:
????from urlparse import urljoin
????except ImportError:
????from urllib.parse import urljoin
????class DemoApi(object):
????def __init__(self?? base_url):
????self.base_url = base_url
????# ????session???
????self.session = Session()
????def login(self?? username?? password):
????"""
??????????
????:param username: ?????
????:param password: ????
????"""
????url = urljoin(self.base_url?? 'login')
????data = {
????'username': username??
????'password': password
????}
????response = self.session.post(url?? data=data).json()
????print(' *****************************************')
????print(u' 1??????url: %s' % url)
????print(u' 2??????????:')
????pprint(self.session.headers)
????print(u' 3?????????:')
????pprint(data)
????print(u' 4?????:')
????pprint(response)
????return response
????def info(self):
????"""
??????????
????"""
????url = urljoin(self.base_url?? 'info')
????response = self.session.get(url).json()
????print(' *****************************************')
????print(u' 1??????url: %s' % url)
????print(u' 2??????????:')
????pprint(self.session.headers)
????print(u' 3??????cookies:')
????pprint(dict(self.session.cookies))
????print(u' 4?????:')
????pprint(response)
????return response
????class TestLogin(unittest.TestCase):
????@classmethod
????def setUpClass(cls):
????cls.base_url = 'http://127.0.0.1:5000'
????cls.username = 'admin'
????cls.password = '123456'
????cls.app = DemoApi(cls.base_url)
????def test_login(self):
????"""
??????????
????"""
????response = self.app.login(self.username?? self.password)
????assert response['code'] == 200
????assert response['msg'] == 'success'
????def test_info(self):
????"""
?????????????????
????"""
????self.app.login(self.username?? self.password)
????response = self.app.info()
????assert response['code'] == 200
????assert response['msg'] == 'success'
????assert response['data'] == 'info'
????????????????????????÷??????????У????????requests Session?????????cookies??????????ù????д??????????????????????????????????????????о???????????????????????д???print 1??2??3... ??url??????????????????????????????????????????????????url??????body???????????headers????????????????趨????????????????????????????????????????????????????????