private static Func<NancyContext, string, Response> AddStaticResourcePath(string staticPath, Assembly assembly, string namespacePrefix) { return (context, s) => { var path = context.Request.Path; var name = path.Replace('/', '.').TrimStart('.'); var resourcePath = namespacePrefix + "." + staticPath; // Important to return null so that it passes through to the other resource providers if (!assembly.GetManifestResourceNames().Contains(resourcePath + "." + name)) { return null; } var response = new EmbeddedFileResponse(assembly, resourcePath, name); // Check ETag values to generate 304NotModified string currentFileEtag; if (response.Headers.TryGetValue("ETag", out currentFileEtag)) { if (context.Request.Headers.IfNoneMatch.Contains(currentFileEtag)) { return new Response {StatusCode = HttpStatusCode.NotModified, ContentType = response.ContentType}; } } return response; }; }
public void Should_not_contain_etag_in_response_header_if_embedded_resource_does_not_exists() { // Given, when var response = new EmbeddedFileResponse(this.GetType().Assembly, "Nancy.Tests", "i_dont_exist.jpg"); // Then response.Headers.ContainsKey("ETag").ShouldBeFalse(); }
public void Should_contain_etag_in_response_header_if_embedded_resource_exists() { // Given, when var response = new EmbeddedFileResponse(this.GetType().Assembly, "Nancy.Tests", "Resources.Views.staticviewresource.html"); // Then response.Headers["ETag"].ShouldEqual("\"B9D9DC2B50ADFD0867749D4837C63556339080CE\""); }
public void Should_contain_etag_in_response_header_if_embedded_resource_exists() { // Given, when var response = new EmbeddedFileResponse(this.GetType().Assembly, "Nancy.Tests", "Resources.Views.staticviewresource.html"); // Then response.Headers["ETag"].ShouldEqual("\"5D6EFDFDB135DC90F16D57E05603DA1E\""); }
public Stream GetBodyStream(string name) { var view = new EmbeddedFileResponse(typeof(SugarTownViewRenderer).Assembly, "SugarTown.Views.Posts", name); var stream = new MemoryStream(); view.Contents.Invoke(stream); stream.Position = 0; return stream; }
public void Should_not_contain_etag_in_response_header_if_embedded_resource_does_not_exists_when_invoking() { // Given var response = new EmbeddedFileResponse(this.GetType().Assembly, "Nancy.Tests", "i_dont_exist.jpg"); var outputStream = new MemoryStream(); // when response.Contents.Invoke(outputStream); // Then response.Headers.ContainsKey("ETag").ShouldBeFalse(); }
public void Should_contain_etag_in_response_header_if_embedded_resource_exists_when_invoking() { // Given var response = new EmbeddedFileResponse(this.GetType().Assembly, "Nancy.Tests", "Resources.Views.staticviewresource.html"); var outputStream = new MemoryStream(); // when response.Contents.Invoke(outputStream); // Then response.Headers["ETag"].ShouldEqual("\"B9D9DC2B50ADFD0867749D4837C63556339080CE\""); }
public void Should_contain_etag_in_response_header_if_embedded_resource_exists_when_invoking() { // Given var response = new EmbeddedFileResponse(this.GetType().Assembly, "Nancy.Tests", "Resources.Views.staticviewresource.html"); var outputStream = new MemoryStream(); // when response.Contents.Invoke(outputStream); // Then response.Headers["ETag"].ShouldEqual("\"5D6EFDFDB135DC90F16D57E05603DA1E\""); }