use of retrofit.client.Header in project enroscar by stanfy.
the class RetrofitClient method readResponse.
private Response readResponse(URLConnection connection) throws IOException {
int status = HttpURLConnection.HTTP_OK;
String reason = "";
if (connection instanceof HttpURLConnection) {
status = ((HttpURLConnection) connection).getResponseCode();
reason = ((HttpURLConnection) connection).getResponseMessage();
}
List<Header> headers = new ArrayList<>();
for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
String name = field.getKey();
for (String value : field.getValue()) {
headers.add(new Header(name, value));
}
}
String mimeType = connection.getContentType();
int length = connection.getContentLength();
InputStream stream;
if (status >= 400 && connection instanceof HttpURLConnection) {
stream = ((HttpURLConnection) connection).getErrorStream();
} else {
stream = connection.getInputStream();
}
TypedInput responseBody = new TypedInputStream(mimeType, length, stream);
return new Response(connection.getURL().toString(), status, reason, headers, responseBody);
}
use of retrofit.client.Header in project enroscar by stanfy.
the class RetrofitClient method prepareRequest.
private void prepareRequest(final URLConnection connection, final Request request) throws IOException {
if (connection instanceof HttpURLConnection) {
// HttpURLConnection artificially restricts request method
try {
((HttpURLConnection) connection).setRequestMethod(request.getMethod());
} catch (ProtocolException e) {
try {
methodField.set(connection, request.getMethod());
} catch (IllegalAccessException e1) {
throw RetrofitError.unexpectedError(getUrl(request), e1);
}
}
}
connection.setDoInput(true);
for (Header header : request.getHeaders()) {
connection.addRequestProperty(header.getName(), header.getValue());
}
TypedOutput body = request.getBody();
if (body != null) {
connection.setDoOutput(true);
connection.addRequestProperty("Content-Type", body.mimeType());
long length = body.length();
if (length != -1) {
connection.addRequestProperty("Content-Length", String.valueOf(length));
}
if (connection instanceof HttpURLConnection) {
if (length != -1) {
((HttpURLConnection) connection).setFixedLengthStreamingMode((int) length);
} else {
((HttpURLConnection) connection).setChunkedStreamingMode(CHUNK_SIZE);
}
}
body.writeTo(connection.getOutputStream());
}
}
use of retrofit.client.Header in project Varis-Android by dkhmelenko.
the class TestAuthPresenter method testTwoFactorAuth.
@Test
public void testTwoFactorAuth() {
final String login = "login";
final String password = "password";
String auth = EncryptionUtils.generateBasicAuthorization(login, password);
// rules for throwing a request for 2-factor auth
Header header = new Header(GithubApiService.TWO_FACTOR_HEADER, "required");
List<Header> headers = new ArrayList<>();
headers.add(header);
Response response = new Response("https://github.com", 401, "twoFactorAuth", headers, null);
TaskError taskError = new TaskError(401, "twoFactorAuth");
taskError.setResponse(response);
TaskException exception = new TaskException(taskError);
when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenThrow(exception);
mAuthPresenter.login(login, password);
verify(mTaskManager).createNewAuthorization(eq(auth), any(AuthorizationRequest.class));
verify(mAuthView).showTwoFactorAuth();
// rules for handling 2-factor auth continuation
final String securityCode = "123456";
final String gitHubToken = "gitHubToken";
Authorization authorization = new Authorization();
authorization.setToken(gitHubToken);
authorization.setId(1L);
when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), eq(securityCode), any(AuthorizationRequest.class))).thenReturn(authorization);
final String accessToken = "token";
AccessTokenRequest request = new AccessTokenRequest();
request.setGithubToken(gitHubToken);
AccessToken token = new AccessToken();
token.setAccessToken(accessToken);
when(mTravisRestClient.getApiService().auth(request)).thenReturn(token);
when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
mAuthPresenter.twoFactorAuth(securityCode);
verify(mTaskManager).startAuth(gitHubToken);
verify(mTaskManager).deleteAuthorization(auth, String.valueOf(authorization.getId()), securityCode);
verify(mAuthView, times(2)).hideProgress();
verify(mAuthView).finishView();
}
Aggregations