From d8ad1d4f5e9e067d333f641c2718419a5b312520 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Sat, 29 Nov 2014 09:21:50 -0800 Subject: [PATCH] GH-168: Add newline to end of page Fixes GH-168 --- httpbin/core.py | 8 +++++++- test_httpbin.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/httpbin/core.py b/httpbin/core.py index 2e8774b5..7a5d2b09 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -15,7 +15,7 @@ import random import base64 -from flask import Flask, Response, request, render_template, redirect, jsonify, make_response +from flask import Flask, Response, request, render_template, redirect, jsonify as flask_jsonify, make_response from werkzeug.datastructures import WWWAuthenticate from werkzeug.http import http_date from werkzeug.wrappers import BaseResponse @@ -37,6 +37,12 @@ '__utmb' ) +def jsonify(*args, **kwargs): + response = flask_jsonify(*args, **kwargs) + if not response.data.endswith(b'\n'): + response.data += b'\n' + return response + # Prevent WSGI from correcting the casing of the Location header BaseResponse.autocorrect_location_header = False diff --git a/test_httpbin.py b/test_httpbin.py index 8af40d81..585ebd57 100755 --- a/test_httpbin.py +++ b/test_httpbin.py @@ -24,6 +24,19 @@ def setUp(self): httpbin.app.debug = True self.app = httpbin.app.test_client() + def test_get(self): + response = self.app.get('/get', headers={'User-Agent': 'test'}) + self.assertEqual(response.status_code, 200) + data = json.loads(response.data.decode('utf-8')) + self.assertEqual(data['args'], {}) + self.assertEqual(data['headers']['Host'], 'localhost') + self.assertEqual(data['headers']['Content-Type'], '') + self.assertEqual(data['headers']['Content-Length'], '0') + self.assertEqual(data['headers']['User-Agent'], 'test') + self.assertEqual(data['origin'], None) + self.assertEqual(data['url'], 'http://localhost/get') + self.assertTrue(response.data.endswith(b'\n')) + def test_base64(self): greeting = u'Здравствуй, мир!' b64_encoded = _string_to_base64(greeting)