コード例 #1
0
        public void GivenAPartialContract_AndAttemptToStartAnother_ItThrowsException()
        {
            var mockProvider = new MockProviderService(port);

            mockProvider.Given("Test1");

            Assert.Throws <Exception>(() => mockProvider.Given("Test2"));

            mockProvider.UponReceiving("A Request");

            Assert.Throws <Exception>(() => mockProvider.Given("A Request 2"));
        }
コード例 #2
0
ファイル: Tests.cs プロジェクト: ardz/PactTesting
        public void GetThing_WhenTheThingExists_ReturnTheThing()
        {
            MockProviderService.Given("There is a thing")
            .UponReceiving("A GET request to retrieve the thing")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/api/values/",
                Headers = new Dictionary <string, object>
                {
                    { "Accept", "application/json" }
                }
                //Query = ""
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body =
                    new     //NOTE: Note the case sensitivity here, the body will be serialised as per the casing defined
                {
                    value1 = "value1",
                    value2 = "value2"
                }
            });     //NOTE: WillRespondWith call must come last as it will register the interaction

            var response = ApiTestClient.MakeRequest(HttpMethod.Get, "http://localhost:9222/api/values");

            var content = ApiTestClient.HttpResponseToString(response);

            Assert.IsNotNull(content);
        }
コード例 #3
0
        public async Task GetEtaForNextTrip_ReturnsExpectedTripInformation()
        {
            // /eta/routeName/direction/stopName

            // Arrange

            string routeName = "20";
            string direction = "Northbound";
            string stopName  = "Opera";

            var busId = 99999;
            var eta   = 5;

            var busIdRegex = "^[1-9]{1,5}$"; // All positive integers between 1 and 99999
            var etaIdRegex = "^[0-9]{0,2}$"; // All positive integers between 0 and 99

            var expectedBusInfo = new
            {
                busID = Match.Type(busId),
                eta   = Match.Type(eta),
            };

            MockProviderService
            .Given($"There are buses scheduled for route {routeName} and direction {direction} to arrive at stop {stopName}") // Describe the state the provider needs to setup
            .UponReceiving($"A request for eta for route {routeName} to {stopName} station")                                  // textual description - business case
            .With(new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path   = Uri.EscapeUriString($"/eta/{routeName}/{direction}/{stopName}"),
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = expectedBusInfo
            });

            var consumer = new BusServiceClient(MockServerBaseUri);

            // Act
            var result = await consumer.GetBusInfo(routeName, direction, stopName);

            // Assert
            Assert.That(result.BusID.ToString(), Does.Match(busIdRegex));
            Assert.That(result.Eta.ToString(), Does.Match(etaIdRegex));

            // NOTE: Verifies that interactions registered on the mock provider are called at least once
            MockProviderService.VerifyInteractions();
        }
コード例 #4
0
        public void GivenAContractWithoutRequest_AndAttemptToComplete_ItThrowsException()
        {
            var mockProvider = new MockProviderService(port);

            mockProvider.Given("A Test");


            Assert.Throws <Exception>(() =>
                                      mockProvider.WillRespondWith(new ContractResponse()
            {
                StatusCode = HttpStatusCode.Accepted
            }));
        }
コード例 #5
0
        public async Task GivenAContract_AndItsRequested_TheProviderReturnsTheResponse()
        {
            var mockProvider = new MockProviderService(port);

            mockProvider.Given("A Test").UponReceiving("A Scenario").With(new ContractRequest()
            {
                Url = "/1234", Method = "GET"
            }).WillRespondWith(new ContractResponse()
            {
                StatusCode = HttpStatusCode.OK
            });

            HttpClient client   = new HttpClient();
            var        response = await client.GetAsync($"http://localhost:{port}/1234");

            Assert.That(response.StatusCode == HttpStatusCode.OK);
        }
コード例 #6
0
 public void AddInteraction(ProviderServiceInteraction PSI)
 {
     MockProviderService
     .Given(PSI.ProviderState)
     .UponReceiving(PSI.Description)
     .With(new ProviderServiceRequest
     {
         Method  = PSI.Request.Method,
         Path    = PSI.Request.Path,
         Headers = PSI.Request.Headers,
         Body    = PSI.Request.Body
     })
     .WillRespondWith(new ProviderServiceResponse
     {
         Status  = PSI.Response.Status,
         Headers = PSI.Response.Headers,
         Body    = PSI.Response.Body
     });
 }
コード例 #7
0
        public void GivenAContractToBuild_ItIsInTheStore()
        {
            var mockProvider = new MockProviderService(port);

            mockProvider.Given("A Test").UponReceiving("A Request").With(new ContractRequest()
            {
                Method = "Get",
                Url    = "/1234"
            }).WillRespondWith(new ContractResponse()
            {
                StatusCode = HttpStatusCode.OK
            });
            var contractFromStore = mockProvider.GetContracts().FirstOrDefault(c => c.Name == "A Test");

            Assert.NotNull(contractFromStore);
            Assert.AreEqual("A Test", contractFromStore.Name);
            Assert.AreEqual("A Request", contractFromStore.Scenario);
            Assert.AreEqual("Get", contractFromStore.Request.Method);
            Assert.AreEqual("/1234", contractFromStore.Request.Url);
            Assert.AreEqual(HttpStatusCode.OK, contractFromStore.Response.StatusCode);
        }
コード例 #8
0
ファイル: ProductTests.cs プロジェクト: m4cx/dwx2019-pact
        public async Task GetProduct_ReturnsExpectedProduct()
        {
            var guidRegex         = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";
            var expectedProductId = Guid.Parse("E0C2E684-D83F-45C3-A6E5-D62A6F83A0BD");
            var expectedName      = "Test";
            var expectedProduct   = new
            {
                id          = Match.Regex(expectedProductId.ToString(), $"^{guidRegex}$"),
                name        = Match.Type(expectedName),
                description = Match.Type("A product for testing"),
            };

            MockProviderService
            .Given("A Product with expected structure")          // Describe the state the provider needs to setup
            .UponReceiving("a GET request for a single product") // textual description - business case
            .With(new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path   = Match.Regex($"/{expectedProductId}", $"^\\/{guidRegex}$"),
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", "application/json; charset=utf-8" }
                },
                Body = expectedProduct
            });

            var consumer = new ProductClient(MockServerBaseUri);
            var result   = await consumer.Get(expectedProductId);

            Assert.AreEqual(expectedProductId, result.Id);
            Assert.AreEqual(expectedName, result.Name);

            MockProviderService.VerifyInteractions();
        }