Merge pull request #7543 from MarkEWaite/fix-URICheckEncodingMonitor-NPE

Fix URICheckEncodingMonitor NPE
This commit is contained in:
Mark Waite authored and GitHub committed 2022-12-25 10:40:05 -07:00
commit fe416de6a0
2 files changed
+32 -1

No files matched your search

@@ -43,7 +43,7 @@ public class URICheckEncodingMonitor extends AdministrativeMonitor {
if (!expected.equals(value)) {
String expectedHex = Util.toHexString(expected.getBytes(StandardCharsets.UTF_8));
String valueHex = Util.toHexString(value.getBytes(StandardCharsets.UTF_8));
String valueHex = value != null ? Util.toHexString(value.getBytes(StandardCharsets.UTF_8)) : null;
LOGGER.log(Level.CONFIG, "Expected to receive: " + expected + " (" + expectedHex + ") but got: " + value + " (" + valueHex + ")");
return FormValidation.warningWithMarkup(hudson.model.Messages.Hudson_NotUsesUTF8ToDecodeURL());
}
@@ -0,0 +1,31 @@
package jenkins.diagnostics;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import hudson.util.FormValidation;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.kohsuke.stapler.Stapler;
public class URICheckEncodingMonitorTest {
@Rule
public JenkinsRule j = new JenkinsRule();
/* Test that an empty value returned to URICheckEncodingMonitor
* does not cause a null pointer exception
*/
@Test
public void emptyValueInResponse() throws Exception {
j.executeOnServer(() -> {
URICheckEncodingMonitor monitor = new URICheckEncodingMonitor();
FormValidation validation = monitor.doCheckURIEncoding(Stapler.getCurrentRequest());
assertThat(validation.kind, is(FormValidation.Kind.WARNING));
assertThat(validation.getMessage(), containsString("Your container doesn’t use UTF-8 to decode URLs."));
return null;
});
}
}