-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtest_live_server.py
More file actions
executable file
·84 lines (65 loc) · 2.76 KB
/
test_live_server.py
File metadata and controls
executable file
·84 lines (65 loc) · 2.76 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
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
from flask import url_for
class TestLiveServer:
def test_server_is_alive(self, live_server):
assert live_server._process
assert live_server._process.is_alive()
def test_server_url(self, live_server):
assert live_server.url() == 'http://127.0.0.1:%d' % live_server.port
assert live_server.url('/ping') == 'http://127.0.0.1:%d/ping' % live_server.port
def test_server_listening(self, live_server):
res = urlopen(live_server.url('/ping'))
assert res.code == 200
assert b'pong' in res.read()
def test_url_for(self, live_server):
assert url_for('ping', _external=True) == 'http://127.0.0.1:%s/ping' % live_server.port
def test_set_application_server_name(self, live_server):
assert live_server.app.config['SERVER_NAME'] == '127.0.0.1:%d' % live_server.port
@pytest.mark.options(server_name='example.com:5000')
def test_rewrite_application_server_name(self, live_server):
assert live_server.app.config['SERVER_NAME'] == 'example.com:%d' % live_server.port
def test_prevent_starting_live_server(self, appdir):
appdir.create_test_module('''
import pytest
def test_a(live_server):
assert live_server._process is None
''')
result = appdir.runpytest('-v', '--no-start-live-server')
result.stdout.fnmatch_lines(['*PASSED*'])
assert result.ret == 0
def test_start_live_server(self, appdir):
appdir.create_test_module('''
import pytest
def test_a(live_server):
assert live_server._process
assert live_server._process.is_alive()
''')
result = appdir.runpytest('-v', '--start-live-server')
result.stdout.fnmatch_lines(['*PASSED*'])
assert result.ret == 0
def test_add_endpoint_to_live_server(self, appdir):
appdir.create_test_module('''
import pytest
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
from flask import url_for
def test_a(live_server):
@live_server.app.route('/new-endpoint')
def new_endpoint():
return 'got it', 200
live_server.start()
res = urlopen(url_for('new_endpoint', _external=True))
assert res.code == 200
assert b'got it' in res.read()
''')
result = appdir.runpytest('-v', '--no-start-live-server')
result.stdout.fnmatch_lines(['*PASSED*'])
assert result.ret == 0