NuGetGallery.FunctionalTests.ODataHelper.TryDownloadPackageFromFeed C# (CSharp) Method

TryDownloadPackageFromFeed() public method

public TryDownloadPackageFromFeed ( string packageId, string version ) : Task
packageId string
version string
return Task
        public async Task<string> TryDownloadPackageFromFeed(string packageId, string version)
        {
            try
            {
                var handler = new HttpClientHandler();
                handler.AllowAutoRedirect = false;
                using (var client = new HttpClient(handler))
                {
                    string requestUri = UrlHelper.V2FeedRootUrl + @"Package/" + packageId + @"/" + version;
                    var response = await client.GetAsync(requestUri).ConfigureAwait(false);

                    //print the header
                    WriteLine("HTTP status code : {0}", response.StatusCode);
                    WriteLine("HTTP header : {0}", response.Headers);
                    if (response.StatusCode == HttpStatusCode.Found)
                    {
                        return response.Headers.GetValues("Location").FirstOrDefault();
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (HttpRequestException hre)
            {
                WriteLine("Exception : {0}", hre.Message);
                return null;
            }
        }

Usage Example

コード例 #1
0
ファイル: LoadTests.cs プロジェクト: atrevisan/NuGetGallery
 public async Task DownloadPackageSimulationTest()
 {
     string packageId = "EntityFramework"; //try to down load a pre-defined package.
     string version = "5.0.0";
     //Just try download and not actual download. Since this will be used in load test, we don't to actually download the nupkg everytime.
     var odataHelper = new ODataHelper();
     string redirectUrl = await odataHelper.TryDownloadPackageFromFeed(packageId, version);
     Assert.IsNotNull(redirectUrl, " Package download from V2 feed didnt work");
     string expectedSubString = "packages/entityframework.5.0.0.nupkg";
     Assert.IsTrue(redirectUrl.Contains(expectedSubString), " The re-direct Url {0} doesnt contain the expect substring {1}", redirectUrl, expectedSubString);
 }