emiliosetiadarma commented on code in PR #6193:
URL: https://github.com/apache/nifi/pull/6193#discussion_r919316353
##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutEmail.java:
##########
@@ -299,4 +303,55 @@ public void testOutgoingMessageWithFlowfileContent()
throws Exception {
assertEquals("[email protected]",
message.getRecipients(RecipientType.BCC)[1].toString());
}
-}
+ @Test
+ public void testUnrecognizedCharset() {
+ runner.setProperty(PutEmail.SMTP_HOSTNAME, "smtp-host");
+ runner.setProperty(PutEmail.HEADER_XMAILER, "TestingNiFi");
+ runner.setProperty(PutEmail.FROM, "[email protected]");
+ runner.setProperty(PutEmail.MESSAGE, "test message");
+ runner.setProperty(PutEmail.TO, "[email protected]");
+
+ // not one of the recognized charsets
+ runner.setProperty(PutEmail.INPUT_CHARACTER_SET, "NOT A CHARACTER
SET");
+
+ runner.assertNotValid();
+ }
+
+ @Test
+ public void testPutEmailWithMismatchedCharset() throws Exception {
+ // String specifically chosen to have characters encoded differently
in US_ASCII and UTF_8
+ final String rawString = "SoftwÄrë Ënginëër Ön NiFi";
+ final byte[] rawBytes = rawString.getBytes(StandardCharsets.US_ASCII);
+ final byte[] rawBytesUTF8 = rawString.getBytes(StandardCharsets.UTF_8);
+
+ // verify that the message bytes are different (some messages are not)
+ assertNotEquals(rawBytes, rawBytesUTF8);
+
+ runner.setProperty(PutEmail.SMTP_HOSTNAME, "smtp-host");
+ runner.setProperty(PutEmail.HEADER_XMAILER, "TestingNiFi");
+ runner.setProperty(PutEmail.FROM, "[email protected]");
+ runner.setProperty(PutEmail.MESSAGE, new String(rawBytes,
StandardCharsets.US_ASCII));
+ runner.setProperty(PutEmail.TO, "[email protected]");
+ runner.setProperty(PutEmail.INPUT_CHARACTER_SET,
StandardCharsets.UTF_8.name());
+
+ runner.enqueue("Some Text".getBytes());
+
+ runner.run();
+
+ runner.assertQueueEmpty();
+ runner.assertAllFlowFilesTransferred(PutEmail.REL_SUCCESS);
+
+ // Verify that the Message was populated correctly
+ assertEquals("Expected a single message to be sent", 1,
processor.getMessages().size());
+ Message message = processor.getMessages().get(0);
+ assertNotEquals(rawString, message.getContent());
Review Comment:
After looking at the test, I decided to rework it a bit. My goal in this
test was to show that the text `"SoftwÄrë Ënginëër Ön NiFi"` which has
characters that are not supported with US-ASCII (proven by the first
`assertNotEquals(rawBytes, rawBytesUTF8)`). This will result in a corrupted
message (`Softw?r? ?ngin??r ?n NiFi`) when improperly setting the
`INPUT_CHARACTER_SET`.
I reworked the test to send "SoftwÄrë Ënginëër Ön NiFi" while setting the
`INPUT_CHARACTER_SET` to US-ASCII (and making sure the result is a corrupted
message) - indicating that we must make sure that we specify the appropriate
character set for the message
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]