ExecuteJavaScriptModifier implements CollectorJob { String encodeBasicAuth(String username, String password) { String usernameOrEmpty = StringUtils.defaultString(username); String passwordOrEmpty = StringUtils.defaultString(password); String authString = usernameOrEmpty + ":" + passwordOrEmpty; LOG.debug("authentication: '{}'", authString); byte[] encoded = Base64.encodeBase64(authString.getBytes(StandardCharsets.UTF_8)); LOG.debug("encoded bytes: '{}'", encoded); return new String(encoded); } ExecuteJavaScriptModifier(ExternalSnippetHttpClient httpClient,
      JavaScriptJobExecutor jsExecutor); @Override CollectorStepResult collect(); @Override void setParameters(Map<String, String> params); static final String NAME; }
@Test public void encodeBasicAuth_ComplexPassword_ShouldBeProperlyEncoded() { String actual = tested.encodeBasicAuth("admin", "zażółć!@#$%^&*()_+-={}|[]\\:\";'<>?,./"); String base64Encoded = "YWRtaW46emHFvMOzxYLEhyFAIyQlXiYqKClfKy09e318W11cOiI7Jzw+PywuLw=="; assertThat(actual, equalTo(base64Encoded)); }
@Test public void encodeBasicAuth_EmptyPassword_ShouldBeProperlyEncoded() { String actual = tested.encodeBasicAuth("admin", ""); String base64Encoded = "YWRtaW46"; assertThat(actual, equalTo(base64Encoded)); }
@Test public void encodeBasicAuth_NullPassword_ShouldBeTreatedAsEmptyString() { String actual = tested.encodeBasicAuth("admin", null); String base64Encoded = "YWRtaW46"; assertThat(actual, equalTo(base64Encoded)); }