package scoop.tests.http.server.consumer import scoop.asyncio.* import scoop.http.* import scoop.http.asyncio.* import scoop.http.server.* import scoop.platform.env.* import scoop.platform.fs.* import scoop.platform.net.* import scoop.test.* @Test private class HttpServerFileConsumerTests { @Test fun `published file and static handlers support ranges and conditional responses`() { val parent = when (environmentVariable("TMPDIR")) { case Some(val value) -> Path.fromOsString(value); case None -> currentDirectory() } val directory = createTempDirectory(Some(parent), "scoop-file-consumer-") try { val path = directory.resolve(Path.fromString("data.txt")); path.writeText("published file") val routes = routing { this.get( "/single", suspend { _ -> file( path, fileResponseOptions { this.entityTag = Some(HttpEntityTag.parse("\"v1\"")) } ) } ) this.get( "/assets/{*asset}", staticFiles(directory, staticFileOptions { this.pathParameter = "asset" }) ) } val loop = eventLoop() loop.run { val scope = this; val listener = loop.listenTcp(SocketAddress(IpAddress.IPV4_LOOPBACK, 0)) val address = listener.localAddress HttpServer.takeOwnership(listener, routes, scope).useAsync suspend { _ -> AsyncHttp1Connection.takeOwnershipAsClient( loop.connectTcp(address), scope ).useAsync suspend { client -> val range = HttpRequestHead( HttpVersion.Http11, "GET".toByteArray(), HttpRequestTarget.Http1("/assets/data.txt".toByteArray()), [ HttpField("Host".toByteArray(), "localhost".toByteArray()), HttpField("Range".toByteArray(), "bytes=0-8".toByteArray()) ] ) client.sendRequest(range).finish() val partial = client.receiveResponse() assertEquals(206U, partial.head.status) assertEquals("published", consumerResponseText(partial.body)) val condition = HttpRequestHead( HttpVersion.Http11, "GET".toByteArray(), HttpRequestTarget.Http1("/single".toByteArray()), [ HttpField("Host".toByteArray(), "localhost".toByteArray()), HttpEntityTagCondition.wildcard().toIfNoneMatchField() ] ) client.sendRequest(condition).finish() val cached = client.receiveResponse() assertEquals(304U, cached.head.status); assertEquals("", consumerResponseText(cached.body)) } } } } finally { directory.deleteRecursively() } } }