public void apiRequestContextResolve___does_not_allow_multiple_sets() { var resolver = new ApiRequestContextResolver(); resolver.SetContext(new ApiRequestContext()); var exception = Assert.Throws <InvalidOperationException>(() => resolver.SetContext(new ApiRequestContext())); exception.Should().NotBeNull(); exception.Message.Should().Be("Attempt to overwrite existing context not allowed"); }
public async void ReturnsTrueAndDoesWritesUsingDefaultFormatterWhenMissingRequestAccept(string accept) { var formatter = SetupJsonFormatterMock(null, new string[] { "application/json" }); var mockFactory = SetupFormatterFactory(formatter.Object); var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Response = new ApiResponseInfo { StatusCode = 201, ResponseObject = "test" }, Request = new ApiRequestInfo { Accept = accept } }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpResponseBodyWriting(contextResolver, mockFactory.Object).ConfigureAwait(false); processed.Should().BeTrue(); context.Response.ResponseObject.Should().NotBeNull(); context.Response.StatusCode.Should().Be(201); context.Response.Headers.Should().NotBeNull(); context.Response.Headers.Should().HaveCount(0); context.Response.ContentType.Should().NotBeNull(); context.Response.ContentType.Should().Be("application/json"); context.Response.ContentLength.Should().Be(0); }
public async void body_binding___returns_false_and_415_status_for_missing_contenttype_and_has_content(string contentType) { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Request = new ApiRequestInfo { Method = "PUT", ContentLength = 1, ContentType = contentType } }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestBodyBinding(contextResolver, null).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.StatusCode.Should().Be(415); }
public async void body_binding___returns_false_and_411_status_for_contentlength_supplied_but_null_invocationcontext() { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Request = new ApiRequestInfo { Method = "PATCH", ContentLength = 1, ContentType = "application/json", InvocationContext = null }, }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestBodyBinding(contextResolver, null).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.StatusCode.Should().Be(413); }
public async void ReturnsEmptyAcceptAllowWhenFormatterFactoryIsNull() { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Routing = new ApiRoutingInfo { Route = new ApiRoutingItem { Location = new ApiEndpointLocation( controller: typeof(TestController), methodInfo: typeof(TestController).GetMethod(nameof(TestController.Get)), httpMethod: "GET") } }, Request = new ApiRequestInfo() }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestAccept(contextResolver, null).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.StatusCode.Should().Be(406); context.Response.Headers.Should().NotBeNull(); context.Response.Headers.Should().HaveCount(1); context.Response.Headers[0].Name.Should().Be("X-Allow-Accept"); context.Response.Headers[0].Value.Should().Be(string.Empty); }
public async void body_binding___returns_true_for_successfull_bodymodel_binding() { using (var memoryStream = new MemoryStream()) using (var writer = new StreamWriter(memoryStream)) { writer.Write("{ \"Name\": \"MyHeader\", \"Value\": \"MyValue\" }"); await writer.FlushAsync().ConfigureAwait(false); memoryStream.Seek(0, SeekOrigin.Begin); var formatter = SetupJsonFormatterMock(new string[] { "application/json" }, null); var mockFactory = SetupFormatterFactory(formatter.Object); var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Request = new ApiRequestInfo { Method = "PATCH", ContentLength = 1, ContentType = "application/json", InvocationContext = new ApiInvocationContext { }, Body = memoryStream }, Routing = new ApiRoutingInfo { Route = new ApiRoutingItem { Location = new ApiEndpointLocation( controller: null, httpMethod: null, methodInfo: null, uriParameterType: null, bodyParameterType: typeof(ApiHeader), simpleParameters: null, methodReturnType: null) } } }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestBodyBinding(contextResolver, mockFactory.Object).ConfigureAwait(false); processed.Should().BeTrue(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Request.InvocationContext.BodyModel.Should().NotBeNull(); context.Request.InvocationContext.BodyModel.Should().BeOfType <ApiHeader>(); ((ApiHeader)context.Request.InvocationContext.BodyModel).Name.Should().Be("MyHeader"); ((ApiHeader)context.Request.InvocationContext.BodyModel).Value.Should().Be("MyValue"); } }
public async void body_binding___returns_false_and_415_status_for_non_writable_formatter() { var xmlformatter = SetupXmlFormatterMock(null, null); var jsonformatter = SetupJsonFormatterMock(null, null); var mockFactory = SetupFormatterFactory(xmlformatter.Object, jsonformatter.Object); var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Request = new ApiRequestInfo { Method = "POST", ContentLength = 1, ContentType = "application/json", InvocationContext = new ApiInvocationContext { } }, Routing = new ApiRoutingInfo { Route = new ApiRoutingItem { Location = new ApiEndpointLocation( controller: null, httpMethod: null, methodInfo: null, uriParameterType: null, bodyParameterType: typeof(string), simpleParameters: null, methodReturnType: null) } } }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestBodyBinding(contextResolver, mockFactory.Object).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.StatusCode.Should().Be(415); }
public async void ReturnsFalseForCancelledRequest() { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(true) }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestAccept(contextResolver, null).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); }
public async void ReturnsTrueAndDoesWritesWithMatchedEtagButUnMatchedIfModifiedSince() { var formatter = SetupJsonFormatterMock(null, new string[] { "application/json", "text/json" }); var mockFactory = SetupFormatterFactory(formatter.Object); var etag = "TEST-IF-MATCH"; DateTimeOffset lastModifed = DateTimeOffset.UtcNow; var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Response = new ApiResponseInfo { StatusCode = 201, ResponseObject = "test" }, Request = new ApiRequestInfo { Method = "GET", Accept = "application/json", IfMatch = etag, IfModifiedSince = lastModifed.AddSeconds(1) } }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); context.Response.Headers.Add(new ApiHeader("ETag", etag)); context.Response.Headers.Add(new ApiHeader("Last-Modified", lastModifed.ToString("r"))); var processed = await context.ProcessHttpResponseBodyWriting(contextResolver, mockFactory.Object).ConfigureAwait(false); processed.Should().BeTrue(); context.Response.ResponseObject.Should().NotBeNull(); context.Response.StatusCode.Should().Be(201); context.Response.Headers.Should().NotBeNull(); context.Response.Headers.Should().HaveCount(2); context.Response.ContentType.Should().NotBeNull(); context.Response.ContentType.Should().Be("application/json"); context.Response.ContentLength.Should().Be(0); }
public async void body_binding___returns_false_for_cancelled_request() { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(true), Request = null }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestBodyBinding(contextResolver, null).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); }
public async void body_binding___returns_false_and_411_status_for_contentlength_supplied_but_null_invocationcontext_bodymodeltype() { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Request = new ApiRequestInfo { Method = "PATCH", ContentLength = 1, ContentType = "application/json", InvocationContext = new ApiInvocationContext { } }, Routing = new ApiRoutingInfo { Route = new ApiRoutingItem { Location = new ApiEndpointLocation( controller: null, httpMethod: null, methodInfo: null, uriParameterType: null, bodyParameterType: null, simpleParameters: null, methodReturnType: null) } } }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestBodyBinding(contextResolver, null).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.StatusCode.Should().Be(413); }
public async void ReturnsAllFormatterTypesAcceptAllowWhenFormatterNotFound() { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Routing = new ApiRoutingInfo { Route = new ApiRoutingItem { Location = new ApiEndpointLocation( controller: typeof(TestController), methodInfo: typeof(TestController).GetMethod(nameof(TestController.Get)), httpMethod: "GET") } }, Request = new ApiRequestInfo() }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var mockFormatterFactory = new Mock <IDeepSleepMediaSerializerFactory>(); mockFormatterFactory .Setup(m => m.GetWriteableTypes(It.IsAny <Type>(), It.IsAny <IList <IDeepSleepMediaSerializer> >())) .Returns(new string[] { "application/json", "text/xml", "text/plain" }); var processed = await context.ProcessHttpRequestAccept(contextResolver, mockFormatterFactory.Object).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.StatusCode.Should().Be(406); context.Response.Headers.Should().NotBeNull(); context.Response.Headers.Should().HaveCount(1); context.Response.Headers[0].Name.Should().Be("X-Allow-Accept"); context.Response.Headers[0].Value.Should().Be("application/json, text/xml, text/plain"); }
public async void ReturnsTrueForFoundFormatter() { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Routing = new ApiRoutingInfo { Route = new ApiRoutingItem { Location = new ApiEndpointLocation( controller: typeof(TestController), methodInfo: typeof(TestController).GetMethod(nameof(TestController.Get)), httpMethod: "GET") } }, Request = new ApiRequestInfo() }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); string formatterType; var mockFormatter = new Mock <IDeepSleepMediaSerializer>(); var mockFormatterFactory = new Mock <IDeepSleepMediaSerializerFactory>(); mockFormatterFactory .Setup(m => m.GetAcceptableFormatter(It.IsAny <AcceptHeader>(), It.IsAny <Type>(), out formatterType, It.IsAny <IList <IDeepSleepMediaSerializer> >(), It.IsAny <IList <string> >())) .Returns(Task.FromResult(mockFormatter.Object)); var processed = await context.ProcessHttpRequestAccept(contextResolver, mockFormatterFactory.Object).ConfigureAwait(false); processed.Should().BeTrue(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.Headers.Should().NotBeNull(); context.Response.Headers.Should().HaveCount(0); }
public async void ReturnsTrueForFoundImplmentedFormatter(string requestAccept) { var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Routing = new ApiRoutingInfo { Route = new ApiRoutingItem { Location = new ApiEndpointLocation( controller: typeof(TestController), methodInfo: typeof(TestController).GetMethod(nameof(TestController.Get)), httpMethod: "GET") } }, Request = new ApiRequestInfo { Accept = new AcceptHeader(requestAccept) } }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var formatter = SetupJsonFormatterMock(null, new string[] { "application/json" }); var mockFactory = SetupFormatterFactory(formatter.Object); var processed = await context.ProcessHttpRequestAccept(contextResolver, mockFactory.Object).ConfigureAwait(false); processed.Should().BeTrue(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.Headers.Should().NotBeNull(); context.Response.Headers.Should().HaveCount(0); }
public async void body_binding___returns_false_and_413_status_for_maxrequestlength_too_long() { using (var memoryStream = new MemoryStream()) using (var writer = new StreamWriter(memoryStream)) { writer.Write("{ \"Name\": \"MyHeader\", \"Value\": \"MyValue\" }"); await writer.FlushAsync().ConfigureAwait(false); memoryStream.Seek(0, SeekOrigin.Begin); var formatter = SetupJsonFormatterMock(new string[] { "application/json" }, null); var mockFactory = SetupFormatterFactory(formatter.Object); var context = new ApiRequestContext { RequestAborted = new System.Threading.CancellationToken(false), Request = new ApiRequestInfo { Method = "PATCH", ContentLength = 10, ContentType = "application/json", InvocationContext = new ApiInvocationContext { //BodyModelType = typeof(ApiHeader) }, Body = memoryStream }, Configuration = new DeepSleepRequestConfiguration { RequestValidation = new ApiRequestValidationConfiguration { MaxRequestLength = 1 } }, Routing = new ApiRoutingInfo { Route = new ApiRoutingItem { Location = new ApiEndpointLocation( controller: null, httpMethod: null, methodInfo: null, uriParameterType: null, bodyParameterType: null, simpleParameters: null, methodReturnType: null) } } }; var contextResolver = new ApiRequestContextResolver(); contextResolver.SetContext(context); var processed = await context.ProcessHttpRequestBodyBinding(contextResolver, mockFactory.Object).ConfigureAwait(false); processed.Should().BeFalse(); context.Response.Should().NotBeNull(); context.Response.ResponseObject.Should().BeNull(); context.Response.StatusCode.Should().Be(413); } }