public void StringResponseWriter_Write_LargeContentWebServerContext_WritesContentAndUpdateContentDisposition() { //------------Setup for test-------------------------- string content; NameValueCollection boundVars; NameValueCollection queryStr; NameValueCollection headers; var request = WebServerRequestTests.CreateHttpRequest(out content, out boundVars, out queryStr, out headers); var context = new WebServerContext(request, boundVars); var contentType = ContentTypes.Xml; var largeContent = CreateLargeContent(contentType); var responseWriter = new StringResponseWriter(largeContent, contentType); //------------Execute Test--------------------------- responseWriter.Write(context); //------------Assert Results------------------------- Assert.AreEqual(ContentTypes.ForceDownload, context.ResponseMessage.Content.Headers.ContentType); Assert.AreEqual("attachment", context.ResponseMessage.Content.Headers.ContentDisposition.DispositionType); Assert.AreEqual("attachment; filename=Output.xml", context.ResponseMessage.Content.Headers.ContentDisposition.ToString()); Assert.IsInstanceOfType(context.ResponseMessage.Content, typeof(StringContent)); var task = context.ResponseMessage.Content.ReadAsStringAsync(); task.Wait(); Assert.AreEqual(largeContent, task.Result); }
public void DynamicFileResponseWriter_Write_WebServerContext_WritesContent() { //------------Setup for test-------------------------- string content; NameValueCollection boundVars; NameValueCollection queryStr; NameValueCollection headers; var request = WebServerRequestTests.CreateHttpRequest(out content, out boundVars, out queryStr, out headers); var context = new WebServerContext(request, boundVars); const string Token = "%%Token%%"; const string LayoutContentFormat = "<html><body>{0}</body></html>"; const string NewContent = "Hello world"; var responseWriter = new TestDynamicFileResponseWriter(string.Format(LayoutContentFormat, Token), Token, NewContent); //------------Execute Test--------------------------- responseWriter.Write(context); //------------Assert Results------------------------- Assert.AreEqual(ContentTypes.Html, context.ResponseMessage.Content.Headers.ContentType); Assert.IsInstanceOfType(context.ResponseMessage.Content, typeof(StringContent)); var task = context.ResponseMessage.Content.ReadAsStringAsync(); task.Wait(); Assert.AreEqual(string.Format(LayoutContentFormat, NewContent), task.Result); }
public void Write(WebServerContext context) { context.ResponseMessage.Content = new StringContent(_text); context.ResponseMessage.Content.Headers.ContentType = _contentType; UpdateContentDisposition(context.ResponseMessage); }
public void StringResponseWriter_Write_WebServerContext_WritesContent() { //------------Setup for test-------------------------- string content; NameValueCollection boundVars; NameValueCollection queryStr; NameValueCollection headers; var request = WebServerRequestTests.CreateHttpRequest(out content, out boundVars, out queryStr, out headers); var context = new WebServerContext(request, boundVars); const string NewContent = "Hello world"; var responseWriter = new StringResponseWriter(NewContent, ContentTypes.Plain); //------------Execute Test--------------------------- responseWriter.Write(context); //------------Assert Results------------------------- Assert.AreEqual(ContentTypes.Plain, context.ResponseMessage.Content.Headers.ContentType); Assert.IsInstanceOfType(context.ResponseMessage.Content, typeof(StringContent)); var task = context.ResponseMessage.Content.ReadAsStringAsync(); task.Wait(); Assert.AreEqual(NewContent, task.Result); }
public void Write(WebServerContext context) { var content = GetContent(); context.ResponseMessage.Content = new StringContent(content); context.ResponseMessage.Content.Headers.ContentType = ContentType; }
public void WebServerContext_Constructor_NullRequestPaths_ThrowsArgumentNullException() { //------------Setup for test-------------------------- //------------Execute Test--------------------------- var context = new WebServerContext(new HttpRequestMessage(), null); //------------Assert Results------------------------- }
public static void Main() { var context = new WebServerContext(); var server = new WebServer( 42420, new ControllerRouter(), new ResourceRouter()); MvcEngine.Run(server); }
public HttpResponseMessage ExecuteLoginWorkflow() { var requestVariables = new NameValueCollection(); var context = new WebServerContext(Request, requestVariables) { Request = { User = User } }; var handler = CreateHandler <TokenRequestHandler>(); handler.ProcessRequest(context); return(context.ResponseMessage); }
public static bool HostBindingExists([NotNull] string host) { Assert.ArgumentNotNull(host, nameof(host)); bool result; using (WebServerContext context = CreateContext()) { result = context.Sites.Any(site => site.Bindings.Any(binding => binding.Host.EqualsIgnoreCase(host))); } return(result); }
public static bool WebsiteExists([NotNull] string name) { Assert.ArgumentNotNull(name, nameof(name)); bool v; using (WebServerContext context = CreateContext()) { v = context.Sites.Any(s => s.Name.EqualsIgnoreCase(name)); } return(v); }
public static void DeleteWebsite([NotNull] long id) { Log.Info($"Deleting website {id}"); using (WebServerContext context = CreateContext()) { Site site = context.Sites.SingleOrDefault(s => s.Id == id); if (site != null) { DeleteWebsite(context, site); } } }
public static bool HostBindingExists([NotNull] string host) { Assert.ArgumentNotNull(host, "host"); bool result; using (WebServerContext context = CreateContext("WebServerManager.HostBindingExists('{0}')".FormatWith(host))) { result = context.Sites.Any(site => site.Bindings.Any(binding => binding.Host.EqualsIgnoreCase(host))); } return(result); }
public static bool WebsiteExists([NotNull] string name) { Assert.ArgumentNotNull(name, "name"); bool v; using (WebServerContext context = CreateContext("WebServerManager.WebsiteExists('{0}')".FormatWith(name))) { v = context.Sites.Any(s => s.Name.EqualsIgnoreCase(name)); } return(v); }
public static void DeleteWebsite([NotNull] long id) { Log.Info("Deleting website {0}".FormatWith(id), typeof(WebServerManager)); using (WebServerContext context = CreateContext("WebServerManager.DeleteWebsite({0})".FormatWith(id))) { Site site = context.Sites.SingleOrDefault(s => s.Id == id); if (site != null) { DeleteWebsite(context, site); } } }
public void WebServerContext_Send_ResponseIsNull_ThrowsArgumentNullException() { //------------Setup for test-------------------------- var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/services") { Content = new StringContent("", Encoding.UTF8) }; var context = new WebServerContext(request, new NameValueCollection()); //------------Execute Test--------------------------- context.Send(null); //------------Assert Results------------------------- }
public static void DeleteWebsite([NotNull] string name) { Assert.ArgumentNotNull(name, "name"); Log.Info("Deleting website {0}".FormatWith(name), typeof(WebServerManager)); using (WebServerContext context = CreateContext("WebServerManager.DeleteWebsite('{0}')".FormatWith(name))) { Site site = context.Sites[name]; if (site != null) { DeleteWebsite(context, site); } } }
public void WebServerContext_Constructor_PropertiesInitialized() { var request = WebServerRequestTests.CreateHttpRequest(out string content, out NameValueCollection boundVars, out NameValueCollection queryStr, out NameValueCollection headers); //------------Execute Test--------------------------- var context = new WebServerContext(request, boundVars); //------------Assert Results------------------------- Assert.IsNotNull(context.ResponseMessage); Assert.IsNotNull(context.Request); Assert.IsNotNull(context.Response); CollectionAssert.AreEqual(headers, context.FetchHeaders()); WebServerRequestTests.VerifyProperties(request, (WebServerRequest)context.Request, content, queryStr, boundVars); }
public void WebServerContext_Dispose_InputStreamIsClosed() { //------------Setup for test-------------------------- var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/services") { Content = new StringContent("", Encoding.UTF8) }; var context = new WebServerContext(request, new NameValueCollection()); //------------Execute Test--------------------------- context.Dispose(); //------------Assert Results------------------------- Assert.IsFalse(context.Request.InputStream.CanRead, "WebServerContext Request input stream not null after dispose."); }
public void StatusResponseWriter_Write_WebServerContext_WritesTheStatus() { //------------Setup for test-------------------------- const HttpStatusCode Expected = HttpStatusCode.PaymentRequired; var request = WebServerRequestTests.CreateHttpRequest(out string content, out NameValueCollection boundVars, out NameValueCollection queryStr, out NameValueCollection headers); var context = new WebServerContext(request, boundVars); var responseWriter = new StatusResponseWriter(Expected); //------------Execute Test--------------------------- responseWriter.Write(context); //------------Assert Results------------------------- Assert.AreEqual(Expected, context.ResponseMessage.StatusCode); }
public async Task Handle(HttpContext context) { RenderContext renderContext = await WebServerContext.GetRenderContext(context); try { await _handle(renderContext); await WebServerContext.SetResponse(context, renderContext); } catch (Exception ex) { renderContext.Response.StatusCode = 500; renderContext.Response.Body = System.Text.Encoding.UTF8.GetBytes(ex.Message); await WebServerContext.SetResponse(context, renderContext); } }
protected virtual HttpResponseMessage ProcessRequest <TRequestHandler>(NameValueCollection requestVariables) where TRequestHandler : class, IRequestHandler, new() { if (!IsAuthenticated()) { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } var context = new WebServerContext(Request, requestVariables) { Request = { User = User } }; var handler = CreateHandler <TRequestHandler>(); handler.ProcessRequest(context); return(context.ResponseMessage); }
public void WebServerContext_Send_ResponseIsNotNull_InvokesWriteOnResponse() { //------------Setup for test-------------------------- var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/services") { Content = new StringContent("", Encoding.UTF8) }; var context = new WebServerContext(request, new NameValueCollection()); var response = new Mock <IResponseWriter>(); response.Setup(r => r.Write(It.IsAny <WebServerContext>())).Verifiable(); //------------Execute Test--------------------------- context.Send(response.Object); //------------Assert Results------------------------- response.Verify(r => r.Write(It.IsAny <WebServerContext>())); }
protected HttpResponseMessage ProcessRequest <TRequestHandler>() where TRequestHandler : class, IRequestHandler, new() { if (!IsAuthenticated()) { return(Request.CreateWarewolfErrorResponse(new WarewolfErrorResponseArgs { StatusCode = HttpStatusCode.Unauthorized, Title = GlobalConstants.USER_UNAUTHORIZED, Message = ErrorResource.AuthorizationDeniedForThisUser })); } var context = new WebServerContext(Request) { Request = { User = User } }; var handler = CreateHandler <TRequestHandler>(); handler.ProcessRequest(context); return(context.ResponseMessage); }
public static bool AddHostBinding([NotNull] string siteName, [NotNull] BindingInfo binding) { Assert.ArgumentNotNull(siteName, nameof(siteName)); Assert.ArgumentNotNull(binding, nameof(binding)); using (WebServerContext context = CreateContext("WebServerManager.AddHostBinding('{0}','{1}')".FormatWith(siteName, binding.Host))) { Site siteInfo = context.Sites.FirstOrDefault(site => Extensions.EqualsIgnoreCase(site.Name, siteName)); if (HostBindingExists(binding.Host) || siteInfo == null) { return(false); } var bindingInformation = binding.IP + ":" + binding.Port + ":" + binding.Host; siteInfo.Bindings.Add(bindingInformation, binding.Protocol); context.CommitChanges(); } return(true); }
public static bool AddHostBinding([NotNull] string siteName, [NotNull] BindingInfo binding) { Assert.ArgumentNotNull(siteName, nameof(siteName)); Assert.ArgumentNotNull(binding, nameof(binding)); using (WebServerContext context = CreateContext()) { Site siteInfo = context.Sites.FirstOrDefault(site => site.Name.EqualsIgnoreCase(siteName)); if (HostBindingExists(binding.Host) || siteInfo == null) { return(false); } var bindingInformation = $"{binding.IP}:{binding.Port}:{binding.Host}"; siteInfo.Bindings.Add(bindingInformation, binding.Protocol); context.CommitChanges(); } return(true); }
private static void DeleteWebsite([NotNull] WebServerContext context, [NotNull] Site site) { Assert.ArgumentNotNull(context, nameof(context)); Assert.ArgumentNotNull(site, nameof(site)); foreach (Application application in site.Applications) { var applicationPoolName = application.ApplicationPoolName; ApplicationPool appplicationPool = context.ApplicationPools[applicationPoolName]; // Application is used only in the current website or isn't used at all if (appplicationPool != null && context.Sites.Count(s => s.ApplicationDefaults.ApplicationPoolName.EqualsIgnoreCase(applicationPoolName)) <= 1) { context.ApplicationPools.Remove(appplicationPool); } } context.Sites.Remove(site); context.CommitChanges(); }
protected virtual HttpResponseMessage ProcessRequest <TRequestHandler>(NameValueCollection requestVariables, bool isUrlWithTokenPrefix) where TRequestHandler : class, IRequestHandler, new() { var user = User; try { if (isUrlWithTokenPrefix) { if (!TryOverrideByToken(ref user)) { return(Request.CreateWarewolfErrorResponse(new WarewolfErrorResponseArgs { StatusCode = HttpStatusCode.Unauthorized, Title = GlobalConstants.TOKEN_UNAUTHORIZED, Message = ErrorResource.AuthorizationDeniedForThisToken })); } } else { if (!IsAuthenticated()) { return(Request.CreateWarewolfErrorResponse(new WarewolfErrorResponseArgs { StatusCode = HttpStatusCode.Unauthorized, Title = GlobalConstants.USER_UNAUTHORIZED, Message = ErrorResource.AuthorizationDeniedForThisUser })); } } var context = new WebServerContext(Request, requestVariables) { Request = { User = user } }; var handler = CreateHandler <TRequestHandler>(); handler.ProcessRequest(context); return(context.ResponseMessage); } catch (Exception e) { return(Request.CreateWarewolfErrorResponse(new WarewolfErrorResponseArgs { StatusCode = HttpStatusCode.InternalServerError, Title = GlobalConstants.INTERNAL_SERVER_ERROR, Message = e.Message })); } }
protected virtual HttpResponseMessage ProcessRequest <TRequestHandler>(NameValueCollection requestVariables, bool isUrlWithTokenPrefix) where TRequestHandler : class, IRequestHandler, new() { var user = User; try { if (isUrlWithTokenPrefix) { if (!TryOverrideByToken(ref user)) { throw new HttpException((int)HttpStatusCode.Unauthorized, Warewolf.Resource.Errors.ErrorResource.TokenNotAuthorizedToExecuteOuterWorkflowException); } } else { if (!IsAuthenticated()) { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } } var context = new WebServerContext(Request, requestVariables) { Request = { User = user } }; var handler = CreateHandler <TRequestHandler>(); handler.ProcessRequest(context); return(context.ResponseMessage); } catch (HttpException e) { var response = Request.CreateResponse((HttpStatusCode)e.GetHttpCode()); response.ReasonPhrase = e.Message; return(response); } }
public void Write(WebServerContext context) { context.ResponseMessage.Content = new StreamContent(File.Open(_fileName,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)); }
public void Write(WebServerContext context) { context.ResponseMessage.StatusCode = _statusCode; }
public void Write(WebServerContext context) { var stream = new HttpFileStream(OpenFileStream, context.ResponseMessage, _contentType, _chunkSize); stream.Write(); }