import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static okhttp3.internal.http.StatusLine.HTTP_PERM_REDIRECT;
import static okhttp3.internal.http.StatusLine.HTTP_TEMP_REDIRECT;
if (!isNullOrEmpty(builder.login)) {
// password is null when login represents an access token. In this case
// the Basic credentials consider an empty password.
- okHttpClientBuilder.setCredentials(Credentials.basic(builder.login, nullToEmpty(builder.password)));
+ okHttpClientBuilder.setCredentials(Credentials.basic(builder.login, nullToEmpty(builder.password), UTF_8));
}
this.systemPassCode = builder.systemPassCode;
okHttpClientBuilder.setProxy(builder.proxy);
import okhttp3.Response;
import static com.google.common.base.Strings.nullToEmpty;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
/**
return null;
}
if (HttpURLConnection.HTTP_PROXY_AUTH == response.code()) {
- String credential = Credentials.basic(proxyLogin, nullToEmpty(proxyPassword));
+ String credential = Credentials.basic(proxyLogin, nullToEmpty(proxyPassword), UTF_8);
return response.request().newBuilder().header(PROXY_AUTHORIZATION, credential).build();
}
return null;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
+import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.rules.TemporaryFolder;
import org.sonarqube.ws.MediaTypes;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static okhttp3.Credentials.basic;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theLogin", ""));
}
+ @Test
+ public void use_basic_authentication_with_utf8_login_and_password() throws Exception {
+ answerHelloWorld();
+ String login = "ζθ½";
+ String password = "εδΈ";
+ underTest = HttpConnector.newBuilder()
+ .url(serverUrl)
+ .credentials(login, password)
+ .build();
+
+ GetRequest request = new GetRequest("api/issues/search");
+ underTest.call(request);
+
+ RecordedRequest recordedRequest = server.takeRequest();
+ // do not use OkHttp Credentials.basic() in order to not use the same code as the code under test
+ String expectedHeader = "Basic " + Base64.encodeBase64String((login + ":" + password).getBytes(UTF_8));
+ assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(expectedHeader);
+ }
+
/**
* Access token replaces the couple {login,password} and is sent through
* the login field