-
Notifications
You must be signed in to change notification settings - Fork 7
/
testserver.py
103 lines (70 loc) · 2.5 KB
/
testserver.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from __future__ import unicode_literals
from flask import Flask, jsonify, make_response, request
import os
app = Flask(__name__)
app.config.from_pyfile('testserver.cfg')
test_server = os.environ.get('TEST_SERVER', None)
if test_server:
app.config['SERVER_NAME'] = test_server.split('://')[-1]
# A global to store state between GET polling requests
_poll_counter = 3
@app.route('/behave-http', methods=['HEAD', 'GET'])
def test_i_am_a_testserver():
return ''
@app.route('/test/head', methods=['HEAD'])
def test_head():
return ''
@app.route('/test/get', methods=['GET'])
def test_get():
return make_response(jsonify({}))
@app.route('/different-prefix/get', methods=['GET'])
def test_get_another_prefix():
return make_response("different prefix", 200)
@app.route('/test/post', methods=['POST'])
def test_post():
return make_response('', 204)
@app.route('/test/post/mirror/json', methods=['POST'])
def test_post_json_mirror():
return make_response(jsonify(request.get_json()), 201)
@app.route('/test/options', methods=['OPTIONS'])
def test_options():
resp = make_response('', 200)
resp.headers['Allow'] = 'HEAD, GET, OPTIONS'
return resp
@app.route('/test/put/json', methods=['PUT'])
def test_put_json():
return make_response(jsonify(request.get_json()), 200)
@app.route('/test/patch/json', methods=['PATCH'])
def test_patch_json():
return make_response(jsonify(request.get_json()), 200)
@app.route('/test/delete', methods=['DELETE'])
def test_delete():
return make_response('', 204)
@app.route('/test/trace', methods=['TRACE'])
def test_trace():
body = '\n'.join(
['{0}: {1}'.format(h, v) for h, v in request.headers.items()])
resp = make_response(body, 200)
resp.headers['Content-Type'] = 'message/http'
return resp
@app.route('/test/get/args-to-json', methods=['GET'])
def test_get_args_to_json():
resp = make_response(jsonify(request.args.to_dict(flat=True)))
return resp
@app.route('/test/get/poll', methods=['GET'])
def test_get_poll():
global _poll_counter
if not _poll_counter > 0:
_poll_counter = 3
else:
_poll_counter -= 1
resp = dict(counter=_poll_counter)
return make_response(jsonify(resp))
@app.route('/test/get/basic-auth', methods=['GET'])
def test_basic_auth():
if (request.authorization.username == 'test-user' and
request.authorization.password == 'test-password'):
return make_response('', 200)
return make_response('', 401)
if __name__ == '__main__':
app.run()