Commit 7133e97d authored by Jason Williams's avatar Jason Williams
Browse files

Did the refactor of AppDeployment to take advantage of new Post capabilities.

Post can take a Stream, so we can simplify a bit of the ifdef'd code here.
parent 5191612a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ using static Microsoft.Tools.WindowsDevicePortal.DevicePortal;
namespace Microsoft.Tools.WindowsDevicePortal.Tests
{
    /// <summary>
    /// Test class for OsInformation APIs
    /// Test class for AppFileExplorer APIs
    /// </summary>
    [TestClass]
    public class AppFileExplorerTests : BaseTests
+63 −139
Original line number Diff line number Diff line
@@ -118,24 +118,6 @@ namespace Microsoft.Tools.WindowsDevicePortal
                    out uri,
                    out boundaryString);

                // Create the request
#if WINDOWS_UWP
                HttpBaseProtocolFilter requestSettings = new HttpBaseProtocolFilter();
                requestSettings.AllowUI = false;
                requestSettings.ServerCredential = new PasswordCredential();
                requestSettings.ServerCredential.UserName = this.deviceConnection.Credentials.UserName;
                requestSettings.ServerCredential.Password = this.deviceConnection.Credentials.Password;
#else
                WebRequestHandler requestSettings = new WebRequestHandler();
                requestSettings.UseDefaultCredentials = false;
                requestSettings.Credentials = this.deviceConnection.Credentials;
                requestSettings.ServerCertificateValidationCallback = this.ServerCertificateValidation;
#endif // WINDOWS_UWP   
                                                             
                using (HttpClient client = new HttpClient(requestSettings))
                {
                    this.ApplyHttpHeaders(client, "POST");

                using (MemoryStream dataStream = new MemoryStream())
                {
                    byte[] data;
@@ -148,7 +130,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                        installPhaseDescription);
                    data = Encoding.ASCII.GetBytes(string.Format("--{0}\r\n", boundaryString));
                    dataStream.Write(data, 0, data.Length);
                        this.CopyInstallationFileToStream(packageFile, dataStream);
                    CopyFileToRequestStream(packageFile, dataStream);

                    // Copy dependency files, if any.
                    foreach (string dependencyFile in dependencyFileNames)
@@ -161,7 +143,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                            installPhaseDescription);
                        data = Encoding.ASCII.GetBytes(string.Format("\r\n--{0}\r\n", boundaryString));
                        dataStream.Write(data, 0, data.Length);
                            this.CopyInstallationFileToStream(fi, dataStream);
                        CopyFileToRequestStream(fi, dataStream);
                    }

                    // Copy the certificate file, if provided.
@@ -175,7 +157,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                            installPhaseDescription);
                        data = Encoding.ASCII.GetBytes(string.Format("\r\n--{0}\r\n", boundaryString));
                        dataStream.Write(data, 0, data.Length);
                            this.CopyInstallationFileToStream(fi, dataStream);
                        CopyFileToRequestStream(fi, dataStream);
                    }

                    // Close the installation request data.
@@ -183,41 +165,11 @@ namespace Microsoft.Tools.WindowsDevicePortal
                    dataStream.Write(data, 0, data.Length);

                    dataStream.Position = 0;
                        string contentTypeHeaderName = "Content-Type";
                        string contentType = string.Format("multipart/form-data; boundary={0}", boundaryString);

#if WINDOWS_UWP
                        using (HttpStreamContent content = new HttpStreamContent(dataStream.AsInputStream()))
                        {                        
                            content.Headers.Remove(contentTypeHeaderName);
                            content.Headers.TryAppendWithoutValidation(contentTypeHeaderName, contentType);

                            IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> responseOperation = client.PostAsync(uri, null);
                            System.Runtime.CompilerServices.TaskAwaiter<HttpResponseMessage> responseAwaiter = responseOperation.GetAwaiter();
                            while (!responseAwaiter.IsCompleted)
                            { 
                            }

                            using (HttpResponseMessage response = responseOperation.GetResults())
                            {
#else
                        using (StreamContent content = new StreamContent(dataStream))
                        {
                            content.Headers.Remove(contentTypeHeaderName);
                            content.Headers.TryAddWithoutValidation(contentTypeHeaderName, contentType);
                    string contentType = string.Format("multipart/form-data; boundary={0}", boundaryString);

                            using (HttpResponseMessage response = await client.PostAsync(uri, content))
                            {
#endif // WINDOWS_UWP
                                if (response.StatusCode != HttpStatusCode.Accepted)
                                {
                                    throw new DevicePortalException(
                                        response.StatusCode,
                                        response.StatusCode.ToString(),
                                        uri,
                                        "Failed to upload installation package");
                                }
                            }
                    // Make the HTTP request.
                    await this.Post(uri, dataStream, contentType);
                }

                // Poll the status until complete.
@@ -242,8 +194,6 @@ namespace Microsoft.Tools.WindowsDevicePortal
                    ApplicationInstallPhase.Idle,
                    installPhaseDescription);
            }
                }
            }
            catch (Exception e)
            {
                DevicePortalException dpe = e as DevicePortalException;
@@ -276,32 +226,6 @@ namespace Microsoft.Tools.WindowsDevicePortal
                string.Format("package={0}", packageName));
        }

        /// <summary>
        /// Copies a file to the specified stream and prepends the necessary content information
        /// required to be part of a multipart form data request.
        /// </summary>
        /// <param name="file">The file to be copied.</param>
        /// <param name="stream">The stream to which the file will be copied.</param>
        private void CopyInstallationFileToStream(
            FileInfo file,
            Stream stream)
        {
            byte[] data;
            string contentDisposition = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n", file.Name, file.Name);
            string contentType = "Content-Type: application/octet-stream\r\n\r\n";

            data = Encoding.ASCII.GetBytes(contentDisposition);
            stream.Write(data, 0, data.Length);

            data = Encoding.ASCII.GetBytes(contentType);
            stream.Write(data, 0, data.Length);

            using (FileStream fs = File.OpenRead(file.FullName))
            {
                fs.CopyTo(stream);
            }
        }

        /// <summary>
        /// Builds the application installation Uri and generates a unique boundary string for the multipart form data.
        /// </summary>