-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_requests.py
More file actions
71 lines (54 loc) · 2.33 KB
/
test_requests.py
File metadata and controls
71 lines (54 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import random
from bs4 import BeautifulSoup
def abs_url(url, port=8000):
return f'http://127.0.0.1:{port}{url}'
def test_http_get_index():
url = '/'
response = requests.get(abs_url(url))
assert response.status_code == 200
def test_http_get_params_detail():
url = '/person/'
response = requests.get(abs_url(url), params={'id': '1'})
assert response.status_code == 200
response = requests.get(abs_url(url), params={'id': '10000'})
assert response.status_code == 404
response = requests.get(abs_url(url))
assert response.status_code == 404
def test_post_not_auth():
url = '/create/'
response = requests.get(abs_url(url))
assert response.status_code == 200
response == requests.post(abs_url(url), data={'name': 'Mark', 'age': random.randint(1, 100)})
assert response.status_code == 200
def test_post_auth():
url = '/create-auth/'
response = requests.get(abs_url(url))
assert response.status_code == 404
# response = requests.get(abs_url(url), auth=('admin', 'admin123456'))
# assert response.status_code == 200
# session = requests.Session()
# session.auth = ('admin', 'admin123456')
#
# response = session.get(abs_url(url))
# assert response.status_code == 200
# headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
# 'Cookie': 'csrftoken=esXp1bCa9zsLQjznqo1w688nIJohppsg5XsJ1jabihNpgbfIi62jZqlWBDif9PB4; sessionid=8ykujv76stb5vsw47q5h2lygvyj0m7hk'}
# response = requests.get(abs_url(url), headers=headers)
# assert response.status_code == 200
#
# response == requests.post(abs_url(url), data={'name': 'Bob', 'age': random.randint(1, 100)}, headers=headers)
# assert response.status_code == 200
def test_bs4_class():
url = '/'
response = requests.get(abs_url(url))
soup = BeautifulSoup(response.text, "html.parser")
buttons = soup.findAll('a', {'class': 'btn-primary'})
href = buttons[0].get('href')
assert href == '/create/'
def test_bs4_js():
url = '/person/'
response = requests.get(abs_url(url), params={'id': '1'})
soup = BeautifulSoup(response.text, "html.parser")
texts = soup.findAll('text', {'class': 'text-danger'})
assert texts == []