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 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 StringResponseWriter_Constructor_ContentTypeIsNullString_ThrowsFormatException()
        {
            //------------Setup for test--------------------------

            //------------Execute Test---------------------------
            var responseWriter = new StringResponseWriter("XXX", (string)null);

            //------------Assert Results-------------------------
        }
        public void StringResponseWriter_Constructor_ContentTypeIsNullMediaTypeHeaderValue_ThrowsArgumentNullException()
        {
            //------------Setup for test--------------------------

            //------------Execute Test---------------------------
            var responseWriter = new StringResponseWriter("XXX", (MediaTypeHeaderValue)null);

            //------------Assert Results-------------------------
        }
Ejemplo n.º 5
0
        public override void ProcessRequest(ICommunicationContext ctx)
        {
            if (ctx.Request.QueryString.HasKeys())
            {
                var numberOfLines = GlobalConstants.LogFileNumberOfLines;
                try
                {
                    var numberOfLinesString = ctx.Request.QueryString.Get("numLines");
                    int numLines;
                    if (int.TryParse(numberOfLinesString, out numLines))
                    {
                        if (numLines > 0)
                        {
                            numberOfLines = numLines;
                        }
                    }
                }
                catch (Exception)
                {
                    //Bad Query return default number of lines
                }

                string serverLogFile;
                var    logFile = EnvironmentVariables.ServerLogFile;
                if (File.Exists(logFile))
                {
                    var    buffor = new Queue <string>(numberOfLines);
                    Stream stream = File.Open(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    var    file   = new StreamReader(stream);
                    while (!file.EndOfStream)
                    {
                        string line = file.ReadLine();

                        if (buffor.Count >= numberOfLines)
                        {
                            buffor.Dequeue();
                        }
                        buffor.Enqueue(line);
                    }
                    string[] lastLines = buffor.ToArray();
                    serverLogFile = string.Join(Environment.NewLine, lastLines);
                }
                else
                {
                    serverLogFile = "Could not locate Warewolf Server log file.";
                }
                var response = new StringResponseWriter(serverLogFile, "text/xml");
                ctx.Send(response);
            }
            else
            {
                var result = GetFileFromPath(EnvironmentVariables.ServerLogFile);
                ctx.Send(result);
            }
        }
Ejemplo n.º 6
0
        static IResponseWriter GetApisJson(string basePath, bool isPublic)
        {
            var apiBuilder     = new ApisJsonBuilder(_authorizationService, _resourceCatalog);
            var apis           = apiBuilder.BuildForPath(basePath, isPublic);
            var converter      = new JsonSerializer();
            var result         = new StringBuilder();
            var jsonTextWriter = new JsonTextWriter(new StringWriter(result))
            {
                Formatting = Formatting.Indented
            };

            converter.Serialize(jsonTextWriter, apis);
            jsonTextWriter.Flush();
            var apisJson             = result.ToString();
            var stringResponseWriter = new StringResponseWriter(apisJson, ContentTypes.Json, false);

            return(stringResponseWriter);
        }