public void When_resource_has_default_constructor_it_must_succeed() { // Arrange var options = new JsonApiOptions(); IResourceGraph graph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add <ResourceWithoutConstructor>().Build(); var serviceContainer = new ServiceContainer(); serviceContainer.AddService(typeof(IResourceDefinitionAccessor), new NeverResourceDefinitionAccessor()); var serializer = new RequestDeserializer(graph, new ResourceFactory(serviceContainer), new TargetedFields(), _mockHttpContextAccessor.Object, _requestMock.Object, options); var body = new { data = new { id = "1", type = "resourceWithoutConstructors" } }; string content = JsonConvert.SerializeObject(body); // Act object result = serializer.Deserialize(content); // Assert Assert.NotNull(result); Assert.Equal(typeof(ResourceWithoutConstructor), result.GetType()); }
public void When_resource_has_constructor_with_string_parameter_it_must_fail() { // Arrange var options = new JsonApiOptions(); IResourceGraph graph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add <ResourceWithStringConstructor>().Build(); var serviceContainer = new ServiceContainer(); serviceContainer.AddService(typeof(IResourceDefinitionAccessor), new NeverResourceDefinitionAccessor()); var serializer = new RequestDeserializer(graph, new ResourceFactory(serviceContainer), new TargetedFields(), _mockHttpContextAccessor.Object, _requestMock.Object, options); var body = new { data = new { id = "1", type = "resourceWithStringConstructors" } }; string content = JsonConvert.SerializeObject(body); // Act Action action = () => serializer.Deserialize(content); // Assert var exception = Assert.Throws <InvalidOperationException>(action); Assert.Equal("Failed to create an instance of 'UnitTests.Models.ResourceWithStringConstructor' using injected constructor parameters.", exception.Message); }
public void When_resource_has_default_constructor_that_throws_it_must_fail() { // Arrange var graph = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance) .AddResource <ResourceWithThrowingConstructor>() .Build(); var serializer = new RequestDeserializer(graph, new DefaultResourceFactory(new ServiceContainer()), new TargetedFields()); var body = new { data = new { id = "1", type = "resourceWithThrowingConstructors" } }; string content = JsonConvert.SerializeObject(body); // Act Action action = () => serializer.Deserialize(content); // Assert var exception = Assert.Throws <InvalidOperationException>(action); Assert.Equal( "Failed to create an instance of 'UnitTests.Models.ResourceWithThrowingConstructor' using its default constructor.", exception.Message); }
public void When_resource_has_constructor_with_injectable_parameter_it_must_succeed() { // Arrange var graph = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance) .AddResource <ResourceWithDbContextConstructor>() .Build(); var appDbContext = new AppDbContext(new DbContextOptionsBuilder <AppDbContext>().Options, new FrozenSystemClock()); var serviceContainer = new ServiceContainer(); serviceContainer.AddService(typeof(AppDbContext), appDbContext); var serializer = new RequestDeserializer(graph, new DefaultResourceFactory(serviceContainer), new TargetedFields()); var body = new { data = new { id = "1", type = "resourceWithDbContextConstructors" } }; string content = JsonConvert.SerializeObject(body); // Act object result = serializer.Deserialize(content); // Assert Assert.NotNull(result); Assert.Equal(typeof(ResourceWithDbContextConstructor), result.GetType()); Assert.Equal(appDbContext, ((ResourceWithDbContextConstructor)result).AppDbContext); }
public void When_model_has_no_parameterless_contructor_it_must_fail() { // Arrange var graph = new ResourceGraphBuilder(new JsonApiOptions()).AddResource <ResourceWithParameters>().Build(); var serializer = new RequestDeserializer(graph, new TargetedFields()); var body = new { data = new { id = "1", type = "resourceWithParameters" } }; string content = Newtonsoft.Json.JsonConvert.SerializeObject(body); // Act Action action = () => serializer.Deserialize(content); // Assert var exception = Assert.Throws <InvalidOperationException>(action); Assert.Equal("Failed to create an instance of 'UnitTests.Models.ConstructionTests+ResourceWithParameters' using its default constructor.", exception.Message); }
public void When_resource_has_default_constructor_it_must_succeed() { // Arrange var graph = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance) .AddResource <ResourceWithoutConstructor>() .Build(); var serializer = new RequestDeserializer(graph, new DefaultResourceFactory(new ServiceContainer()), new TargetedFields()); var body = new { data = new { id = "1", type = "resourceWithoutConstructors" } }; string content = JsonConvert.SerializeObject(body); // Act object result = serializer.Deserialize(content); // Assert Assert.NotNull(result); Assert.Equal(typeof(ResourceWithoutConstructor), result.GetType()); }
private void ParseCommand(ref ReadOnlySequence <byte> buffer) { if (_clientManager.SupportAcknowledgement) { ReadAcknowledgement(ref buffer); } short cmdType = 0; if (_clientManager.ClientVersion >= 5000) { var cmdTypeBuffer = buffer.Slice(0, 2); if (cmdTypeBuffer.First.Span.Length >= 2) { cmdType = HelperFxn.ConvertToShort(cmdTypeBuffer.First.Span); } else { cmdType = HelperFxn.ConvertToShort(cmdTypeBuffer.ToArray()); } buffer = buffer.Slice(2, buffer.End); } var commandLengthBuffer = buffer.Slice(0, _headerLength); var cmdLength = RequestDeserializer.ToInt32(commandLengthBuffer.ToArray(), 0, _headerLength); buffer = buffer.Slice(_headerLength, buffer.End); var commandBuffer = buffer.Slice(0, cmdLength); object command = null; using (var stream = new MemoryStream(commandBuffer.ToArray())) command = RequestDeserializer.Deserialize(cmdType, stream); buffer = buffer.Slice(cmdLength, buffer.End); if (CommandHelper.IsBasicCRUDOperation((Common.Protobuf.Command.Type)cmdType)) { _cmdManager.ProcessCommand(_clientManager, command, cmdType, _acknowledgementId, null, buffer.Length > 0); } else { //Due to response pipelining, old responses of basic CRUD operations are queued for sending //therefore before executing this long running command, we should send those responses _clientManager.SendPendingResponses(true); ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessCommandAsync), new LongRunningCommand() { Command = command, CommandType = cmdType, AcknowledgementId = _acknowledgementId }); } _expectedLength = null; _acknowledgementId = -1; }
public void InternalDeserialize_Array_ReturnArray() { var request = new HttpRequest( "TargetTestPage.ashx", "http://local/Tests/TargetTestPage.ashx", "Users=1,2,3&SearchText=OK_TEXT"); var source = RequestDeserializer.InternalDeserialize <TestArrayParameters>(request.QueryString, CultureInfo.InvariantCulture); Assert.IsTrue((new[] { 1, 2, 3 }).SequenceEqual(source.Users)); Assert.AreEqual("OK_TEXT", source.SearchText); }
public void InternalDeserialize_TwoParameters_ReturnObjectInstantiatedWithParameters() { var request = new HttpRequest( "TargetTestPage.ashx", "http://local/Tests/TargetTestPage.ashx", "UserId=1&SearchText=OK_TEXT"); var source = RequestDeserializer.InternalDeserialize <TestParameters>(request.QueryString, CultureInfo.InvariantCulture); Assert.AreEqual(1, source.UserId); Assert.AreEqual("OK_TEXT", source.SearchText); }
private bool ReadHeader(ref ReadOnlySequence <byte> buffer) { if (buffer.Length >= _headerLength) { _expectedLength = RequestDeserializer.ToInt32(buffer.Slice(0, _headerLength).ToArray(), 0, _headerLength); buffer = buffer.Slice(_headerLength, buffer.End); return(true); } return(false); }
public RequestDeserializerTests() { _deserializer = new RequestDeserializer(_resourceGraph, new ResourceFactory(new ServiceContainer()), _fieldsManagerMock.Object, _mockHttpContextAccessor.Object); }
public RequestDeserializerTests() { _deserializer = new RequestDeserializer(_resourceGraph, _fieldsManagerMock.Object); }
public static ShippingApiResponse Request(string fullPath, ISession session) { List <ShippingApiMethod> methods = new List <ShippingApiMethod>() { new ShippingApiMethod() { Verb = HttpVerb.POST, UriRegex = "/shippingservices/v1/addresses/verify-suggest", RequestType = typeof(VerifySuggestResponse), RequestInterface = null, ResponseType = typeof(Address) }, new ShippingApiMethod() { Verb = HttpVerb.POST, UriRegex = "/shippingservices/v1/addresses/verify", RequestType = typeof(Address), RequestInterface = typeof(IAddress), ResponseType = typeof(Address) }, new ShippingApiMethod() { Verb = HttpVerb.GET, UriRegex = "/shippingservices/v1/manifests?originalTransactionId=(?<TransactionId>[^/]+)", RequestType = typeof(RetryManifestRequest), RequestInterface = null, ResponseType = typeof(Manifest) }, new ShippingApiMethod() { Verb = HttpVerb.POST, UriRegex = "/shippingservices/v1/manifests", RequestType = typeof(Manifest), RequestInterface = typeof(IManifest), ResponseType = typeof(Manifest) }, new ShippingApiMethod() { Verb = HttpVerb.GET, UriRegex = "/shippingservices/v1/manifests/(?<ManifestId>[^/]+)", RequestType = typeof(ReprintManifestRequest), RequestInterface = null, ResponseType = typeof(Manifest) }, new ShippingApiMethod() { Verb = HttpVerb.POST, UriRegex = "/shippingservices/v1/rates", RequestType = typeof(Shipment), RequestInterface = typeof(IShipment), ResponseType = typeof(Shipment) }, new ShippingApiMethod() { Verb = HttpVerb.POST, UriRegex = "/shippingservices/v1/shipments", RequestType = typeof(Shipment), RequestInterface = typeof(IShipment), ResponseType = typeof(Shipment) }, new ShippingApiMethod() { Verb = HttpVerb.DELETE, UriRegex = "/shippingservices/v1/shipments/(?<ShipmentId>[^/]+)", RequestType = typeof(CancelShipmentRequest), RequestInterface = null, ResponseType = typeof(CancelShipmentResponse) }, new ShippingApiMethod() { Verb = HttpVerb.GET, UriRegex = "/shippingservices/v1/shipments/(?<ShipmentId>[^/]+)", RequestType = typeof(ReprintShipmentRequest), RequestInterface = null, ResponseType = typeof(Shipment) }, }; if (File.Exists(fullPath)) { using (var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) using (var mimeStream = new MimeStream(fileStream)) { mimeStream.SeekNextPart(); //request mimeStream.ClearHeaders(); var request = RequestDeserializer.Request(mimeStream, methods, session); if (request.Method.Verb == HttpVerb.POST && request.Method.UriRegex == "/shippingservices/v1/shipments") { var shipment = (IShipment)request.Request; shipment.TransactionId = Guid.NewGuid().ToString().Substring(15); foreach (var option in shipment.ShipmentOptions) { if (option.ShipmentOption == ShipmentOption.SHIPPER_ID) { option.Value = session.GetConfigItem("ShipperID"); } } if (shipment.ShipmentGroupId != null && shipment.ShipmentGroupId != string.Empty) { shipment.ShipmentGroupId = "500002"; //TODO: get from config } if (shipment.IntegratorCarrierId != null && shipment.IntegratorCarrierId != string.Empty) { shipment.IntegratorCarrierId = "987654321"; //TODO: get from config } } session.LogDebug("Getting file"); return(request.Call(session)); } } return(null); }
public RequestDeserializerTests() { _deserializer = new RequestDeserializer(ResourceGraph, new TestResourceFactory(), _fieldsManagerMock.Object, MockHttpContextAccessor.Object, _requestMock.Object, new JsonApiOptions()); }
public RequestDeserializerTests() { _deserializer = new RequestDeserializer(_resourceGraph, new DefaultResourceFactory(new ServiceContainer()), _fieldsManagerMock.Object); }