public async Task InvokeAsync(string code)
        {
            var requestActualContent       = EmbeddedResourceManager.GetString($"Assets.{code}_req.json");
            var responseExpectedContent    = EmbeddedResourceManager.GetString($"Assets.{code}_res.json");
            var responseExpectedStatusCode = !string.IsNullOrEmpty(responseExpectedContent) ? StatusCodes.Status200OK : StatusCodes.Status204NoContent;

            var serviceProviderMock = new Mock <IServiceProvider>(MockBehavior.Strict);

            serviceProviderMock.Setup(o => o.GetService(typeof(JsonRpcTestHandler1)))
            .Returns(null);
            serviceProviderMock.Setup(o => o.GetService(typeof(IOptions <JsonRpcOptions>)))
            .Returns(null);
            serviceProviderMock.Setup(o => o.GetService(typeof(ILoggerFactory)))
            .Returns(null);

            var environmentMock = new Mock <IWebHostEnvironment>(MockBehavior.Strict);

            environmentMock
            .Setup(o => o.EnvironmentName)
            .Returns(Environments.Production);

            var loggerMock = new Mock <ILogger <JsonRpcMiddleware <JsonRpcTestHandler1> > >(MockBehavior.Loose);

            loggerMock
            .Setup(o => o.IsEnabled(It.IsAny <LogLevel>()))
            .Returns(true);

            var jsonRpcMiddleware = new JsonRpcMiddleware <JsonRpcTestHandler1>(serviceProviderMock.Object, environmentMock.Object, loggerMock.Object);
            var httpContext       = new DefaultHttpContext();

            httpContext.Request.Method      = HttpMethods.Post;
            httpContext.Request.ContentType = "application/json; charset=utf-8";
            httpContext.Request.Headers.Add(HeaderNames.Accept, "application/json; charset=utf-8");
            httpContext.Request.Body  = new MemoryStream(Encoding.UTF8.GetBytes(requestActualContent));
            httpContext.Response.Body = new MemoryStream();

            await jsonRpcMiddleware.InvokeAsync(httpContext, c => Task.CompletedTask);

            Assert.AreEqual(responseExpectedStatusCode, httpContext.Response.StatusCode);

            if (responseExpectedStatusCode == StatusCodes.Status200OK)
            {
                var responseActualContent = default(string);

                httpContext.Response.Body.Position = 0;

                using (var reader = new StreamReader(httpContext.Response.Body))
                {
                    responseActualContent = await reader.ReadToEndAsync();
                }

                Assert.IsFalse(string.IsNullOrEmpty(responseActualContent), "Actual response content is empty");

                var responseActualContentToken   = JToken.Parse(responseActualContent);
                var responseExpectedContentToken = JToken.Parse(responseExpectedContent);

                Assert.IsTrue(JToken.DeepEquals(responseExpectedContentToken, responseActualContentToken), "Actual JSON token differs from expected");
            }
        }
        public async Task InvokeAsyncWhenHttpMethodIsInvalid(string method)
        {
            var serviceProviderMock = new Mock <IServiceProvider>(MockBehavior.Strict);

            serviceProviderMock.Setup(o => o.GetService(typeof(JsonRpcTestHandler1)))
            .Returns(null);
            serviceProviderMock.Setup(o => o.GetService(typeof(IOptions <JsonRpcOptions>)))
            .Returns(null);
            serviceProviderMock.Setup(o => o.GetService(typeof(ILoggerFactory)))
            .Returns(null);

            var environmentMock   = new Mock <IWebHostEnvironment>(MockBehavior.Strict);
            var loggerMock        = new Mock <ILogger <JsonRpcMiddleware <JsonRpcTestHandler1> > >(MockBehavior.Loose);
            var jsonRpcMiddleware = new JsonRpcMiddleware <JsonRpcTestHandler1>(serviceProviderMock.Object, environmentMock.Object, loggerMock.Object);
            var httpContext       = new DefaultHttpContext();

            httpContext.Request.Method      = method;
            httpContext.Request.ContentType = "application/json; charset=utf-8";

            await jsonRpcMiddleware.InvokeAsync(httpContext, c => Task.CompletedTask);

            Assert.AreEqual(StatusCodes.Status405MethodNotAllowed, httpContext.Response.StatusCode);
        }
        public void Dispose()
        {
            var serviceProviderMock    = new Mock <IServiceProvider>(MockBehavior.Strict);
            var environmentMock        = new Mock <IWebHostEnvironment>(MockBehavior.Strict);
            var loggerMock             = new Mock <ILogger <JsonRpcMiddleware <JsonRpcTestHandler1> > >(MockBehavior.Loose);
            var jsonRpcHandler         = new JsonRpcTestHandler1();
            var jsonRpcHandlerDisposed = false;

            jsonRpcHandler.Disposed += (sender, e) => jsonRpcHandlerDisposed = true;

            serviceProviderMock.Setup(o => o.GetService(typeof(JsonRpcTestHandler1)))
            .Returns(jsonRpcHandler);
            serviceProviderMock.Setup(o => o.GetService(typeof(IOptions <JsonRpcOptions>)))
            .Returns(null);
            serviceProviderMock.Setup(o => o.GetService(typeof(ILoggerFactory)))
            .Returns(null);

            var jsonRpcMiddleware = new JsonRpcMiddleware <JsonRpcTestHandler1>(serviceProviderMock.Object, environmentMock.Object, loggerMock.Object);

            jsonRpcMiddleware.Dispose();

            Assert.IsTrue(jsonRpcHandlerDisposed);
        }
        public async Task InvokeAsyncWhenContentTypeHeaderIsInvalid(string mediaType)
        {
            var serviceProviderMock = new Mock <IServiceProvider>(MockBehavior.Strict);

            serviceProviderMock.Setup(o => o.GetService(typeof(JsonRpcTestHandler1)))
            .Returns(null);
            serviceProviderMock.Setup(o => o.GetService(typeof(IOptions <JsonRpcOptions>)))
            .Returns(null);
            serviceProviderMock.Setup(o => o.GetService(typeof(ILoggerFactory)))
            .Returns(null);

            var environmentMock   = new Mock <IWebHostEnvironment>(MockBehavior.Strict);
            var loggerMock        = new Mock <ILogger <JsonRpcMiddleware <JsonRpcTestHandler1> > >(MockBehavior.Loose);
            var jsonRpcMiddleware = new JsonRpcMiddleware <JsonRpcTestHandler1>(serviceProviderMock.Object, environmentMock.Object, loggerMock.Object);
            var httpContext       = new DefaultHttpContext();

            httpContext.Request.Method      = HttpMethods.Post;
            httpContext.Request.ContentType = mediaType;

            await jsonRpcMiddleware.InvokeAsync(httpContext, c => Task.CompletedTask);

            Assert.AreEqual(StatusCodes.Status415UnsupportedMediaType, httpContext.Response.StatusCode);
        }