From 75dd723d77582b4333a28f9b61f5e490650d0f63 Mon Sep 17 00:00:00 2001 From: Carl-Fredrik Arvidson Date: Fri, 23 Apr 2021 13:55:56 +0200 Subject: [PATCH] Keep newlines in synthetic responses Signed-off-by: Carl-Fredrik Arvidson --- CHANGELOG.md | 4 ++++ orm/rendervarnish.py | 12 +++++++++--- test/test_rendervarnish.py | 7 +++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9de1538..9d986f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +### Added +- Keep newlines in synthetic responses + ## [1.1.2] - 2019-08-27 ### Added diff --git a/orm/rendervarnish.py b/orm/rendervarnish.py index 33b3479..669b156 100644 --- a/orm/rendervarnish.py +++ b/orm/rendervarnish.py @@ -18,8 +18,9 @@ def vcl_escape_regex(regex): return regex -def vcl_safe_string(string): - string = string.replace("\n", "") +def vcl_safe_string(string, strip_newline=True): + if strip_newline: + string = string.replace("\n", "") split = string.split(r'"}') if len(split) == 1: if r'"' in split[0]: @@ -209,7 +210,12 @@ def make_redirect_action(config_in, config_out, rule_id, indent_depth=0): def make_synth_resp_action(config_in, config_out, rule_id, indent_depth=0): - synth = indent(3) + "synthetic(" + vcl_safe_string(config_in) + ");" + synth = ( + indent(3) + + "synthetic(" + + vcl_safe_string(config_in, strip_newline=False) + + ");" + ) synth_clause = make_action_if_clause([synth], rule_id, indent_depth=2) config_out["synth"] = synth_clause config_out["sb"] = [indent(indent_depth) + 'return (synth(750, ""));'] diff --git a/test/test_rendervarnish.py b/test/test_rendervarnish.py index b8297c6..a12e532 100644 --- a/test/test_rendervarnish.py +++ b/test/test_rendervarnish.py @@ -65,6 +65,13 @@ def test_vcl_safe_string(self): exp_str = '"ab"' nice_newline_str = rendervarnish.vcl_safe_string(newline_str) self.assertEqual(nice_newline_str, exp_str) + # newline_body_str contains '\n' which should be kept in the string + newline_body_str = "\n\nHello\n\n" + exp_str = '"\n\nHello\n\n"' + nice_with_newlines_str = rendervarnish.vcl_safe_string(newline_body_str, + strip_newline=False) + self.assertEqual(nice_with_newlines_str, exp_str) + def test_get_unique_vcl_name(self): namespace = 'ns'