public async Task Test_GetRequestWrapper_StoresSingleRequest() { var notification = new UntypedNotification() { Method = "test" }; var request = new SingleRequestWrapper() { Call = notification, }; var itemsMock = new Mock <IDictionary <object, object> >(); itemsMock.Setup(x => x.ContainsKey(JsonRpcConstants.RequestItemKey)) .Returns(false); itemsMock.SetupSet(x => x[JsonRpcConstants.RequestItemKey] = It.IsAny <IUntypedCall>()); var httpContextMock = new Mock <HttpContext>(); httpContextMock.SetupGet(x => x.Items) .Returns(itemsMock.Object); requestReaderMock.Setup(x => x.ParseRequest(It.IsAny <HttpContext>(), It.IsAny <Encoding>())) .ReturnsAsync(request); var result = await requestReaderMock.Object.GetRequestWrapper(httpContextMock.Object, Encoding.UTF8); result.Should().BeOfType <SingleRequestWrapper>(); result.Should().Be(request); var requestResult = result as SingleRequestWrapper; requestResult.Call.Should().Be(notification); itemsMock.VerifySet(x => x[JsonRpcConstants.RequestItemKey] = It.IsAny <IUntypedCall>()); requestReaderMock.Verify(x => x.ParseRequest(It.IsAny <HttpContext>(), It.IsAny <Encoding>())); requestReaderMock.Verify(x => x.GetRequestWrapper(It.IsAny <HttpContext>(), It.IsAny <Encoding>())); }
public void Test_Constructor_ThrowsOnNullValue() { var notification = new UntypedNotification(); Action action = () => new JsonServerResponseWrapper(null, notification, null); action.Should().Throw <ArgumentNullException>(); }
public async Task Test_Write_CopiesHeaders() { var responseHeaders = new HeaderDictionary() { { "a", "b" } }; var responseMock = new Mock <HttpResponse>(); responseMock.SetupGet(x => x.Headers) .Returns(responseHeaders); var httpContextMock = new Mock <HttpContext>(); httpContextMock.SetupGet(x => x.Response) .Returns(responseMock.Object); var next = Mock.Of <RequestDelegate>(); var handlingContext = new HandlingContext(httpContextMock.Object, Encoding.UTF8, next) { WriteResponse = false }; var notification = new UntypedNotification(); var value = new JObject(); var headers = new HeaderDictionary { { "header", "value" } }; var wrapper = new JsonServerResponseWrapper(value, notification, headers); await wrapper.Write(handlingContext, new HeaderJsonRpcSerializer()); responseMock.VerifyGet(x => x.Headers); responseHeaders.Should().HaveCount(2); responseHeaders.Should().ContainKey("header"); }
public async Task Test_Write_ChecksCancellationToken() { var ms = new MemoryStream(); var responseMock = new Mock <HttpResponse>(); responseMock.SetupGet(x => x.Body) .Returns(ms); var httpContextMock = new Mock <HttpContext>(); httpContextMock.SetupGet(x => x.Response) .Returns(responseMock.Object); httpContextMock.SetupGet(x => x.RequestAborted) .Returns(new CancellationToken(true)); var next = Mock.Of <RequestDelegate>(); var handlingContext = new HandlingContext(httpContextMock.Object, Encoding.UTF8, next) { WriteResponse = true }; var notification = new UntypedNotification(); var value = new JObject(); var wrapper = new JsonServerResponseWrapper(value, notification, null); Func <Task> action = async() => await wrapper.Write(handlingContext, new HeaderJsonRpcSerializer()); await action.Should().ThrowAsync <TaskCanceledException>(); }
public void Test_SetId_IgnoresIfNotification() { var notification = new UntypedNotification() { }; var value = new JObject(); new JsonServerResponseWrapper(value, notification, null); value.Should().NotContainKey(JsonRpcConstants.IdProperty); }
public async Task Test_Write_SetsResponseProperties() { var responseMock = new Mock <HttpResponse>(); var httpContextMock = new Mock <HttpContext>(); httpContextMock.SetupGet(x => x.Response) .Returns(responseMock.Object); var next = Mock.Of <RequestDelegate>(); var handlingContext = new HandlingContext(httpContextMock.Object, Encoding.UTF8, next) { WriteResponse = false }; var notification = new UntypedNotification(); var value = new JObject(); var wrapper = new JsonServerResponseWrapper(value, notification, null); await wrapper.Write(handlingContext, new HeaderJsonRpcSerializer()); responseMock.VerifySet(x => x.StatusCode = 200); responseMock.VerifySet(x => x.ContentLength = null); responseMock.VerifySet(x => x.ContentType = It.IsAny <string>()); }
public async Task Test_Write_SetsResponseErrorCodeOnError() { var items = new Dictionary <object, object>(); var ms = new MemoryStream(); var responseMock = new Mock <HttpResponse>(); responseMock.SetupGet(x => x.Body) .Returns(ms); var httpContextMock = new Mock <HttpContext>(); httpContextMock.SetupGet(x => x.Response) .Returns(responseMock.Object); httpContextMock.SetupGet(x => x.RequestAborted) .Returns(new CancellationToken()); httpContextMock.SetupGet(x => x.Items) .Returns(items); var next = Mock.Of <RequestDelegate>(); var handlingContext = new HandlingContext(httpContextMock.Object, Encoding.UTF8, next) { WriteResponse = true }; var notification = new UntypedNotification(); var value = new JObject() { { "error", new JObject() { { "code", new JValue(1) } } } }; var wrapper = new JsonServerResponseWrapper(value, notification, null); await wrapper.Write(handlingContext, new HeaderJsonRpcSerializer()); var result = httpContextMock.Object.Items[JsonRpcConstants.ResponseErrorCodeItemKey] as string; result.Should().Be("1"); }
public void Test_UntypedNotification_HasDefaultVersion() { var value = new UntypedNotification(); value.Jsonrpc.Should().Be(JsonRpcConstants.Version); }