def _response(response, status_code): fmt = request.accept_mimetypes.best_match(['application/json', 'text/html']) if fmt == 'text/html': response = render_response(response=response, status_code=status_code) else: response = json.dumps(response) return make_response(response, status_code)
def _response(response, status_code): fmt = request.accept_mimetypes.best_match(['application/json', 'text/html', 'application/x-votable+xml']) if fmt == 'text/html': response = render_response(response=response, status_code=status_code) elif fmt == 'application/x-votable+xml': response = render_template('votable.xml.j2', result=response["result"], mappings=votable_mappings) else: response = json.dumps(response) return make_response(response, status_code)
def test_error(self): response = dict(error="NotFoundException") expected = """\ <!doctype html> <html> <head><title>404 - An Error occurred</title></head> <body> <div class="error"> Error: NotFoundException </div></body> </html>""" html = render_response(response=response, status_code=404) self.assertAlmostEqual(expected, html, "Scalar response incorrect")
def test_table(self): response = { "result": { "table": { "metadata": { "elements": [{ "name": "deepForcedSourceId", "datatype": "long" }, { "name": "scienceCcdExposureId", "datatype": "long" }] }, "data": [[8404051561545729, 125230127], [8404051561545730, 125230127]] } } } expected = """\ <!doctype html> <html> <head><title>Found Object(s)</title></head> <body> <div class="result"> <table> <thead> <tr><th data-datatype="long">deepForcedSourceId</th><th data-datatype="long">scienceCcdExposureId</th> </tr> </thead> <tbody> <tr><td>8404051561545729</td><td>125230127</td></tr> <tr><td>8404051561545730</td><td>125230127</td></tr> </tbody> </table> </div> </body> </html>""" html = render_response(response=response, status_code=200) expected = [line.strip() for line in expected.splitlines(1)] html = [line.strip() for line in html.splitlines(1)] self.assertEqual(expected, html, "Table response incorrect")
def test_table(self): response = { "result": { "table": { "metadata": { "elements": [ {"name": "deepForcedSourceId", "datatype": "long"}, {"name": "scienceCcdExposureId", "datatype": "long"} ] }, "data": [ [8404051561545729, 125230127], [8404051561545730, 125230127] ] } } } expected = """\ <!doctype html> <html> <head><title>Found Object(s)</title></head> <body> <div class="result"> <table> <thead> <tr><th data-datatype="long">deepForcedSourceId</th><th data-datatype="long">scienceCcdExposureId</th> </tr> </thead> <tbody> <tr><td>8404051561545729</td><td>125230127</td></tr> <tr><td>8404051561545730</td><td>125230127</td></tr> </tbody> </table> </div> </body> </html>""" html = render_response(response=response, status_code=200) expected = self._string_to_list(expected) html = self._string_to_list(html) self.assertEqual(expected, html, "Table response incorrect")
def test_scalar(self): response = dict(result=result) expected = """\ <!doctype html> <html> <head><title>Found Object(s)</title></head> <body> <div class="result"> <table> <tbody> <tr><td>0</td><td>1</td></tr> </tbody> </table> </div> </body> </html>""" html = render_response(response=response, status_code=200) expected = self._string_to_list(expected) html = self._string_to_list(html) self.assertEquals(expected, html, "Scalar response incorrect")
def test_scalar(self): response = ScalarResponse(result) expected = """\ <!doctype html> <html> <head><title>Found Object(s)</title></head> <body> <div class="result"> <table> <tbody> <tr><td>0</td><td>1</td></tr> </tbody> </table> </div> </body> </html>""" html = render_response(response=response, status_code=200) expected = [line.strip() for line in expected.splitlines(1)] html = [line.strip() for line in html.splitlines(1)] self.assertEquals(expected, html, "Scalar response incorrect")