package scoop.http.client import scoop.asyncio.* import scoop.http.* import scoop.http.asyncio.* import scoop.platform.time.MonotonicTimeSource import scoop.test.* import scoop.time.Duration import scoop.tls.TlsTrustSource @Test private class HttpRequestObservationTest { @Test fun `request observation reuses an unobserved connection and separates informational heads`() { clientTestTrust().use { trust -> eventLoop().run { val scope = this; clientTestListen(currentEventLoop()).useAsync suspend { listener -> val serving = scope.launch suspend { AsyncHttp1Connection.takeOwnershipAsServer( listener.accept(), scope ).useAsync suspend { connection -> for (index in 0L ..< 3L) { connection.receiveRequest()!!.body.readAllBytes(0L) if (index == 1L) connection.sendResponse( HttpResponseHead(HttpVersion.Http11, 103U) ).finish() sendClientTestText(connection, 200U, "response") } } } withHttpClient(clientTestConfig(trust)) suspend { client -> val url = clientTestUrl(listener) client.withResponse(httpRequest(url)) suspend { response -> assertFalse(clientPresent(response.metrics)) response.body.text(32L) Unit } val log = RequestObservationLog() val options = httpCallOptions { this.observer = Some(log.observer) this.onInformational = suspend { _ -> val headers = log.ofKind(HttpCallEventKind.ResponseHeaders) assertEquals(1L, headers.size) assertTrue(headers[0L].metrics.responseHeadersAtUtc != None) assertTrue( headers[0L].metrics.finalResponseHeadersAtUtc == None ) assertTrue( headers[0L].metrics.finalResponseHeadersAt == None ) } } val response = client.execute(httpRequest(url) { this.options = options }) val initial = response.metrics!! try { assertTrue(initial.connectionReused == Some(true)) assertTrue(initial.finishedAtUtc == None) assertEquals("response", response.body.text(32L)) } finally { response.close() } val final = response.metrics!! checkRequestObservation(final) assertEquals(initial.requestHeadersAtUtc, final.requestHeadersAtUtc) assertEquals( initial.finalResponseHeadersAtUtc, final.finalResponseHeadersAtUtc ) assertEquals(final.finishedAtUtc, log.completed().finishedAtUtc) assertTrue(initial.finishedAtUtc == None) assertEquals(2L, log.ofKind(HttpCallEventKind.ResponseHeaders).size) val eventCount = log.events.size client.withResponse(httpRequest(url)) suspend { next -> assertFalse(clientPresent(next.metrics)) next.body.text(32L) Unit } assertEquals(eventCount, log.events.size) } serving.join() } } } } @Test fun `request and client observers share snapshots and deduplicate identical observers`() { clientTestTrust().use { trust -> eventLoop().run { val scope = this; clientTestListen(currentEventLoop()).useAsync suspend { listener -> val serving = scope.launch suspend { AsyncHttp1Connection.takeOwnershipAsServer( listener.accept(), scope ).useAsync suspend { connection -> for (index in 0L ..< 2L) { connection.receiveRequest()!!.body.readAllBytes(0L) sendClientTestText(connection, 200U, "ok") } } } val local = RequestObservationLog() val global = RequestObservationLog() val clientObserver = HttpObserver( suspend { event -> global.events.add(event) if (event.kind == HttpCallEventKind.ResponseHeaders) { val sampled = event.metrics.finalResponseHeadersAt!! currentEventLoop().sleep(Duration.milliseconds(2L)) assertTrue( MonotonicTimeSource.markNow() - sampled >= Duration.milliseconds(2L) ) } } ) val config = httpClientConfig { this.tls = httpClientTlsConfig { this.trust = TlsTrustSource.Store(trust) } this.observer = Some(clientObserver) } withHttpClient(config) suspend { client -> client.withResponse( httpRequest(clientTestUrl(listener)) { this.options = httpCallOptions { this.observer = Some(local.observer) } } ) suspend { response -> response.body.text(32L) Unit } assertEquals(local.events.size, global.events.size) for (index in 0L ..< local.events.size) assertSame(local.events[index], global.events[index]) checkRequestObservation(local.completed()) val count = global.ofKind(HttpCallEventKind.Completed).size client.withResponse( httpRequest(clientTestUrl(listener)) { this.options = httpCallOptions { this.observer = Some(clientObserver) } } ) suspend { response -> response.body.text(32L) Unit } assertEquals(count + 1L, global.ofKind(HttpCallEventKind.Completed).size) } serving.join() } } } } }