public void ApiKey_ExecutionRequestValidationShouldFailIfBadToken() { RegisterDb(); ServiceProxySystem.Register <ApiKeyRequiredEcho>(); IUserResolver mockUserResolver = Substitute.For <IUserResolver>(); mockUserResolver.GetUser(Arg.Any <IHttpContext>()).Returns("testUser"); LocalApiKeyManager.Default.UserResolver = mockUserResolver; string methodName = MethodBase.GetCurrentMethod().Name; IApplicationNameProvider nameProvider = new TestApplicationNameProvider(methodName.RandomLetters(4)); IApiKeyProvider keyProvider = new LocalApiKeyProvider(); ExecutionRequest er = new ExecutionRequest("ApiKeyRequiredEcho", "Send", "json") { ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider), Request = new ServiceProxyTestHelpers.JsonTestRequest() }; string data = ApiParameters.ParametersToJsonParamsObjectString("some random data"); er.InputString = data; ApiKeyResolver resolver = new ApiKeyResolver(keyProvider, nameProvider); resolver.SetKeyToken(er.Request.Headers, data); er.Request.Headers[Headers.KeyToken] = "bad token value"; ValidationResult result = er.Validate(); Expect.IsFalse(result.Success, "Validation should have failed"); List <ValidationFailures> failures = new List <ValidationFailures>(result.ValidationFailures); Expect.IsTrue(failures.Contains(ValidationFailures.InvalidApiKeyToken), "ValidationFailure should have been InvalidApiKeyToken"); }
public void ApiKey_ExecutionRequestShouldValidateApiKey() { RegisterDb(); ServiceProxySystem.Register <ApiKeyRequiredEcho>(); string testName = MethodBase.GetCurrentMethod().Name; IUserResolver mockUserResolver = Substitute.For <IUserResolver>(); mockUserResolver.GetUser(Arg.Any <IHttpContext>()).Returns("testUser"); LocalApiKeyManager.Default.UserResolver = mockUserResolver; IApplicationNameProvider nameProvider = new TestApplicationNameProvider(testName.RandomLetters(6)); IApiKeyProvider keyProvider = new LocalApiKeyProvider(); ExecutionRequest er = new ExecutionRequest("ApiKeyRequiredEcho", "Send", "json") { ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider), Request = new ServiceProxyTestHelpers.JsonTestRequest() }; string data = ApiParameters.ParametersToJsonParamsObjectString("some random data"); er.InputString = data; ValidationResult result = er.Validate(); result.Success.IsFalse(); List <ValidationFailures> failures = new List <ValidationFailures>(result.ValidationFailures); failures.Contains(ValidationFailures.InvalidApiKeyToken).IsTrue(); }
public void ApiKey_ExecutionRequestValidationShouldSucceedIfGoodToken() { Prepare(); ServiceProxySystem.Register <ApiKeyRequiredEcho>(); string methodName = MethodBase.GetCurrentMethod().Name; IApplicationNameProvider nameProvider = new TestApplicationNameProvider(methodName); IApiKeyProvider keyProvider = new LocalApiKeyProvider(); string className = "ApiKeyRequiredEcho"; string method = "Send"; string data = ApiParameters.ParametersToJsonParamsArray("some random data").ToJson(); ExecutionRequest er = new ExecutionRequest(className, method, "json") { JsonParams = data, ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider), Request = new ServiceProxyTestHelpers.FormUrlEncodedTestRequest() }; er.ApiKeyResolver.SetKeyToken(er.Request.Headers, ApiParameters.GetStringToHash(className, method, data)); ValidationResult result = er.Validate(); Expect.IsTrue(result.Success); }
/// <summary> /// Build a form to be used as parameters for the specified method /// </summary> /// <param name="type"></param> /// <param name="wrapperTagName"></param> /// <param name="methodName"></param> /// <param name="defaults"></param> /// <param name="registerProxy"></param> /// <param name="paramCount"></param> /// <returns></returns> public TagBuilder MethodForm(Type type, string wrapperTagName, string methodName, Dictionary <string, object> defaults, bool registerProxy, out int paramCount) { Args.ThrowIfNull(type, "InvocationType"); if (registerProxy) { ServiceProxySystem.Register(type); } MethodInfo method = type.GetMethod(methodName); defaults = defaults ?? new Dictionary <string, object>(); System.Reflection.ParameterInfo[] parameters = method.GetParameters(); paramCount = parameters.Length; TagBuilder form = new TagBuilder(wrapperTagName); for (int i = 0; i < parameters.Length; i++) { System.Reflection.ParameterInfo parameter = parameters[i]; object defaultValue = defaults.ContainsKey(parameter.Name) ? defaults[parameter.Name] : null; string defaultString = defaultValue == null ? string.Empty : defaultValue.ToString(); TagBuilder label = new TagBuilder("label") .Html(string.Format(this.LabelFormat, parameter.Name.PascalSplit(" "))) .Css(this.LabelCssClass); bool addLabel = this.AddLabels; bool addValue = true; bool wasObject = false; bool handled = false; TryBuildPrimitiveInput(parameter, defaultValue, ref addValue, ref handled, out TagBuilder toAdd, out Type paramType); if (!handled) { string legend = GetLegend(paramType); toAdd = FieldsetFor(paramType, defaultValue, legend); addLabel = false; addValue = false; wasObject = true; } toAdd.DataSet("parameter-name", parameter.Name) .DataSetIf(!wasObject, "type", parameter.ParameterType.Name.ToLowerInvariant()) .ValueIf(!string.IsNullOrEmpty(defaultString) && addValue, defaultString); if (addLabel) { form.Child(label).BrIf(this.Layout == ParameterLayouts.BreakAfterLabels); } form.Child(toAdd).BrIf( this.Layout != ParameterLayouts.NoBreaks && i != parameters.Length - 1 && !wasObject); } return(form.DataSet("method", methodName) .FirstChildIf(wrapperTagName.Equals("fieldset"), new TagBuilder("legend") .Text(GetLegend(method)))); }
public void ShouldSetContext() { ExecutionRequest execRequest = CreateExecutionRequest("/TakesContextTest/Monkey.json"); ServiceProxySystem.Register <TakesContextTest>(); Expect.IsFalse(_setContextCalled.Value); execRequest.Execute(); Expect.IsTrue(_setContextCalled.Value); }
public void MethodNotFoundShouldBeReturned() { ServiceProxySystem.Register <TestClass>(); ExecutionRequest er = new ExecutionRequest("TestClass", "MissingMethod", "json"); ValidationResult result = er.Validate(); Expect.IsFalse(result.Success); Expect.IsTrue(result.ValidationFailures.ToList().Contains(ValidationFailures.MethodNotFound)); Message.PrintLine(result.Message); }
public void MissingMethodShouldReturnMethodNotSpecified() { ServiceProxySystem.Register <TestClass>(); ExecutionRequest er = new ExecutionRequest("TestClass", "", "json"); ValidationResult result = er.Validate(); Expect.IsFalse(result.Success); Expect.IsTrue(result.ValidationFailures.ToList().Contains(ValidationFailures.MethodNameNotSpecified)); OutLineFormat(result.Message); }
public void ExecuteShouldSucceed() { ServiceProxySystem.Register <TestClass>(); ExecutionRequest er = new ExecutionRequest("TestClass", "ShouldWork", "json"); ValidationResult result = er.Validate(); Expect.IsTrue(result.Success); er.Execute(); Expect.IsTrue(er.Result.Equals("Yay")); Message.PrintLine(er.Result.ToString()); }
public void ParameterCountMismatchShouldBeReturned() { ServiceProxySystem.Register <TestClass>(); ExecutionRequest er = new ExecutionRequest("TestClass", "TestMethod", "json"); er.Arguments = new object[] { new { }, new { } }; ValidationResult result = er.Validate(); Expect.IsFalse(result.Success); Expect.IsTrue(result.ValidationFailures.ToList().Contains(ValidationFailures.ParameterCountMismatch)); Message.PrintLine(result.Message); }
/// <summary> /// Build a fieldset element for the specified method of the specified /// class. /// </summary> /// <param name="helper">The FileExtHelper</param> /// <param name="type">The Type whose method will be called</param> /// <param name="methodName">The name of the method that will be called</param> /// <param name="defaults">A dictionary of default values to fill into the resulting /// form keyed by the names of the method parameters</param> /// <returns>MvchtmlString</returns> public static MvcHtmlString ParamsFor(this HtmlHelper helper, Type type, string methodName, string wrapperTagName = "fieldset", Dictionary <string, object> defaults = null, object htmlAttributes = null) { if (ServiceProxySystem.Incubator[type] == null) { ServiceProxySystem.Register(type); } int ignore = -1; TagBuilder form = GetParamsBuilder(type, methodName, wrapperTagName, defaults, htmlAttributes, out ignore); return(MvcHtmlString.Create(form.ToString())); }
public void LocalMethodShouldValidateIfLocalClient() { ServiceProxySystem.Register <TestClass>(); IRequest request = Substitute.For <IRequest>(); request.QueryString.Returns(new NameValueCollection()); request.UserHostAddress.Returns("127.0.0.1:80"); IResponse response = Substitute.For <IResponse>(); IHttpContext context = Substitute.For <IHttpContext>(); context.Request.Returns(request); context.Response.Returns(response); ExecutionRequest er = new ExecutionRequest("TestClass", "LocalMethod", "json") { Context = context }; ValidationResult result = er.Validate(); Expect.IsTrue(result.Success); }
public void LocalMethodShouldNotValidateIfNotLoopback() { ServiceProxySystem.Register <TestClass>(); IRequest request = Substitute.For <IRequest>(); request.QueryString.Returns(new NameValueCollection()); request.UserHostAddress.Returns("192.168.0.80:80"); IResponse response = Substitute.For <IResponse>(); IHttpContext context = Substitute.For <IHttpContext>(); context.Request.Returns(request); context.Response.Returns(response); ExecutionRequest er = new ExecutionRequest("TestClass", "LocalMethod", "json") { Context = context }; ValidationResult result = er.Validate(); Expect.IsFalse(result.Success); Expect.IsTrue(result.ValidationFailures.ToList().Contains(ValidationFailures.RemoteExecutionNotAllowed)); }
public void ApiKey_ExecutionRequestShouldValidateApiKey() { RegisterDb(); ServiceProxySystem.Register <ApiKeyRequiredEcho>(); string testName = MethodBase.GetCurrentMethod().Name; IApplicationNameProvider nameProvider = new TestApplicationNameProvider(testName.RandomLetters(6)); IApiKeyProvider keyProvider = new LocalApiKeyProvider(); ExecutionRequest er = new ExecutionRequest("ApiKeyRequiredEcho", "Send", "json"); er.ApiKeyResolver = new ApiKeyResolver(keyProvider, nameProvider); er.Request = new ServiceProxyTestHelpers.JsonTestRequest(); string data = ApiParameters.ParametersToJsonParamsObjectString("some random data"); er.InputString = data; ValidationResult result = er.Validate(); Expect.IsFalse(result.Success); List <ValidationFailures> failures = new List <ValidationFailures>(result.ValidationFailures); Expect.IsTrue(failures.Contains(ValidationFailures.InvalidApiKeyToken)); }
public static void RegisterProxy(string appName) { ServiceProxySystem.Register <Fs>(new Fs(appName)); }