示例#1
0
        public void Test_SetResult_SetsSuccess()
        {
            var jToken             = JValue.CreateString("value");
            var resultMock         = new Mock <SuccessParseResult>(MockBehavior.Strict, jToken, "test");
            var bindingContextMock = new Mock <ModelBindingContext>(MockBehavior.Strict);
            var serializerMock     = new Mock <IJsonRpcSerializer>(MockBehavior.Strict);

            serializerMock.SetupGet(x => x.Serializer).Returns(Mock.Of <JsonSerializer>(MockBehavior.Strict));
            var rpcContext = new JsonRpcBindingContext()
            {
                Serializer = serializerMock.Object
            };
            var binderMock = new Mock <ParameterBinder>(testEnvironment.ServiceProvider.GetRequiredService <ILogger <ParameterBinder> >());

            binderMock.Setup(x => x.SetResultSafe(It.IsAny <ModelBindingContext>(), It.IsAny <string>(), It.IsAny <SuccessParseResult>(), It.IsAny <JsonSerializer>()))
            .Returns((Task)null);
            binderMock.Setup(x => x.SetResult(It.IsAny <ModelBindingContext>(), It.IsAny <IParseResult>(), It.IsAny <string>(), It.IsAny <JsonRpcBindingContext>()))
            .CallBase();

            binderMock.Object.SetResult(bindingContextMock.Object, resultMock.Object, "test", rpcContext);

            binderMock.Verify(x => x.SetResult(It.IsAny <ModelBindingContext>(), It.IsAny <IParseResult>(), It.IsAny <string>(), It.IsAny <JsonRpcBindingContext>()));
            binderMock.Verify(x => x.SetResultSafe(It.IsAny <ModelBindingContext>(), It.IsAny <string>(), It.IsAny <SuccessParseResult>(), It.IsAny <JsonSerializer>()));
            binderMock.VerifyNoOtherCalls();
            serializerMock.Verify();
            bindingContextMock.Verify();
        }
示例#2
0
        public void Test_SetResult_ThrowsOnNull()
        {
            var    bindingContextMock = new Mock <ModelBindingContext>(MockBehavior.Strict);
            var    rpcContext         = new JsonRpcBindingContext();
            Action action             = () => parameterBinder.SetResult(bindingContextMock.Object, null, "test", rpcContext);

            action.Should().Throw <ArgumentOutOfRangeException>();

            bindingContextMock.Verify();
        }
示例#3
0
        public void Test_SetResult_SetsError()
        {
            var resultMock         = new Mock <ErrorParseResult>(MockBehavior.Strict, "test", "test");
            var bindingContextMock = new Mock <ModelBindingContext>(MockBehavior.Strict);
            var rpcContext         = new JsonRpcBindingContext();
            var binderMock         = new Mock <ParameterBinder>(testEnvironment.ServiceProvider.GetRequiredService <ILogger <ParameterBinder> >());

            binderMock.Setup(x => x.SetError(It.IsAny <ModelBindingContext>(), It.IsAny <string>(), It.IsAny <IParseResult>()))
            .Returns((Task)null);
            binderMock.Setup(x => x.SetResult(It.IsAny <ModelBindingContext>(), It.IsAny <IParseResult>(), It.IsAny <string>(), It.IsAny <JsonRpcBindingContext>()))
            .CallBase();

            binderMock.Object.SetResult(bindingContextMock.Object, resultMock.Object, "test", rpcContext);

            binderMock.Verify(x => x.SetResult(It.IsAny <ModelBindingContext>(), It.IsAny <IParseResult>(), It.IsAny <string>(), It.IsAny <JsonRpcBindingContext>()));
            binderMock.Verify(x => x.SetError(It.IsAny <ModelBindingContext>(), It.IsAny <string>(), It.IsAny <IParseResult>()));
            binderMock.VerifyNoOtherCalls();
            bindingContextMock.Verify();
        }
示例#4
0
        public void Test_SetResult_SetsNoResultWhenOptional()
        {
            var resultMock         = new Mock <NoParseResult>(MockBehavior.Strict, "test");
            var bindingContextMock = new Mock <ModelBindingContext>(MockBehavior.Strict);
            var rpcContext         = new JsonRpcBindingContext
            {
                ParameterMetadata = new ParameterMetadata(new JsonName("test", "test"), typeof(object), 0, BindingStyle.Default, true)
            };
            var binderMock = new Mock <ParameterBinder>(testEnvironment.ServiceProvider.GetRequiredService <ILogger <ParameterBinder> >());

            binderMock.Setup(x => x.SetNoResult(It.IsAny <ModelBindingContext>(), It.IsAny <string>(), It.IsAny <NoParseResult>()))
            .Returns((Task)null);
            binderMock.Setup(x => x.SetResult(It.IsAny <ModelBindingContext>(), It.IsAny <IParseResult>(), It.IsAny <string>(), It.IsAny <JsonRpcBindingContext>()))
            .CallBase();

            binderMock.Object.SetResult(bindingContextMock.Object, resultMock.Object, "test", rpcContext);

            binderMock.Verify(x => x.SetResult(It.IsAny <ModelBindingContext>(), It.IsAny <IParseResult>(), It.IsAny <string>(), It.IsAny <JsonRpcBindingContext>()));
            binderMock.Verify(x => x.SetNoResult(It.IsAny <ModelBindingContext>(), It.IsAny <string>(), It.IsAny <NoParseResult>()));
            binderMock.VerifyNoOtherCalls();
            bindingContextMock.Verify();
        }
示例#5
0
        public void Test_SetResult_SetsErrorWhenNullNotAllowed()
        {
            var resultMock            = new Mock <NullParseResult>(MockBehavior.Strict, "test");
            var modelMetadataIdentity = ModelMetadataIdentity.ForProperty(typeof(int), "null_name", typeof(object));
            var modelMetadataMock     = new Mock <ModelMetadata>(MockBehavior.Strict, modelMetadataIdentity);
            var bindingContextMock    = new Mock <ModelBindingContext>(MockBehavior.Strict);

            bindingContextMock.SetupGet(x => x.ModelMetadata).Returns(modelMetadataMock.Object);
            var rpcContext = new JsonRpcBindingContext();
            var binderMock = new Mock <ParameterBinder>(testEnvironment.ServiceProvider.GetRequiredService <ILogger <ParameterBinder> >());

            binderMock.Setup(x => x.SetError(It.IsAny <ModelBindingContext>(), It.IsAny <string>(), It.IsAny <IParseResult>()))
            .Returns((Task)null)
            .Verifiable();
            binderMock.Setup(x => x.SetResult(It.IsAny <ModelBindingContext>(), It.IsAny <IParseResult>(), It.IsAny <string>(), It.IsAny <JsonRpcBindingContext>()))
            .CallBase();

            binderMock.Object.SetResult(bindingContextMock.Object, resultMock.Object, "test", rpcContext);

            binderMock.Verify(x => x.SetResult(It.IsAny <ModelBindingContext>(), It.IsAny <IParseResult>(), It.IsAny <string>(), It.IsAny <JsonRpcBindingContext>()));
            binderMock.Verify(x => x.SetError(It.IsAny <ModelBindingContext>(), It.IsAny <string>(), It.IsAny <IParseResult>()));
            binderMock.VerifyNoOtherCalls();
            bindingContextMock.Verify();
        }
        public virtual Task SetResult(ModelBindingContext context, IParseResult result, string parameterName, JsonRpcBindingContext jsonRpcBindingContext)
        {
            log.LogTrace($"Binding parameter [{parameterName}]");
            switch (result)
            {
            case SuccessParseResult successBindResult:
                return(SetResultSafe(context, parameterName, successBindResult, jsonRpcBindingContext.Serializer.Serializer));

            case ErrorParseResult errorBindResult:
                return(SetError(context, parameterName, errorBindResult));

            case NoParseResult noParseResult when jsonRpcBindingContext.ParameterMetadata.IsOptional:
                // key was not in json but is optional parameter
                return(SetNoResult(context, parameterName, noParseResult));

            case NoParseResult noBindResult:
                // key was not in json and is required
                return(SetError(context, parameterName, noBindResult));

            case NullParseResult nullParseResult when context.ModelMetadata.IsReferenceOrNullableType:
                // json value was null and type can be null
                return(SetNullResult(context, parameterName, nullParseResult));

            case NullParseResult nullParseResult:
                // json value was null and type can not be null
                var error = new ErrorParseResult($"Can not bind null json value to non-nullable parameter of type [{context.ModelMetadata.ModelType.Name}]", nullParseResult.Key);
                return(SetError(context, parameterName, error));

            default:
                throw new ArgumentOutOfRangeException(nameof(result), result?.GetType().Name);
            }
        }