def test_get_test_collection(app, test_data): """ Tests the ability of a GET request to obtain a collection of tests based on certain criteria. """ hs_data = test_data('humanssuck.net.har') cnn_data = test_data('cnn.com.har') with app.test_request_context(): t1 = Test(hs_data, name='hs_test_1') t1.save() t2 = Test(hs_data, name='hs_test_2') t2.save() t3 = Test(cnn_data, name='hs_test_1') t3.save() # Get all of them res = app.client.get(ENDPOINT) assert res.status_code == 200 res_json = json.loads(res.data.decode()) assert 'data' in res_json res_data = res_json['data'] # Filter by hostname only res = app.client.get('{0}?hostname=humanssuck.net'.format(ENDPOINT)) assert res.status_code == 200 res_json = json.loads(res.data.decode()) assert 'data' in res_json res_data = res_json['data']
def test_get_single_test(app, test_data): """ Tests ability to retrieve a single Test object with a GET request. """ data = test_data('humanssuck.net.har') with app.test_request_context(): t = Test(data, name='my_test') t.save() res = app.client.get('{0}{1}/'.format(ENDPOINT, t.id)) assert res.status_code == 200 res_json = json.loads(res.data.decode()) assert 'data' in res_json res_data = res_json['data'] assert res_data['hostname'] is not None assert res_data['name'] is not None assert res_data['browser_name'] is not None assert res_data['startedDateTime'] is not None assert 'pages' in res_data res_pages = res_data['pages'] assert len(res_pages) == 1
def test_tests_model(app, test_data): """ When a new Test object is created, it should automatically create corresponding page entries, as well as store some of the fields from the provided HAR data. """ data = test_data('humanssuck.net.har') with app.test_request_context(): t = Test(data) t.save() assert t.pages assert len(t.pages) == 1 # TODO - test a HAR file with multiple pages assert t.hostname == 'humanssuck.net' assert t.browser_name == 'Firefox' assert t.browser_version == '25.0.1' assert t.data == data # MySQL does not store microseconds test_time = t.startedDateTime.replace(microsecond=0) assert test_time == datetime.datetime(2015, 2, 22, 19, 28, 12) assert t.har_data.decode() == data
def post(self): """ Given a string of HAR data, creates a new 'test' entry (as well as it's corresponding 'page' resources). **Example request**: .. sourcecode:: http POST /tests/ HTTP/1.1 Host: har-api.com Accept: application/json, text/javascript { "har_data": "{"log": {"pages": [{"id": "page_3", ......." } **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Vary: accept Content-Type: text/javascript { "data": { "browser_name": "Firefox", "browser_version": "25.0.1", "hostname": "humanssuck.net", "name": null, "pages": [ { "audio_load_time": 0.0, .... see page object for details ..... "video_size": 0.0 } ], "startedDateTime": "Sun, 22 Feb 2015 19:28:12 -0000" } } :<json string har_data: Raw HAR data (JSON) :<json string name: A custom name for the test, which can be used later for searching :>json integer id: System assigned ID :>json string browser_name: Name of browser used for test :>json string browser_version: Browser version used for test :>json string hostname: Hostname of the test :>json string name: Custom test name :>json array pages: An array of page objects :>json startedDateTime: Start date/time of the test run :statuscode 201: Test created without issue :statuscode 500: internal error """ parser = reqparse.RequestParser() parser.add_argument('har_data', help='har_data: String of HAR (JSON) data', required=True) parser.add_argument('name', help='Custom test name') args = parser.parse_args() har_test = Test(data=args['har_data']) har_test.save() return (har_test, 201)