Ejemplo n.º 1
0
        public async Task Response_DescriptiveProperties_SetCorrectly()
        {
            // Arrange:
            var hostPlugin = new MockAppHostPlugin();

            hostPlugin.AddPluginType <LinkedResourceMap>();

            var mockResource = new LinkedResource
            {
                Id     = 10,
                Value1 = 100,
                Value2 = "value-2",
                Value3 = 300,
                Value4 = 400
            };

            var serviceMock = new MockUnitTestService
            {
                ServerResources = new[] { mockResource }
            };

            // Act:
            var client = RequestSettings.Create()
                         .CreateTestClient(hostPlugin, serviceMock);

            var request = ApiRequest.Create("api/linked/resource", HttpMethod.Get);

            var resource = await client.Send <LinkedResourceModel>(request);

            resource.MediaType.Should().Be("application/hal+json");
            resource.CharSet.Should().Be("utf-8");
            resource.ContentLength.Should().Be(1108);
        }
Ejemplo n.º 2
0
        public async Task QueryStringParams_CanBeSpecified()
        {
            var settings = RequestSettings.Create(config =>
            {
                config.UseHalDefaults();

                config.QueryString
                .AddParam("a", "v1")
                .AddParam("b", "v2");
            });

            IMockedService mockedSrv = new MockUnitTestService
            {
                Customers = new[] { new CustomerResource {
                                        CustomerId = "ID_1", Age = 47
                                    } }
            };

            var hostPlugin = new MockAppHostPlugin();

            hostPlugin.AddPluginType <CustomerResourceMap>();

            var client = settings.CreateTestClient(hostPlugin, mockedSrv);

            var request  = ApiRequest.Create("api/customers/24234234234", HttpMethod.Get);
            var response = await client.Send <CustomerModel>(request);

            response.Request.RequestUri.PathAndQuery
            .Should().Be("/api/customers/24234234234?a=v1&b=v2");
        }
Ejemplo n.º 3
0
        public async Task ResourceLinks_NotSerializedWithRequest()
        {
            var mockedSrv  = new MockUnitTestService();
            var hostPlugin = new MockAppHostPlugin();

            hostPlugin.AddPluginType <CustomerResourceMap>();

            var client = RequestSettings.Create()
                         .CreateTestClient(hostPlugin, mockedSrv);

            // Create a test resource as it would have been returned from the server.
            var resource = new CustomerModel {
                CustomerId = Guid.NewGuid().ToString(),
                FirstName  = "Doug",
                LastName   = "Bowan",
                Age        = 77,
                Links      = new Dictionary <string, Link> {
                    { "test:lj4000", new Link {
                          Href = "pull/rip/cord"
                      } }
                }
            };

            var request = ApiRequest.Post("api/customers/pass-through").WithContent(resource);

            await client.Send <CustomerModel>(request);

            var serverReceivedResource = mockedSrv.ServerReceivedResource as CustomerResource;

            serverReceivedResource.Should().NotBeNull("Client Resource Deserialized in Server Resource Representation.");
            serverReceivedResource.CustomerId.Should().Be(resource.CustomerId, "Server Side Serialized Resource Matches Client Resource.");
            serverReceivedResource.Links.Should().BeNull("Links Should only be returned from Server.");
        }
Ejemplo n.º 4
0
        public async 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()
            };

            var embeddedServerResource = new AddressResource
            {
                AddressId  = Guid.NewGuid().ToString(),
                CustomerId = serverResource.CustomerId
            };

            var embeddedServerResource2 = new AddressResource
            {
                AddressId  = Guid.NewGuid().ToString(),
                CustomerId = serverResource.CustomerId
            };

            serverResource.Embed(new[] { embeddedServerResource, embeddedServerResource2 }, "addresses");

            var mockSrv = new MockUnitTestService
            {
                Customers = new CustomerResource[] { serverResource }
            };

            // Create the test client and call route returning an embedded resource collection.
            var hostPlugin = new MockAppHostPlugin();

            hostPlugin.AddPluginType <CustomerResourceMap>();

            var client = RequestSettings.Create()
                         .CreateTestClient(hostPlugin, mockSrv);

            var request  = ApiRequest.Create("api/customers/embedded/resource", HttpMethod.Get);
            var response = await client.Send <CustomerModel>(request);

            // Validate that an embedded resource collection was returned.
            response.Content.Should().NotBeNull();
            response.Content.Embedded.Should().NotBeNull();
            response.Content.Embedded.Keys.Should().HaveCount(1);
            response.Content.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 = response.Content.GetEmbeddedCollection <AddressModel>("addresses");

            embeddedClientResource.Should().NotBeNull();
            embeddedClientResource.Should().HaveCount(2);
        }
Ejemplo n.º 5
0
        public async Task ClientSpecified_EnbeddedTypes_SentAsQueryString()
        {
            // 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()
            };

            var embeddedServerResource = new AddressResource
            {
                AddressId  = Guid.NewGuid().ToString(),
                CustomerId = serverResource.CustomerId
            };

            var embeddedServerResource2 = new AddressResource
            {
                AddressId  = Guid.NewGuid().ToString(),
                CustomerId = serverResource.CustomerId
            };

            var embeddedServerResource3 = new AddressResource
            {
                AddressId  = Guid.NewGuid().ToString(),
                CustomerId = serverResource.CustomerId
            };

            serverResource.Embed(new[] { embeddedServerResource, embeddedServerResource2 }, "addresses");
            serverResource.Embed(embeddedServerResource3, "vacation-address");

            var mockSrv = new MockUnitTestService
            {
                Customers = new CustomerResource[] { serverResource }
            };

            // Create the test client and call route returning an embedded resource collection.
            var hostPlugin = new MockAppHostPlugin();

            hostPlugin.AddPluginType <CustomerResourceMap>();

            var client = RequestSettings.Create()
                         .CreateTestClient(hostPlugin, mockSrv);

            var request  = ApiRequest.Create("api/customers/embedded/resource", HttpMethod.Get).Embed("vacation-address");
            var response = await client.Send <CustomerModel>(request);

            response.Request.RequestUri.Query.Should().Equals("?embed=vacation-address");

            // If supported by the service then only one embedded resource should have been returned.
            response.Content.Embedded.Should().HaveCount(1);
            response.Content.Embedded.ContainsKey("vacation-address").Should().BeTrue();
        }
Ejemplo n.º 6
0
        public async Task EmbeddedResources_NotSerializedWithRequest()
        {
            var settings = RequestSettings.Create(config => config.UseHalDefaults());

            var hostPlugin = new MockAppHostPlugin();

            hostPlugin.AddPluginType <CustomerResourceMap>();

            var mockedSrv = new MockUnitTestService();
            var client    = settings.CreateTestClient(hostPlugin, mockedSrv);

            // Create a test resource as it would have been returned from the server.
            var resource = new CustomerModel
            {
                CustomerId = Guid.NewGuid().ToString(),
                FirstName  = "Doug",
                LastName   = "Bowan",
                Age        = 77,
            };

            // Create an embedded resource.  This would be the same state as if
            // the embedded resource, returned from the server, was deserialized into
            // the client-side resource representation.
            var embeddedResource = new AddressModel {
                AddressId  = Guid.NewGuid().ToString(),
                CustomerId = resource.CustomerId,
                City       = "Buckhannon",
                Street     = "59 College Avenue",
                ZipCode    = "26201"
            };

            resource.Embedded = new Dictionary <string, object>();
            resource.Embedded.Add("address", embeddedResource);

            var request = ApiRequest.Post("api/customers/pass-through", config => config.WithContent(resource));

            await client.Send <CustomerModel>(request);

            var serverReceivedResource = mockedSrv.ServerReceivedResource as CustomerResource;

            serverReceivedResource.Should().NotBeNull("Client Resource Deserialized in Server Resource Representation.");
            serverReceivedResource.CustomerId.Should().Be(resource.CustomerId, "Server Side Serialized Resource Matches Client Resource.");
            serverReceivedResource.Embedded.Should().BeNull("Embedded Resources Should only be returned from Server.");
        }
Ejemplo n.º 7
0
        public async Task IfErrorStatusCode_SuccessStaus_SetCorrectly()
        {
            // Arrange:
            var hostPlugin = new MockAppHostPlugin();

            var serviceMock = new MockUnitTestService
            {
                ReturnsStatusCode = StatusCodes.Status404NotFound
            };

            // Act:
            var client = RequestSettings.Create()
                         .CreateTestClient(hostPlugin, serviceMock);

            var request = ApiRequest.Create("api/error/server/http/error-code", HttpMethod.Get);

            var response = await client.Send(request);

            response.StatusCode.Should().Be(HttpStatusCode.NotFound);
            response.ReasonPhase.Should().Be("Not Found");
        }
Ejemplo n.º 8
0
        public async Task IfServerException_ExceptionRaised()
        {
            // Arrange:
            var hostPlugin = new MockAppHostPlugin();

            var serviceMock = new MockUnitTestService
            {
                TriggerServerSideException = true
            };

            // Act:
            var client = RequestSettings.Create()
                         .CreateTestClient(hostPlugin, serviceMock);

            var request = ApiRequest.Create("api/error/server/side", HttpMethod.Get);

            var exception = await Record.ExceptionAsync(() => client.Send(request));

            exception.Should().NotBeNull();
            exception.Should().BeOfType <InvalidOperationException>();
            exception.Message.Should().Be("Test-Exception");
        }