Ejemplo n.º 1
0
        private void InitializeServer()
        {
            var baseUrl = MockServer.Uri.TrimEnd(new[] { '/' });
            var builder = new MockResponseBuilder(baseUrl);

            CurrentSource = builder.GetV2Source();
            V2Source      = builder.GetV2Source();
            V3Source      = builder.GetV3Source();

            AddPackageEndpoints(builder, PackageIdentityA, () => CurrentPackageAPath, () => IsPackageAAvailable);
            AddPackageEndpoints(builder, PackageIdentityB, () => PackageBPath, () => IsPackageBAvailable);

            // Add the V3 service index.
            MockServer.Get.Add(
                builder.GetV3IndexPath(),
                request =>
            {
                return(new Action <HttpListenerResponse>(response =>
                {
                    var mockResponse = builder.BuildV3IndexResponse(MockServer.Uri);

                    response.ContentType = mockResponse.ContentType;
                    MockServer.SetResponseContent(response, mockResponse.Content);
                }));
            });

            // Add the V2 "service index".
            var v2IndexPath = builder.GetV2IndexPath();

            MockServer.Get.Add(
                v2IndexPath,
                request =>
            {
                return(new Action <HttpListenerResponse>(response =>
                {
                    if (!request.RawUrl.EndsWith(v2IndexPath))
                    {
                        response.StatusCode = 404;
                        return;
                    }

                    var mockResponse = builder.BuildV2IndexResponse();

                    response.ContentType = mockResponse.ContentType;
                    MockServer.SetResponseContent(response, mockResponse.Content);
                }));
            });
        }
Ejemplo n.º 2
0
        private void AddPackageEndpoints(MockResponseBuilder builder, PackageIdentity identity, Func <string> getPackagePath, Func <bool> isAvailable)
        {
            // Add the /nuget/Packages(Id='',Version='') endpoint.
            MockServer.Get.Add(
                builder.GetODataPath(identity),
                request =>
            {
                return(new Action <HttpListenerResponse>(response =>
                {
                    if (!isAvailable())
                    {
                        response.StatusCode = 404;
                        return;
                    }

                    var packagePath = getPackagePath();
                    var mockResponse = builder.BuildODataResponse(packagePath);

                    response.ContentType = mockResponse.ContentType;
                    MockServer.SetResponseContent(response, mockResponse.Content);
                }));
            });

            // Add the /nuget/FindPackagesById()?id=''&semVerLevel=2.0.0 endpoint.
            MockServer.Get.Add(
                builder.GetFindPackagesByIdPath(identity.Id),
                request =>
            {
                return(new Action <HttpListenerResponse>(response =>
                {
                    var packagePaths = new List <string>();

                    if (isAvailable())
                    {
                        packagePaths.Add(getPackagePath());
                    }

                    var mockResponse = builder.BuildFindPackagesByIdResponse(packagePaths);

                    response.ContentType = mockResponse.ContentType;
                    MockServer.SetResponseContent(response, mockResponse.Content);
                }));
            });

            // Add the registration index.
            MockServer.Get.Add(
                builder.GetRegistrationIndexPath(identity.Id),
                request =>
            {
                return(new Action <HttpListenerResponse>(response =>
                {
                    if (!isAvailable())
                    {
                        response.StatusCode = 404;
                        return;
                    }

                    var mockResponse = builder.BuildRegistrationIndexResponse(MockServer.Uri, new PackageIdentity[] { identity });

                    response.ContentType = mockResponse.ContentType;
                    MockServer.SetResponseContent(response, mockResponse.Content);
                }));
            });

            // Add the flat index
            MockServer.Get.Add(
                builder.GetFlatIndexPath(identity.Id),
                request =>
            {
                return(new Action <HttpListenerResponse>(response =>
                {
                    if (!isAvailable())
                    {
                        response.StatusCode = 404;
                        return;
                    }

                    var mockResponse = builder.BuildFlatIndex(identity.Version);

                    response.ContentType = mockResponse.ContentType;
                    MockServer.SetResponseContent(response, mockResponse.Content);
                }));
            });

            // Add the .nupkg download.
            Func <HttpListenerRequest, object> downloadAction = request =>
            {
                return(new Action <HttpListenerResponse>(response =>
                {
                    if (!isAvailable())
                    {
                        response.StatusCode = 404;
                        return;
                    }

                    var packagePath = getPackagePath();
                    var mockResponse = builder.BuildDownloadResponse(packagePath);

                    response.ContentType = mockResponse.ContentType;
                    MockServer.SetResponseContent(response, mockResponse.Content);
                }));
            };

            MockServer.Get.Add(builder.GetFlatDownloadPath(identity), downloadAction);
            MockServer.Get.Add(builder.GetDownloadPath(identity), downloadAction);
        }