public async Task Can_write_json_to_stream() { ////Arrange var service = new CitylineServer(new[] { new SampleProvider() }) { UseJson = true }; var stream = new MemoryStream(); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(1000); ////Act await service.WriteStream(stream, new CitylineRequest(), null, cancellationTokenSource.Token); ////Assert stream.Position = 0; string json; using (var reader = new StreamReader(stream)) { json = await reader.ReadLineAsync(); } var parsed = JsonConvert.DeserializeObject <ParseHelper>(json); Assert.AreEqual("sample", parsed.Event); }
public async Task Can_write_to_stream() { ////Arrange var service = new CitylineServer(new[] { new SampleProvider() }); var stream = new MemoryStream(); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(1000); ////Act await service.WriteStream(stream, new CitylineRequest(), null, cancellationTokenSource.Token); ////Assert stream.Position = 0; string eventName; string eventData; string eventTicket; using (var reader = new StreamReader(stream)) { eventTicket = await reader.ReadLineAsync(); eventName = await reader.ReadLineAsync(); eventData = await reader.ReadLineAsync(); } Assert.AreEqual("event: sample", eventName); }
public async Task StartStream(CitylineRequest request, CancellationToken cancellationToken = default(CancellationToken)) { var context = new CustomContext { RequestUrl = new Uri(Request.GetEncodedUrl()), User = User, SampleHeader = Request.Headers["sample"] }; Response.Headers.Add("content-type", "text/event-stream"); await _citylineServer.WriteStream(Response.Body, request, context, cancellationToken); }
public async Task Can_write_large_json_to_stream() { ////Arrange var sampleObject = new { Test = "dimmer", Numbers = Enumerable.Range(0, 1000).ToDictionary(m => $"row-{m}", m => $"value-{m}") }; var service = new CitylineServer(new[] { new SampleProvider { sampleObject = sampleObject } }) { UseJson = true }; var stream = new MemoryStream(); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(1000); ////Act await service.WriteStream(stream, new CitylineRequest(), null, cancellationTokenSource.Token); ////Assert stream.Position = 0; string json; using (var reader = new StreamReader(stream)) { json = await reader.ReadLineAsync(); } var parsed = JsonConvert.DeserializeObject <ParseHelper>(json); var parsedData = JsonConvert.DeserializeObject <ParseHelper2>(parsed.Data); Assert.AreEqual("value-999", parsedData.Numbers["row-999"]); }