예제 #1
0
        public void Handle(HttpStatusCode statusCode, NancyContext context)
        {
            var response = new GenericFileResponse("views/404.html", "text/html");

            response.StatusCode = statusCode;
            context.Response    = response;
        }
        public void Should_set_status_code_to_ok()
        {
            // Given, When
            var response = new GenericFileResponse(this.imagePath, imageContentType, this.context);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.OK);
        }
예제 #3
0
        public void Should_set_status_code_to_not_found_when_file_name_is_empty()
        {
            // Given, When
            var response = new GenericFileResponse(string.Empty, imageContentType);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
        public void Should_set_status_code_to_not_found_when_file_name_is_null()
        {
            // Given, When
            var response = new GenericFileResponse(null, imageContentType, this.context);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
        public void Should_set_filename_property_to_filename()
        {
            // Given, When
            var response = new GenericFileResponse(this.imagePath, imageContentType, this.context);

            // Then
            response.Filename.ShouldEqual(Path.GetFileName(this.imagePath));
        }
예제 #6
0
        public void Should_set_status_code_to_ok()
        {
            // Given
            // When
            var response = new GenericFileResponse(this.filePath, "text/plain", this.context);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.OK);
        }
예제 #7
0
        public void Should_set_status_code_to_not_found_when_file_does_not_exist()
        {
            // Given
            // When
            var response = new GenericFileResponse("nancy", this.fileContentType, this.context);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
        public void Should_contain_etag_in_response_header()
        {
            // Given, when
            var response =
                new GenericFileResponse(this.imagePath, imageContentType, this.context);

            // Then
            response.Headers["ETag"].ShouldStartWith("\"");
            response.Headers["ETag"].ShouldEndWith("\"");
        }
예제 #9
0
        public void Should_set_content_length_in_response_header()
        {
            // Given, when
            var path     = Path.Combine(this.GetLocation(), this.filePath);
            var expected = new FileInfo(path).Length.ToString();
            var response = new GenericFileResponse(this.filePath, this.fileContentType, this.context);

            // Then
            response.Headers["Content-Length"].ShouldEqual(expected);
        }
        public void Should_set_content_length_in_response_header()
        {
            // Given, when
            var expected = new FileInfo(imagePath).Length.ToString();
            var response =
                new GenericFileResponse(this.imagePath, imageContentType, this.context);

            // Then
            response.Headers["Content-Length"].ShouldEqual(expected);
        }
예제 #11
0
        public void Should_set_status_code_to_not_found_when_file_name_does_not_contain_extension()
        {
            // Given
            var fileNameWithoutExtensions = Path.GetFileNameWithoutExtension(this.filePath);

            // When
            var response = new GenericFileResponse(fileNameWithoutExtensions, this.fileContentType, this.context);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
        public void Should_set_status_code_to_not_found_when_file_does_not_exist()
        {
            // Given
            var path = Path.Combine("Resources", "thatsnotit.jpg");

            // When
            var response = new GenericFileResponse(path, imageContentType, this.context);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
예제 #13
0
        public void Should_set_status_code_to_not_found_when_file_name_does_not_contain_extension()
        {
            // Given
            var path = Path.Combine("Resources", "zip");

            // When
            var response = new GenericFileResponse(path, imageContentType);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
        public void Should_set_status_code_to_not_found_when_file_is_above_root_path()
        {
            // Given
            var path =
                Path.Combine(this.imagePath, "..", "..");

            // When
            var response = new GenericFileResponse(path, imageContentType, this.context);

            // Then
            response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
        }
        public void Should_return_file_unchanged()
        {
            // Given
            var expected = File.ReadAllBytes(this.imagePath);
            var response = new GenericFileResponse(this.imagePath, imageContentType, this.context);

            // When
            var result = GetResponseContents(response);

            // Then
            result.ShouldEqualSequence(expected);
        }
예제 #16
0
        public void Should_return_file_unchanged()
        {
            // Given
            var path     = Path.Combine(this.GetLocation(), this.filePath);
            var expected = File.ReadAllBytes(path);
            var response = new GenericFileResponse(this.filePath, this.fileContentType, this.context);

            // When
            var result = GetResponseContents(response);

            // Then
            result.ShouldEqualSequence(expected);
        }
예제 #17
0
        public void Should_use_filename_and_content_type_for_attachments_from_file_response_if_not_overridden()
        {
            // Given
            var assemblyPath =
                Path.GetDirectoryName(this.GetType().Assembly.Location);

            GenericFileResponse.RootPath = assemblyPath;

            var filename = Path.GetFileName(this.GetType().Assembly.Location);
            var response = new GenericFileResponse(filename, "image/png");

            // When
            var result = response.AsAttachment();

            // Then
            result.Headers["Content-Disposition"].ShouldContain(filename);
            result.ContentType.ShouldEqual("image/png");
        }
예제 #18
0
        public void Should_use_filename_and_content_type_for_attachments_from_file_response_if_not_overridden()
        {
            // Given
            var environment = new DefaultNancyEnvironment();

            environment.StaticContent(safepaths: this.GetLocation());

            var filename = this.GetFilePath();
            var response = new GenericFileResponse(filename, "foo/bar", new NancyContext()
            {
                Environment = environment
            });

            // When
            var result = response.AsAttachment();

            // Then
            result.Headers["Content-Disposition"].ShouldContain(Path.GetFileName(filename));
            result.ContentType.ShouldEqual("foo/bar");
        }
예제 #19
0
        public void Should_use_filename_and_content_type_for_attachments_from_file_response_if_not_overridden()
        {
            // Given
            var assemblyPath =
                Path.GetDirectoryName(this.GetType().Assembly.Location);

            var environment = new DefaultNancyEnvironment();

            environment.StaticContent(safepaths: assemblyPath);

            var filename = Path.GetFileName(this.GetType().Assembly.Location);
            var response = new GenericFileResponse(filename, "image/png", new NancyContext()
            {
                Environment = environment
            });

            // When
            var result = response.AsAttachment();

            // Then
            result.Headers["Content-Disposition"].ShouldContain(filename);
            result.ContentType.ShouldEqual("image/png");
        }
        public TrackingModule()
        {
            Get["/gif/{id}"] = _ =>
            {
                var query = this.Bind <TicketQueryById>();

                Tickets.LogAccess(query.Id, Request.UserHostAddress);

                var filename = @"Content\transparent_1x1.gif";
                var response = new GenericFileResponse(filename)
                {
                    ContentType = MimeTypes.GetMimeType(filename)
                };

                return(response);
            };

            Get["/gif/search/{name}"] = _ =>
            {
                var query = this.Bind <TicketQueryByName>();

                var result = new TicketQueryMultiResults
                {
                    Query   = query,
                    Results = Tickets.Find(query.Name).ToList()
                };

                return(result);
            };

            Get["/gif/{id}/details"] = _ =>
            {
                var query = this.Bind <TicketQueryById>();

                object result;

                var ticket = Tickets.Find(query.Id);

                if (ticket == null)
                {
                    result = HttpStatusCode.NotFound;
                }
                else
                {
                    result = new TicketQuerySingleResult
                    {
                        Query  = query,
                        Result = ticket
                    };
                }


                return(result);
            };

            Post["/gif/generate"] = _ =>
            {
                var input = this.Bind <GenerateTicketInputModel>();

                var ticket = Tickets.CreateTicket(input.Name);

                return(Response.AsRedirect($"/gif/{ticket.Id}/details"));
            };
        }