public void HeartbeatRespondsAsExpected()
        {
            // arrange
            var expectedSuccessMessage = "API is responding";

            _mockProviderService
            .UponReceiving("A heartbeat request")
            .With(new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path   = "/api/heartbeat"
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Body    = Match.Regex("API is responding", "^API is responding$"),
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "text/plain; charset=utf-8" }
                }
            });

            // act
            var result = ConsumerApiClient.Heartbeat(_mockProviderServiceBaseUri).GetAwaiter().GetResult();

            // assert
            Assert.Equal(200, (int)result.StatusCode);
        }
        public void WhenRegisteringTheSameInteractionTwiceInATest_ThenPactFailureExceptionIsThrown()
        {
            var description = "A POST request to create a new thing";
            var request     = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/things",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    thingId = 1234,
                    type    = "Awesome"
                }
            };

            var response = new ProviderServiceResponse
            {
                Status = 201
            };

            _mockProviderService
            .UponReceiving(description)
            .With(request)
            .WillRespondWith(response);

            _mockProviderService
            .UponReceiving(description)
            .With(request);

            Assert.Throws <PactFailureException>(() => _mockProviderService.WillRespondWith(response));
        }
        public void WhenRegisteringAnInteractionThatIsNeverSent_ThenPactFailureExceptionIsThrown()
        {
            _mockProviderService
            .UponReceiving("A POST request to create a new thing")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/things",
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    thingId = 1234,
                    type    = "Awesome"
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status = 201
            });

            Assert.Throws <PactFailureException>(() => _mockProviderService.VerifyInteractions());
        }
        public async Task WhenCryptocurrencyIsNotSupportedReturnsNotFound()
        {
            const string cryptocurrency    = "unknownCryptocurrency";
            var          endpointUnderTest = $"/api/priceindex/{cryptocurrency}";

            _mockProviderService
            .UponReceiving($"A GET request to get price indexes for '{cryptocurrency}' cryptocurrency")
            .With(new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path   = endpointUnderTest
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status = (int)HttpStatusCode.NotFound
            });

            var consumer = new TestWebClient(_mockProviderServiceBaseUri);

            var(statusCode, coinDeskResponse) = await consumer.Get(endpointUnderTest);

            Assert.Equal(HttpStatusCode.NotFound, statusCode);
            Assert.Null(coinDeskResponse);

            _mockProviderService.VerifyInteractions();
        }
Beispiel #5
0
        public void CreateEvent_WhenCalledWithEvent_Succeeds()
        {
            //Arrange
            var carId = Guid.Parse("1F587704-2DCC-4313-A233-7B62B4B469DB");

            _mockProviderService.UponReceiving("a request to create a new car")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/cars",
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    carId,
                    mileage = 0,
                    fuel    = "Gasoline"
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status = 201
            });

            var consumer = new CarsApiClient(_mockProviderServiceBaseUri);

            //Act / Assert
            consumer.CreateCar(carId);

            _mockProviderService.VerifyInteractions();
        }
Beispiel #6
0
        public void ItGetsAValue()
        {
            // Arange
            var stringConsumerNeeds = "Hello from provider!";

            _mockProviderService
            .UponReceiving("A invalid GET request for Date Validation with invalid date parameter")
            .With(new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path   = "/api/provider",
            })
            .WillRespondWith(new ProviderServiceResponse {
                Status  = 200,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    message = stringConsumerNeeds
                }
            });

            // Act
            var result         = ConsumerApiClient.ValidateDateTimeUsingProviderApi(_mockProviderServiceBaseUri).GetAwaiter().GetResult();
            var resultBodyText = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();

            // Assert
            Assert.Contains(stringConsumerNeeds, resultBodyText);
        }
Beispiel #7
0
 public void Visit(IMockProviderService mockProvider)
 {
     mockProvider
     .UponReceiving("a GET request to retrieve the forecasts")
     .With(new ProviderServiceRequest
     {
         Method  = HttpVerb.Get,
         Path    = "/weatherforecast",
         Headers = new Dictionary <string, object> {
             { "Accept", "application/json" }
         }
     })
     .WillRespondWith(new ProviderServiceResponse
     {
         Status  = 200,
         Headers = new Dictionary <string, object> {
             { "Content-Type", "application/json; charset=utf-8" }
         },
         Body = Match.MinType(
             new
         {
             date         = Match.Type(_exampleForecast.Date),
             summary      = Match.Type(_exampleForecast.Summary),
             temperatureC = Match.Type(_exampleForecast.TemperatureC)
         },
             1)
     });
 }
        public void Visit(IMockProviderService mockProvider)
        {
            var pathMatch = Matchers.Url($"/weatherforecast/1", "/weatherforecast/{number}");

            mockProvider
            .UponReceiving("a POST request to set a forecast")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = pathMatch,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    date         = Match.Type(_exampleForecast.Date),
                    summary      = Match.Type(_exampleForecast.Summary),
                    temperatureC = Match.Type(_exampleForecast.TemperatureC),
                    temperatureF = Match.Type(_exampleForecast.TemperatureF)
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status = 200
            });
        }
        public void WhenApiIsUp_ReturnsTrue()
        {
            //Arrange
            _mockProviderService.UponReceiving("a request to check the api status")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Headers = new Dictionary <string, object> {
                    { "Accept", "application/json" }
                },
                Path = "/echo/status"
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object> {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    up      = true,
                    upSince = DateTime.UtcNow,
                    version = "2.0.0",
                    message = "I'm up and running from last 19 hours."
                }
            });

            var consumer = new ProductApiClient(_serviceBaseUri);

            //Act
            var result = consumer.ApiStatus().Up;

            //Assert
            Assert.True(result);

            _mockProviderService.VerifyInteractions();
        }
        public void CreateEvent_WhenCalledWithEvent_Succeeds()
        {
            //Arrange
            var eventId  = Guid.Parse("1F587704-2DCC-4313-A233-7B62B4B469DB");
            var dateTime = new DateTime(2011, 07, 01, 01, 41, 03);

            DateTimeFactory.Now = () => dateTime;

            _mockProviderService.UponReceiving("a request to create a new event")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    eventId,
                    timestamp = dateTime.ToString("O"),
                    eventType = "DetailsView"
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status = 201
            });

            var consumer = new EventsApiClient(_mockProviderServiceBaseUri);

            //Act / Assert
            consumer.CreateEvent(eventId);

            _mockProviderService.VerifyInteractions();
        }
Beispiel #11
0
        public async Task Test1()
        {
            _mockProviderService
            .UponReceiving("Receber a previsão do tempo para amanhã")
            .With(new ProviderServiceRequest
            {
                Path    = "/WeatherForecast/tomorrow",
                Method  = HttpVerb.Get,
                Headers = new Dictionary <string, object>
                {
                    { "Accept", "application/json" }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = new
                {
                    date         = DateTime.Today.AddDays(1),
                    summary      = "Chilly",
                    temperatureC = 15,
                    temperatureF = 58
                }
            });

            var consumer = new WeatherForecastApiClient(_mockProviderServiceBaseUri);

            // Act
            var result = await consumer.GetForecastTomorrow();

            // Assert
            result.Should().BeEquivalentTo(
                new WeatherForecastResponse
            {
                Date         = DateTime.Today.AddDays(1),
                Summary      = "Chilly",
                TemperatureC = 15,
                TemperatureF = 58
            }
                );

            _mockProviderService.VerifyInteractions();
        }
 public IMockProviderService UponReceiving(string description)
 {
     return(_decoratedMockProvider.UponReceiving(description));
 }