Beispiel #1
0
        public async Task Consumer_Should_Create_A_Pack()
        {
            var options = new PactDefinitionOptions
            {
                IgnoreContractValues = true,
                IgnoreCasing         = true
            };


            await PactMaker
            .Create(options)
            .Between("frontend", "bicycles")
            .WithHttpInteraction(bc =>
                                 bc.Given("There is a request for some id")
                                 .UponReceiving("A GET request to get bicycle")
                                 .With(request => request
                                       .WithMethod(HttpMethod.Get)
                                       .WithPath("bicycles/1"))
                                 .WillRespondWith(response => response
                                                  .WithHeader("Content-Type", "application/json")
                                                  .WithStatusCode(HttpStatusCode.OK)
                                                  .WithBody <BicycleResponseModel>()))
            .PublishedViaHttp("http://localhost/pacts/provider/bicycles/consumer/frontend/version/1.0.0", HttpMethod.Put)
            .MakeAsync();
        }
Beispiel #2
0
        public async Task Consumer_Should_Create_APact()
        {
            var options = new PactDefinitionOptions
            {
                IgnoreContractValues = true,
                IgnoreCasing = true
            };

            await PactMaker
                .Create(options)
                .Between("orders", "parcels")
                .WithHttpInteraction(cb => cb
                    .Given("There is a parcel with some id")
                    .UponReceiving("A GET Request to retrieve the parcel")
                    .With( request => request
                        .WithMethod(HttpMethod.Get)
                        .WithPath("api/parcels/{Id}"))
                    .WillRespondWith(response => response
                        .WithStatusCode(HttpStatusCode.OK)
                        .WithBody<ParcelReadModel>()))
                .PublishedViaHttp("http://localhost:9292/pacts/provider/parcels/consumer/orders/version/1.2.104", HttpMethod.Put)
                .MakeAsync();
        }
Beispiel #3
0
        public async Task Given_Valid_Parcel_Id_Parcel_Should_Be_Returned()
        {
            var options = new PactDefinitionOptions
            {
                IgnoreCasing         = true,
                IgnoreContractValues = true
            };

            await PactMaker
            .Create(options)
            .Between("orders", "parcels")
            .WithHttpInteraction(b => b
                                 .Given("Existing parcel")
                                 .UponReceiving("A GET request to retrieve parcel details")
                                 .With(request => request
                                       .WithMethod(HttpMethod.Get)
                                       .WithPath($"/parcels/{ParcelId}"))
                                 .WillRespondWith(response => response
                                                  .WithHeader("Content-Type", "application/json")
                                                  .WithStatusCode(HttpStatusCode.OK)
                                                  .WithBody <ParcelDto>()))
            .PublishedAsFile("../../../../../../pacts")
            .MakeAsync();
        }
Beispiel #4
0
        private static void VerifyBody(PactDefinitionOptions options, object expectedBody,
                                       IDictionary <string, object> providedBody, List <string> errors)
        {
            foreach (var pair in (IDictionary <string, object>)expectedBody)
            {
                var stringComparision = options.IgnoreCasing
                    ? StringComparison.InvariantCultureIgnoreCase
                    : StringComparison.InvariantCulture;
                var propertyName  = pair.Key;
                var propertyValue = pair.Value;

                var providedProperty = providedBody.FirstOrDefault(p => p.Key.Equals(propertyName, stringComparision)).Value;

                if (providedProperty is null)
                {
                    var message = GetErrorMessage(ErrorMessages.MissingResponseBodyProperty, propertyName);
                    errors.Add(message);
                }
                else
                {
                    if (options.IgnoreContractValues)
                    {
                        continue;
                    }

                    var propertyHasExpectedValue = providedProperty.Equals(propertyValue);

                    if (!propertyHasExpectedValue)
                    {
                        var message = GetErrorMessage(ErrorMessages.IncorrectReposnseBodyPropertyValue,
                                                      propertyValue, providedProperty);
                        errors.Add(message);
                    }
                }
            }
        }
Beispiel #5
0
        public async Task <PactVerificationResult> VerifyAsync(HttpInteractionDefinition definition, PactDefinitionOptions options,
                                                               object templateObject = null)
        {
            var getResult   = GetHttpMethod(definition.Request.Method);
            var requestPath = templateObject is null
                ? definition.Request.Path
                : Smart.Format(definition.Request.Path, templateObject);
            var httpResponse = await getResult(requestPath);

            var json = await httpResponse.Content.ReadAsStringAsync();

            var providedBody = JsonConvert.DeserializeObject <ExpandoObject>(json);
            var expectedBody = definition.Response.Body;
            var errors       = new List <string>();

            VerifyStatusCode(definition, httpResponse, errors);
            VerifyHeaders(definition, httpResponse, errors);

            if (providedBody is null || expectedBody is null)
            {
                return(new PactVerificationResult(errors));
            }

            VerifyBody(options, expectedBody, providedBody, errors);
            return(new PactVerificationResult(errors));
        }