Ejemplo n.º 1
0
        public Task QueryStringParams_CanBeSpecified()
        {
            return(WebHostFixture.TestAsync <LinkGenerationTests>(async host =>
            {
                IMockedService mockedSrv = new MockUnitTestService
                {
                    Customers = new[] { new CustomerResource {
                                            CustomerId = "ID_1", Age = 47
                                        } }
                };

                var response = await host
                               .ArrangeWithDefaults(mockedSrv)

                               .Act.OnRestClient(async client =>
                {
                    var request = ApiRequest.Create("api/customers/24234234234", HttpMethod.Get,
                                                    config =>
                    {
                        config.Settings.UseHalDefaults();
                        config.Settings.QueryString
                        .AddParam("a", "v1")
                        .AddParam("b", "v2");
                    });

                    return await client.SendAsync <CustomerModel>(request);
                });

                response.Assert.ApiResponse(apiResponse =>
                {
                    apiResponse.Request.RequestUri.PathAndQuery
                    .Should().Be("/api/customers/24234234234?a=v1&b=v2");
                });
            }));
        }
Ejemplo n.º 2
0
        public Task ClientCan_ReceiveEmbeddedResourceCollection()
        {
            // Arrange the test resources that will be returned from the server
            // to test the client consumer code.
            var serverResource = new CustomerResource
            {
                CustomerId = Guid.NewGuid().ToString()
            };

            // Embed to child resources.
            serverResource.Embed(new[] {
                new AddressResource {
                    AddressId = Guid.NewGuid().ToString(), CustomerId = serverResource.CustomerId
                },
                new AddressResource {
                    AddressId = Guid.NewGuid().ToString(), CustomerId = serverResource.CustomerId
                }
            }, "addresses");

            // Configure the mock service to return the resource.
            var mockSrv = new MockUnitTestService
            {
                Customers = new[] { serverResource }
            };

            // Run the unit test and request the resource and assert the expected results.
            return(WebHostFixture.TestAsync <LinkGenerationTests>(async host =>
            {
                var response = await host
                               .ArrangeWithDefaults(mockSrv)

                               .Act.OnRestClient(async client =>
                {
                    var request = ApiRequest.Get("api/customers/embedded/resource");
                    return await client.SendAsync <CustomerModel>(request);
                });

                response.Assert.ApiResponse(apiResponse =>
                {
                    var resource = (CustomerModel)apiResponse.Content;

                    // Validate that an embedded resource collection was returned.
                    resource.Should().NotBeNull();
                    resource.Embedded.Should().NotBeNull();
                    resource.Embedded.Keys.Should().HaveCount(1);
                    resource.Embedded.ContainsKey("addresses").Should().BeTrue();

                    // At this point, the embedded resource is the generic JSON.NET representation.
                    // The next line of code will deserialize this generic representation in the C# client side class
                    //      matching the server-sided resource collection.

                    var embeddedClientResource = resource.GetEmbeddedCollection <AddressModel>("addresses").ToArray();
                    embeddedClientResource.Should().NotBeNull();
                    embeddedClientResource.Should().HaveCount(2);
                });
            }));
        }