Skip to content

Commit 68af39f

Browse files
committed
Server: Add tests for server API
During testing some bugs were found in the server handlers, so those have been fixed as well.
1 parent 4acb7ab commit 68af39f

File tree

7 files changed

+755
-32
lines changed

7 files changed

+755
-32
lines changed

.appveyor.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ install:
5555
# Install Python packages
5656
- cmd: "%PIP3% install pyinstaller"
5757
- cmd: "%PIP3% install mkdocs"
58+
#- cmd: "%PIP3% install pydocstyle"
5859
- cmd: "%PIP3% install coverage"
60+
- cmd: "%PIP3% install requests"
5961
- cmd: "%PIP% install coverage"
62+
- cmd: "%PIP% install requests"
6063
- cmd: "%PIP% install mock"
6164
- cmd: "%PIP% freeze"
6265
- cmd: "%PIP3% freeze"
@@ -82,6 +85,7 @@ test_script:
8285
- cmd: "%PYTHON%\\python -m coverage report"
8386
- cmd: "%PYTHON3%\\python -m coverage run ardublocklyserver\\tests\\run_all.py"
8487
- cmd: "%PYTHON3%\\python -m coverage report"
88+
#- cmd: pydocstyle ardublocklyserver --match-dir='ardublocklyserver'
8589

8690
# Push artefact to S3 bucket and list all
8791
before_deploy:

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ before_install:
3636
# Install Python packages (built with Python 3, tests for 2 and 3)
3737
- sudo python -m pip install mock
3838
- sudo python -m pip install coverage
39+
- sudo python -m pip install requests
3940
- pip3 install coverage
41+
- pip3 install requests
4042
- pip3 install coveralls
43+
#- pip3 install pydocstyle
4144
- pip3 install mkdocs
4245
- pip3 install pyinstaller
4346

@@ -77,6 +80,7 @@ script:
7780
- python -m coverage report
7881
- python3 -m coverage run ardublocklyserver/tests/run_all.py
7982
- python3 -m coverage report
83+
#- pydocstyle ardublocklyserver --match-dir='ardublocklyserver'
8084

8185
after_success:
8286
- coveralls

ardublocklyserver/__init__.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# -*- coding: utf-8 -*-
2-
#
3-
# ardublocklyserver package.
4-
#
5-
# Copyright (c) 2017 carlosperate https://github.com/carlosperate/
6-
# Licensed under the Apache License, Version 2.0 (the "License"):
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# There is a specific requirements for this Python project to not need
10-
# external module dependencies, so all third-party modules have been carefully
11-
# chosen with this purpose in mind and included in a folder named
12-
# 'local-packages'. The sys.path has to be expanded to be able to import these.
13-
#
2+
"""ardublocklyserver package.
3+
4+
Copyright (c) 2017 carlosperate https://github.com/carlosperate/
5+
Licensed under the Apache License, Version 2.0 (the "License"):
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
There is a specific requirements for this Python project to not need external
9+
module dependencies, so all third-party modules have been carefully chosen with
10+
this purpose in mind and included in a folder named 'local-packages'.
11+
The sys.path has to be expanded to be able to import these.
12+
"""
1413
import os
1514
import sys
1615

@@ -21,3 +20,7 @@
2120

2221
# Follows Semantic Versioning 2.0.0 http://semver.org/spec/v2.0.0.html
2322
__version__ = '0.1.3-a'
23+
24+
__author__ = 'carlosperate'
25+
__copyright__ = 'Copyright 2017, carlosperate https://github.com/carlosperate/'
26+
__license__ = 'Apache License, Version 2.0'

ardublocklyserver/server.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
from __future__ import unicode_literals, absolute_import, print_function
99
import os
10+
import sys
1011
# local-packages imports
1112
from bottle import request, response
1213
from bottle import static_file, run, default_app, redirect, abort
@@ -23,7 +24,7 @@
2324
document_root = ''
2425

2526

26-
def launch_server(ip='127.0.0.1', port=8000, document_root_=''):
27+
def launch_server(ip='localhost', port=8000, document_root_=''):
2728
"""Launch the Waitress server and Bottle framework with given settings.
2829
2930
:param ip: IP address to serve. Default to localhost, set to '0.0.0.0' to
@@ -45,6 +46,18 @@ def strip_path():
4546
request.environ['PATH_INFO'] = request.environ['PATH_INFO'].rstrip('/')
4647

4748

49+
def set_header_no_cache():
50+
"""Set the HTTP response to no cache the data.
51+
52+
Implementation depends on Python version.
53+
"""
54+
if sys.version_info[0] < 3:
55+
response.headers[
56+
'Cache-Control'.encode('ascii', 'ignore')] = 'no-cache'
57+
else:
58+
response.headers['Cache-Control'] = 'no-cache'
59+
60+
4861
#
4962
# Serving static files.
5063
#
@@ -155,7 +168,7 @@ def handler_settings_get_all():
155168
'selected': actions.get_load_ide_selected()
156169
}]
157170
}
158-
response.headers['Cache-Control'] = 'no-cache'
171+
set_header_no_cache()
159172
return response_dict
160173

161174

@@ -197,20 +210,23 @@ def handler_settings_get_individual(name):
197210
else:
198211
success = False
199212
response_dict.update({
213+
'settings_type': 'invalid',
200214
'errors': [{
201215
'id': 61,
202216
'description': 'Unexpected setting type requested.'
203217
}]})
204218
response_dict.update({'success': success})
205-
206-
response.headers['Cache-Control'] = 'no-cache'
219+
set_header_no_cache()
207220
return response_dict
208221

209222

210223
@app.put('/settings')
211224
def handler_settings_update_all():
212225
"""Handle the invalid PUT all settings requests.
213226
227+
There is no specific reason for this, is just not used by the client, and
228+
so there is no need to implement it at the moment.
229+
214230
Error codes:
215231
62 - Settings have to be individually updated.
216232
@@ -292,12 +308,14 @@ def handler_settings_update_individual(name):
292308
options = [{'value': k, 'display_text': v} for k, v in
293309
iteritems(actions.get_load_ide_options())]
294310
else:
295-
response_dict.update({'success': False})
311+
response_dict.update({'success': False,
312+
'settings_type': 'invalid'})
296313
response_dict.setdefault('errors', []).append({
297314
'id': 63,
298315
'description': 'Unexpected setting type to update.'
299316
})
300-
if set_value and set_value == new_value:
317+
if set_value and ((set_value == new_value)
318+
or name == 'compiler' or name == 'sketch'):
301319
response_dict.update({
302320
'success': True,
303321
'selected': set_value
@@ -310,6 +328,7 @@ def handler_settings_update_individual(name):
310328
'id': 67,
311329
'description': 'New value could not be set.'
312330
})
331+
set_header_no_cache()
313332
return response_dict
314333

315334

@@ -364,7 +383,7 @@ def handler_code_post():
364383
actions.arduino_ide_send_code(sketch_code)
365384
except Exception as e:
366385
exit_code = 52
367-
err_out = 'Unexpected server error.'
386+
err_out += 'Unexpected server error.'
368387
print('Error: Exception in arduino_ide_send_code:\n%s' % str(e))
369388

370389
response_dict.update({'success': success,
@@ -380,8 +399,5 @@ def handler_code_post():
380399
'description': 'More info available in the \'ide_data\' value.'
381400
}]
382401
})
402+
set_header_no_cache()
383403
return response_dict
384-
385-
386-
if __name__ == '__main__':
387-
launch_server('127.0.0.1', 8000, 'C:\\workspace\\git\\ardublockly')

ardublocklyserver/tests/run_all.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
#
4-
# Runs all the unit tests from this directory.
5-
#
6-
# This file manually adds the tests to be run.
7-
#
8-
# Copyright (c) 2017 carlosperate https://github.com/carlosperate/
9-
# Licensed under the Apache License, Version 2.0 (the "License"):
10-
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
3+
"""Runs all the unit tests from this directory.
4+
5+
This file manually adds the tests to be run.
6+
7+
Copyright (c) 2017 carlosperate https://github.com/carlosperate/
8+
Licensed under the Apache License, Version 2.0 (the "License"):
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
"""
1211
import os
1312
import sys
1413
import unittest
@@ -25,10 +24,12 @@
2524
from sketchcreator_test import SketchCreatorTestCase
2625
from compilersettings_test import ServerCompilerSettingsTestCase
2726
from actions_test import ActionsTestCase
27+
from server_test import ServerTestCase
2828

2929

3030
def run_tests():
3131
unittest.main()
3232

33+
3334
if __name__ == '__main__':
3435
run_tests()

0 commit comments

Comments
 (0)