Repository: thundernest/k-9
Issue: Webdav (Exchange) sending email is broken since v5.109 (alpha)

Android version: 6.01

Using K-9 Mail v5.010 (stable) i could send emails using my old WEBDAV account.
But never delete emails on the server.
Then Updated to K-9 Mail v5.109 (alpha) / and also to K-9 Mail v5.110 (alpha).
Now I can delete emails on the server, but I can not send emails any more.
They end up on the outgoing folder.
Sorry I can't tell you the error message. The message text is too long so it's truncated...

Then Downgraded (titanium Backup / also the database) and sending emails is working again.


Changed files:
k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java, k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavStore.java, k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java

diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java
index 7eda5d5b3..0f7d8da1b 100644
--- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java
+++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java
@@ -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);
diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavStore.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavStore.java
index 3132904b3..44ed5fa04 100644
--- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavStore.java
+++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavStore.java
@@ -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) {
diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java
index 69b18552a..e9a6d1a25 100644
--- a/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java
+++ b/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java
@@ -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"));
+    }
 }