Replace concatenated strings with text blocks

See gh-29408
This commit is contained in:
Kulwant Singh 2022-10-31 22:56:47 +01:00 committed by Sam Brannen
parent 19955c5b77
commit eec3c6f7bb
4 changed files with 84 additions and 41 deletions

View File

@ -614,22 +614,23 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig
private class IframeHandler implements SockJsRequestHandler {
private static final String IFRAME_CONTENT =
"<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />\n" +
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
" <script>\n" +
" document.domain = document.domain;\n" +
" _sockjs_onload = function(){SockJS.bootstrap_iframe();};\n" +
" </script>\n" +
" <script src=\"%s\"></script>\n" +
"</head>\n" +
"<body>\n" +
" <h2>Don't panic!</h2>\n" +
" <p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>\n" +
"</body>\n" +
"</html>";
"""
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script>
document.domain = document.domain;
_sockjs_onload = function(){SockJS.bootstrap_iframe();};
</script>
<script src="%s"></script>
</head>
<body>
<h2>Don't panic!</h2>
<p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>
</body>
</html>""";
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {

View File

@ -60,18 +60,19 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
static {
StringBuilder sb = new StringBuilder(
"<!doctype html>\n" +
"<html><head>\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />\n" +
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
"</head><body><h2>Don't panic!</h2>\n" +
" <script>\n" +
" document.domain = document.domain;\n" +
" var c = parent.%s;\n" +
" c.start();\n" +
" function p(d) {c.message(d);};\n" +
" window.onload = function() {c.stop();};\n" +
" </script>"
"""
<!doctype html>
<html><head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head><body><h2>Don't panic!</h2>
<script>
document.domain = document.domain;
var c = parent.%s;
c.start();
function p(d) {c.message(d);};
window.onload = function() {c.stop();};
</script>"""
);
while (sb.length() < MINIMUM_PARTIAL_HTML_CONTENT_LENGTH) {

View File

@ -109,7 +109,11 @@ public class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages().size()).isEqualTo(1);
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
assertThat(textMessage.getPayload()).isEqualTo(("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000"));
assertThat(textMessage.getPayload()).isEqualTo(("""
CONNECTED
user-name:joe
\u0000"""));
}
@Test
@ -123,7 +127,11 @@ public class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages().size()).isEqualTo(1);
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
assertThat(textMessage.getPayload()).isEqualTo(("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000"));
assertThat(textMessage.getPayload()).isEqualTo(("""
CONNECTED
user-name:joe
\u0000"""));
}
@Test
@ -142,8 +150,13 @@ public class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages().size()).isEqualTo(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload()).isEqualTo(("CONNECTED\n" + "version:1.2\n" + "heart-beat:15000,15000\n" +
"user-name:joe\n" + "\n" + "\u0000"));
assertThat(actual.getPayload()).isEqualTo(("""
CONNECTED
version:1.2
heart-beat:15000,15000
user-name:joe
\u0000"""));
}
@Test
@ -161,8 +174,13 @@ public class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages().size()).isEqualTo(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload()).isEqualTo(("CONNECTED\n" + "version:1.0\n" + "heart-beat:0,0\n" +
"user-name:joe\n" + "\n" + "\u0000"));
assertThat(actual.getPayload()).isEqualTo(("""
CONNECTED
version:1.0
heart-beat:0,0
user-name:joe
\u0000"""));
}
@Test
@ -178,8 +196,12 @@ public class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages().size()).isEqualTo(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload()).isEqualTo(("ERROR\n" + "message:Session closed.\n" + "content-length:0\n" +
"\n\u0000"));
assertThat(actual.getPayload()).isEqualTo(("""
ERROR
message:Session closed.
content-length:0
\u0000"""));
}
@Test
@ -196,7 +218,11 @@ public class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages().size()).isEqualTo(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload()).isEqualTo(("RECEIPT\n" + "receipt-id:message-123\n" + "\n\u0000"));
assertThat(actual.getPayload()).isEqualTo(("""
RECEIPT
receipt-id:message-123
\u0000"""));
}
@Test
@ -391,7 +417,11 @@ public class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages()).hasSize(1);
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
assertThat(textMessage.getPayload())
.isEqualTo("CONNECTED\n" + "user-name:__pete__@gmail.com\n" + "\n" + "\u0000");
.isEqualTo("""
CONNECTED
user-name:__pete__@gmail.com
\u0000""");
}
@Test
@ -470,7 +500,11 @@ public class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages().size()).isEqualTo(1);
textMessage = (TextMessage) this.session.getSentMessages().get(0);
assertThat(textMessage.getPayload()).isEqualTo(("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000"));
assertThat(textMessage.getPayload()).isEqualTo(("""
CONNECTED
user-name:joe
\u0000"""));
this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, this.channel);

View File

@ -75,7 +75,10 @@ public class RestTemplateXhrTransportTests {
@Test
public void connectReceiveAndClose() throws Exception {
String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
String body = """
o
a["foo"]
c[3000,"Go away!"]""";
ClientHttpResponse response = response(HttpStatus.OK, body);
connect(response);
@ -157,7 +160,11 @@ public class RestTemplateXhrTransportTests {
@Test
public void responseClosedAfterDisconnected() throws Exception {
String body = "o\n" + "c[3000,\"Go away!\"]\n" + "a[\"foo\"]\n";
String body = """
o
c[3000,"Go away!"]
a["foo"]
""";
ClientHttpResponse response = response(HttpStatus.OK, body);
connect(response);