public async Task BeforeRequestAsyncTest() { var context = new ApiActionContext { HttpApiConfig = new HttpApiConfig(), RequestMessage = new HttpApiRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri("http://www.webapi.com/") }, ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync")) }; var parameter = context.ApiActionDescriptor.Parameters[0]; parameter.Value = "http://www.baidu.com"; var attr = new UriAttribute(); await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter); Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com")); parameter.Value = "/login"; await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter); Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com/login")); }
public async Task OnRequestAsyncRelativeTest() { var apiAction = new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync")); var context = new TestRequestContext(apiAction, "http://www.baidu.com"); var attr = new UriAttribute(); await attr.OnRequestAsync(new ApiParameterContext(context, 0)); Assert.True(context.HttpContext.RequestMessage.RequestUri == new Uri("http://www.baidu.com")); context.Arguments[0] = "/login"; await attr.OnRequestAsync(new ApiParameterContext(context, 0)); Assert.True(context.HttpContext.RequestMessage.RequestUri == new Uri("http://www.baidu.com/login")); context.Arguments[0] = "/login/login2"; await attr.OnRequestAsync(new ApiParameterContext(context, 0)); Assert.True(context.HttpContext.RequestMessage.RequestUri == new Uri("http://www.baidu.com/login/login2")); context.Arguments[0] = "login3"; await attr.OnRequestAsync(new ApiParameterContext(context, 0)); Assert.True(context.HttpContext.RequestMessage.RequestUri == new Uri("http://www.baidu.com/login/login3")); }
public void IsValid(bool expected, string input) { // Arrange var sut = new UriAttribute(); // Act var actual = sut.IsValid(input); // Assert Assert.Equal(expected, actual); }
public async Task BeforeRequestAsyncTest() { var context = new TestActionContext( httpApi: null, httpApiConfig: new HttpApiConfig(), apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))); var parameter = context.ApiActionDescriptor.Parameters[0].Clone("http://www.baidu.com"); var attr = new UriAttribute(); await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter); Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com")); parameter = parameter.Clone("/login"); await((IApiParameterAttribute)attr).BeforeRequestAsync(context, parameter); Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com/login")); }
public static AttributeSyntax CreateFromValidationAttribute(ValidationAttribute validationAttribute) { if (validationAttribute == null) { throw new ArgumentNullException(nameof(validationAttribute)); } var attributeSyntax = validationAttribute switch { EmailAddressAttribute _ => Create(nameof(EmailAddressAttribute)), MinLengthAttribute attribute => CreateWithOneItemWithOneArgument(nameof(MinLengthAttribute), attribute.Length), MaxLengthAttribute attribute => CreateWithOneItemWithOneArgument(nameof(MaxLengthAttribute), attribute.Length), RangeAttribute attribute => CreateWithOneItemWithTwoArgument(nameof(RangeAttribute), attribute.Minimum, attribute.Maximum), RegularExpressionAttribute attribute => CreateWithOneItemWithOneArgument(nameof(RegularExpressionAttribute), attribute.Pattern), RequiredAttribute _ => Create(nameof(RequiredAttribute)), StringLengthAttribute attribute => CreateWithOneItemWithOneArgument(nameof(StringLengthAttribute), attribute.MaximumLength), UriAttribute _ => Create(nameof(UriAttribute)), UrlAttribute _ => Create(nameof(UrlAttribute)), _ => throw new NotImplementedException($"{nameof(ValidationAttribute)} {validationAttribute.GetType()} must be implemented.") }; return(attributeSyntax); }
public void Ctor_GivenRelativePath_UriKnowsItsRelative() { var uriAttrib = new UriAttribute("/relative"); Assert.That(uriAttrib.Uri.IsAbsoluteUri, Is.False); }
public void Ctor_GivenAbsolutePath_UriKnowsItsAbsolute() { var uriAttrib = new UriAttribute("http://www.tempuri.org/relative"); Assert.That(uriAttrib.Uri.IsAbsoluteUri, Is.True); }
public DiscoveredUrl(Uri url, UriAttribute sourceAttribute) { Url = url; SourceAttribute = sourceAttribute; }
public UriTest() { _attrib = new UriAttribute(); }