-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
33 lines (27 loc) · 1.18 KB
/
test_app.py
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
import pytest
from flask import Flask
from app import app
@pytest.fixture
def client():
with app.test_client() as client:
yield client
def test_get_ip_remote_addr(client):
response = client.get('/')
assert response.status_code == 200
assert response.data.decode('utf-8') == '127.0.0.1'
def test_get_ip_cf_connecting_ip(client):
response = client.get('/', headers={'CF-Connecting-IP': '203.0.113.1'})
assert response.status_code == 200
assert response.data.decode('utf-8') == '203.0.113.1'
def test_get_ip_x_forwarded_for(client):
response = client.get('/', headers={'X-Forwarded-For': '203.0.113.1, 198.51.100.2'})
assert response.status_code == 200
assert response.data.decode('utf-8') == '203.0.113.1'
def test_get_ip_invalid_cf_connecting_ip(client):
response = client.get('/', headers={'CF-Connecting-IP': 'invalid-ip'})
assert response.status_code == 200
assert response.data.decode('utf-8') == '127.0.0.1'
def test_get_ip_invalid_x_forwarded_for(client):
response = client.get('/', headers={'X-Forwarded-For': 'invalid-ip, 198.51.100.2'})
assert response.status_code == 200
assert response.data.decode('utf-8') == '198.51.100.2'