コード例 #1
0
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)
コード例 #2
0
ファイル: dbREST_v0.py プロジェクト: brianv0/dax_dbserv
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)
コード例 #3
0
    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")
コード例 #4
0
    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")
コード例 #5
0
    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")
コード例 #6
0
    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")
コード例 #7
0
    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")
コード例 #8
0
    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")
コード例 #9
0
    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")