WebDAV: Use the proper sendRequest mechanism to append messages

This commit is contained in:
Philip Whitehouse 2017-02-08 23:45:14 +00:00
parent 19b7d4491d
commit 2cec47491d
3 changed files with 29 additions and 27 deletions

View file

@ -27,6 +27,7 @@ import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@ -678,28 +679,8 @@ class WebDavFolder extends Folder<WebDavMessage> {
Log.i(LOG_TAG, "Uploading message as " + messageURL);
httpmethod = new HttpGeneric(messageURL);
httpmethod.setMethod("PUT");
httpmethod.setEntity(bodyEntity);
store.sendRequest(messageURL, "PUT", bodyEntity, null, true);
String mAuthString = store.getAuthString();
if (mAuthString != null) {
httpmethod.setHeader("Authorization", mAuthString);
}
response = httpclient.executeOverride(httpmethod, store.getContext());
statusCode = response.getStatusLine().getStatusCode();
if (statusCode < 200 ||
statusCode > 300) {
//TODO: Could we handle a login timeout here?
throw new IOException("Error with status code " + statusCode
+ " while sending/appending message. Response = "
+ response.getStatusLine().toString() + " for message " + messageURL);
}
WebDavMessage retMessage = new WebDavMessage(message.getUid(), this);
retMessage.setUrl(messageURL);

View file

@ -830,7 +830,7 @@ public class WebDavStore extends RemoteStore {
return mHttpClient;
}
private InputStream sendRequest(String url, String method, StringEntity messageBody,
protected InputStream sendRequest(String url, String method, StringEntity messageBody,
Map<String, String> headers, boolean tryAuth)
throws MessagingException {
if (url == null || method == null) {

View file

@ -12,6 +12,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;
import org.junit.Before;
import org.junit.Test;
@ -37,6 +38,7 @@ import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyMapOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@ -75,10 +77,16 @@ public class WebDavFolderTest {
private StatusLine mockStatusLine;
@Captor
private ArgumentCaptor<Map<String, String>> headerCaptor;
@Captor
private ArgumentCaptor<String> urlCaptor;
@Captor
private ArgumentCaptor<StringEntity> entityCaptor;
private WebDavFolder folder;
private WebDavFolder destinationFolder;
private String storeUrl = "https://localhost/webDavStoreUrl";
private String folderName = "testFolder";
private String moveOrCopyXml = "<xml>MoveOrCopyXml</xml>";
private HashMap<String, String> moveOrCopyHeaders;
private List<WebDavMessage> messages;
@ -86,10 +94,10 @@ public class WebDavFolderTest {
@Before
public void before() throws MessagingException, IOException {
MockitoAnnotations.initMocks(this);
when(mockStore.getUrl()).thenReturn("https://localhost/webDavStoreUrl");
when(mockStore.getUrl()).thenReturn(storeUrl);
when(mockStore.getHttpClient()).thenReturn(mockHttpClient);
when(mockStore.getStoreConfig()).thenReturn(mockStoreConfig);
folder = new WebDavFolder(mockStore, "testFolder");
folder = new WebDavFolder(mockStore, folderName);
setupTempDirectory();
}
@ -514,9 +522,6 @@ public class WebDavFolderTest {
@Test
public void appendWebDavMessages_replaces_messages_with_WebDAV_versions() throws MessagingException, IOException {
when(mockHttpClient.executeOverride(any(HttpUriRequest.class), any(HttpContext.class))).thenReturn(mockHttpResponse);
when(mockHttpResponse.getStatusLine()).thenReturn(mockStatusLine);
when(mockStatusLine.getStatusCode()).thenReturn(200);
List<Message> existingMessages = new ArrayList<>();
Message existingMessage = mock(Message.class);
existingMessages.add(existingMessage);
@ -529,4 +534,20 @@ public class WebDavFolderTest {
assertEquals(WebDavMessage.class, response.get(0).getClass());
assertEquals(messageUid, response.get(0).getUid());
}
@Test
public void appendWebDavMessages_sendsRequestUsingStore() throws MessagingException, IOException {
List<Message> existingMessages = new ArrayList<>();
Message existingMessage = mock(Message.class);
existingMessages.add(existingMessage);
String messageUid = "testMessageUid";
when(existingMessage.getUid()).thenReturn(messageUid);
folder.appendWebDavMessages(existingMessages);
verify(mockStore).sendRequest(urlCaptor.capture(), eq("PUT"), entityCaptor.capture(),
Matchers.<Map<String, String>>eq(null), eq(true));
assertTrue(urlCaptor.getValue().startsWith(storeUrl + "/" + folderName + "/" + messageUid));
assertTrue(urlCaptor.getValue().endsWith(".eml"));
}
}