private async Task <object> ReadEnumerable(Type type, Stream stream) { var elementType = TypeUtils.GetEnumerableItemType(type); var supportedElementType = true; if (elementType != null) { supportedElementType = TypeUtils.IsSupportedElementType(elementType); } if (elementType != null && supportedElementType) { var isNull = await ReadNullFlag(stream).ConfigureAwait(false); if (!isNull) { var result = TypeUtils.CreateList(elementType); var count = (int) await ReadPrimitive(typeof(int), stream).ConfigureAwait(false); if (count > 0) { TraceUtils.WriteLineFormatted("Begin reading enumerable"); for (var i = 0; i < count; i++) { var item = await ReadPlainObject(elementType, stream).ConfigureAwait(false); result.Add(item); TraceUtils.WriteLineFormatted("Read #{0}/{1} of enumerable", i + 1, count); } TraceUtils.WriteLineFormatted("End reading enumerable"); } else { TraceUtils.WriteLineFormatted("Enumerable is empty"); } if (!type.IsArray) { return(result); } return(ConvertionUtils.ConvertListToArray(result)); } } else if (elementType != null) { TraceUtils.WriteLineFormatted("Unable to read Enumerable of type \"{0}\": Unsupported element type \"{1}\"", type, elementType); } else { TraceUtils.WriteLineFormatted("Unable to read Enumerable of type \"{0}\": Unsupported", type); } return(null); }
private async Task <object> ReadPrimitive(Type type, Stream stream) { var bytesToRead = Marshal.SizeOf(type); await ReadStream(stream, TemporaryBuffer, bytesToRead).ConfigureAwait(false); return(ConvertionUtils.GetValue(type, TemporaryBuffer)); }
public async Task WithNullCarCallPutRetunsBadRequest() { // Arrange CarModel carModel = null; var car = ConvertionUtils.GetJson(carModel); HttpContent content = new StringContent(car, Encoding.UTF8, "application/json"); var expected = HttpStatusCode.BadRequest; // Act var httpResponse = await Client.PutAsync(Path + $"cars/", content); var actual = httpResponse.StatusCode; // Assert Assert.Equal(expected, actual); }
public async Task WithIdTwoCallGetRetunsCarWithIdTwo() { // Arrange var id = 2; var expected = id; // Act var httpResponse = await Client.GetAsync(Path + $"cars/{id}"); httpResponse.EnsureSuccessStatusCode(); var response = await httpResponse.Content.ReadAsStringAsync(); var actual = ConvertionUtils.GetCarModel(response).CarId; // Assert Assert.Equal(expected, actual); }
private async Task WriteString(string value, Stream stream) { var stringLength = value.Length; await WriteNullFlag(false, stream).ConfigureAwait(false); if (stringLength > 0) { var stringBytes = ConvertionUtils.GetStringBytes(value); await WritePrimitive(stringBytes.Length, stream).ConfigureAwait(false); await WriteBytes(stringBytes, stringBytes.Length, stream).ConfigureAwait(false); } else { await WritePrimitive(0, stream).ConfigureAwait(false); } }
public async Task WithValidCarCallPutRetunsOK() { // Arrange var id = 1; CarModel car = new CarModel(new Car() { Id = id, Make = "BMW", Model = "3.18", Colour = "Red", Year = 2001 }); HttpContent content = new StringContent(ConvertionUtils.GetJson(car), Encoding.UTF8, "application/json"); var expected = HttpStatusCode.OK; // Act var httpResponse = await Client.PutAsync(Path + $"cars/", content); var actual = httpResponse.StatusCode; // Assert Assert.Equal(expected, actual); }
private async Task <object> ReadString(Stream stream) { var isNull = await ReadNullFlag(stream).ConfigureAwait(false); if (!isNull) { var length = (int) await ReadPrimitive(typeof(int), stream).ConfigureAwait(false); if (length > 0) { var bytes = new byte[length]; await ReadStream(stream, bytes, length).ConfigureAwait(false); return(ConvertionUtils.GetString(bytes)); } return(string.Empty); } return(null); }
public async Task WithValidCarCallPostRetunsNewCar() { // Arrange var id = 5; CarModel car = new CarModel(new Car() { Make = "VW", Model = "Polo", Colour = "White", Year = 2005 }); var carJson = ConvertionUtils.GetJson(car); HttpContent content = new StringContent(carJson, Encoding.UTF8, "application/json"); car.CarId = id; var expected = ConvertionUtils.GetJson(car); // Act var httpResponse = await Client.PostAsync(Path + $"cars/", content); httpResponse.EnsureSuccessStatusCode(); var actual = await httpResponse.Content.ReadAsStringAsync(); // Assert Assert.Equal(expected, actual); }
private async Task WriteEnumerable(object value, Type type, Stream stream) { var elementType = TypeUtils.GetEnumerableItemType(type); var supportedElementType = true; if (elementType != null) { supportedElementType = TypeUtils.IsSupportedElementType(elementType); } if (elementType != null && supportedElementType) { var enumerable = (IEnumerable)value; // ReSharper disable PossibleMultipleEnumeration var supportedEnumerable = enumerable.Cast <object>().All(item => item == null || item.GetType() == elementType); if (supportedEnumerable) { TraceUtils.WriteLineFormatted("Begin writing enumerable"); await WriteNullFlag(false, stream).ConfigureAwait(false); using (var internalStream = new MemoryStream()) { var count = 0; foreach (var item in enumerable) { await WritePlainObject(item, elementType, internalStream).ConfigureAwait(false); ++count; } await WriteBytes(TemporaryBuffer, ConvertionUtils.Convert(count, TemporaryBuffer), stream).ConfigureAwait(false); if (count > 0) { internalStream.Seek(0, SeekOrigin.Begin); await internalStream.CopyToAsync(stream).ConfigureAwait(false); } } // ReSharper restore PossibleMultipleEnumeration TraceUtils.WriteLineFormatted("End writing enumerable"); } else { await WriteNullFlag(true, stream).ConfigureAwait(false); TraceUtils.WriteLineFormatted("Unable to write Enumerable of type \"{0}\": All elements must be of one type", type); } } else if (elementType != null) { TraceUtils.WriteLineFormatted("Unable to write Enumerable of type \"{0}\": Unsupported element type \"{1}\"", type, elementType); } else { TraceUtils.WriteLineFormatted("Unable to write Enumerable of type \"{0}\": Unsupported", type); } }
private Task WritePrimitive(object value, Stream stream) { var affected = ConvertionUtils.Convert(value, TemporaryBuffer); return(WriteBytes(TemporaryBuffer, affected, stream)); }