Java Code Examples for com.linecorp.armeria.server.ServiceRequestContext#of()
The following examples show how to use
com.linecorp.armeria.server.ServiceRequestContext#of() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestContextExporterTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void shouldRepopulateWhenAttributeChanges() { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); final RequestContextExporter exporter = RequestContextExporter.builder() .addAttribute("attrs.attr1", ATTR1) .build(); assertThat(exporter.export(ctx)).doesNotContainKeys("attrs.attr1"); ctx.setAttr(ATTR1, "foo"); assertThat(exporter.export(ctx)).containsEntry("attrs.attr1", "foo"); ctx.setAttr(ATTR1, "bar"); assertThat(exporter.export(ctx)).containsEntry("attrs.attr1", "bar"); ctx.setAttr(ATTR1, null); assertThat(exporter.export(ctx)).doesNotContainKeys("attrs.attr1"); }
Example 2
Source File: DefaultRequestLogTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void logNameWithDeferredRequestContent_beforeEndRequest() { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); final DefaultRequestLog log = (DefaultRequestLog) ctx.log(); log.deferRequestContent(); assertThat(log.isAvailable(RequestLogProperty.NAME)).isFalse(); log.requestContent(RpcRequest.of(DefaultRequestLogTest.class, "test"), null); assertThat(log.whenRequestComplete()).isNotDone(); log.endRequest(); assertThat(log.name()).isSameAs("test"); await().untilAsserted(() -> { assertThat(log.whenRequestComplete()).isDone(); }); }
Example 3
Source File: RequestContextExporterTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void shouldUseOwnAttrToStoreInternalState() { final HttpRequest req = HttpRequest.of(HttpMethod.GET, "/"); final ServiceRequestContext rootCtx = ServiceRequestContext.of(req); final RequestContextExporter exporter = RequestContextExporter.builder().build(); // Create an internal state. exporter.export(rootCtx); final Object rootState = rootCtx.attr(RequestContextExporter.STATE); assertThat(rootState).isNotNull(); // Create a child context. final ClientRequestContext childCtx; try (SafeCloseable unused = rootCtx.push()) { childCtx = ClientRequestContext.of(req); } assertThat(childCtx.root()).isSameAs(rootCtx); assertThat(childCtx.attr(RequestContextExporter.STATE)).isSameAs(rootState); assertThat(childCtx.ownAttr(RequestContextExporter.STATE)).isNull(); // Make sure a new internal state object is created. exporter.export(childCtx); final Object childState = childCtx.attr(RequestContextExporter.STATE); assertThat(childState).isNotNull().isNotSameAs(rootState); }
Example 4
Source File: RequestContextExporterTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void customExportKey() { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); ctx.setAttr(ATTR1, "1"); ctx.setAttr(ATTR3, new Foo("foo")); final RequestContextExporter exporter = RequestContextExporter .builder() .addAttribute("attrs.attr1", ATTR1) .addAttribute("my_attr2", ATTR1) .addRequestHeader(HttpHeaderNames.METHOD, "request_method") .addKeyPattern("request_id=req.id") .addKeyPattern("foo=attr:" + Foo.class.getName() + "#ATTR3") .addKeyPattern("bar=attr:" + Foo.class.getName() + "#ATTR3:" + FooStringifier.class.getName()) .build(); final Map<String, String> export; try (SafeCloseable ignored = ctx.push()) { export = exporter.export(); } assertThat(export).containsOnlyKeys("request_id", "request_method", "attrs.attr1", "my_attr2", "foo", "bar"); }
Example 5
Source File: RetryingClientWithContextAwareTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void contextAwareDoesNotThrowException() { final WebClient client = WebClient.builder(server.httpUri()) .responseTimeoutMillis(100) .decorator(RetryingClient.builder(RetryRule.failsafe()) .maxTotalAttempts(2) .newDecorator()) .build(); final ServiceRequestContext dummyCtx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); try (SafeCloseable ignored = dummyCtx.push()) { final CompletableFuture<AggregatedHttpResponse> future = client.get("/").aggregate(); assertThatThrownBy(() -> dummyCtx.makeContextAware(future).join()).hasCauseInstanceOf( ResponseTimeoutException.class); } }
Example 6
Source File: LoggingServiceTest.java From armeria with Apache License 2.0 | 6 votes |
@Test void infoLevel() throws Exception { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); ctx.logBuilder().responseHeaders(ResponseHeaders.of(200)); final Logger logger = LoggingTestUtil.newMockLogger(ctx, capturedCause); when(logger.isInfoEnabled()).thenReturn(true); final LoggingService service = LoggingService.builder() .logger(logger) .requestLogLevel(LogLevel.INFO) .successfulResponseLogLevel(LogLevel.INFO) .newDecorator().apply(delegate); service.serve(ctx, ctx.request()); verify(logger).info(eq(REQUEST_FORMAT), same(ctx), matches(".*headers=\\[:method=GET, :path=/].*")); verify(logger).info(eq(RESPONSE_FORMAT), same(ctx), matches(".*headers=\\[:status=200].*")); }
Example 7
Source File: LoggingServiceTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void defaultsSuccess() throws Exception { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); final Logger logger = LoggingTestUtil.newMockLogger(ctx, capturedCause); final LoggingService service = LoggingService.builder() .logger(logger) .newDecorator().apply(delegate); service.serve(ctx, ctx.request()); verify(logger, times(2)).isTraceEnabled(); }
Example 8
Source File: DefaultRequestLogTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void logNameWithRequestContent() { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); final DefaultRequestLog log = (DefaultRequestLog) ctx.log(); assertThat(log.isAvailable(RequestLogProperty.NAME)).isFalse(); log.requestContent(RpcRequest.of(DefaultRequestLogTest.class, "test"), null); log.endRequest(); assertThat(log.name()).isSameAs("test"); }
Example 9
Source File: MeterIdPrefixFunctionTest.java From armeria with Apache License 2.0 | 5 votes |
private static ServiceRequestContext newContext(HttpMethod method, String path, @Nullable Object requestContent) { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(method, path)); ctx.logBuilder().requestContent(requestContent, null); ctx.logBuilder().endRequest(); return ctx; }
Example 10
Source File: DefaultAttributeMapTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void hasNoAttributeInRoot() { final ServiceRequestContext root = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); final DefaultAttributeMap child = new DefaultAttributeMap(root); final AttributeKey<String> foo = AttributeKey.valueOf("foo"); // root: [foo], child: [] child.setAttr(foo, "foo"); assertThat(root.attr(foo)).isNull(); assertThat(child.attr(foo)).isEqualTo("foo"); final AttributeKey<String> bar = AttributeKey.valueOf("bar"); // root: [foo], child: [bar] assertThat(child.setAttrIfAbsent(bar, "bar")).isNull(); assertThat(root.attr(bar)).isNull(); assertThat(child.attr(bar)).isEqualTo("bar"); // Do not change. // root: [foo], child: [bar] assertThat(child.setAttrIfAbsent(bar, "bar2")).isEqualTo("bar"); assertThat(child.attr(bar)).isEqualTo("bar"); final AttributeKey<String> baz = AttributeKey.valueOf("baz"); // root: [foo], child: [bar, baz] assertThat(child.computeAttrIfAbsent(baz, key -> "baz")).isEqualTo("baz"); assertThat(root.attr(baz)).isNull(); assertThat(child.attr(baz)).isEqualTo("baz"); // Do not change. // root: [foo], child: [bar, baz] assertThat(child.computeAttrIfAbsent(baz, key -> "baz2")).isEqualTo("baz"); assertThat(child.attr(baz)).isEqualTo("baz"); }
Example 11
Source File: LoggingServiceTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void mapRequestLogLevelMapper() throws Exception { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(RequestHeaders.of( HttpMethod.GET, "/", "x-req", "test", "x-res", "test"))); final Logger logger = LoggingTestUtil.newMockLogger(ctx, capturedCause); when(logger.isWarnEnabled()).thenReturn(true); final LoggingService service = LoggingService.builder() .logger(logger) .requestLogLevelMapper(log -> { if (log.requestHeaders().contains("x-req")) { return LogLevel.WARN; } else { return LogLevel.INFO; } }) .responseLogLevelMapper(log -> { if (log.requestHeaders().contains("x-res")) { return LogLevel.WARN; } else { return LogLevel.INFO; } }) .newDecorator().apply(delegate); // Check if logs at WARN level if there are headers we're looking for. service.serve(ctx, ctx.request()); verify(logger, never()).isInfoEnabled(); verify(logger, times(2)).isWarnEnabled(); verify(logger).warn(eq(REQUEST_FORMAT), same(ctx), matches(".*headers=\\[:method=GET, :path=/, x-req=test, x-res=test].*")); verify(logger).warn(eq(RESPONSE_FORMAT), same(ctx), anyString()); }
Example 12
Source File: ServerRequestContextAdapterTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void statusCode() { final HttpRequest req = HttpRequest.of(HttpMethod.GET, "/"); final ServiceRequestContext ctx = ServiceRequestContext.of(req); ctx.logBuilder().endRequest(); ctx.logBuilder().responseHeaders(ResponseHeaders.of(HttpStatus.OK)); ctx.logBuilder().endResponse(); final HttpServerResponse res = ServiceRequestContextAdapter.asHttpServerResponse(ctx.log().ensureComplete(), null); assertThat(res.statusCode()).isEqualTo(200); }
Example 13
Source File: AccessLogFormatsTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void requestLogWithEmptyCause() { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); final RequestLogBuilder logBuilder = ctx.logBuilder(); final List<AccessLogComponent> format = AccessLogFormats.parseCustom("%{requestCause}L %{responseCause}L"); logBuilder.endRequest(); logBuilder.endResponse(); assertThat(AccessLogger.format(format, ctx.log().ensureComplete())).isEqualTo("- -"); }
Example 14
Source File: DefaultClientRequestContextTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void attrsDoNotIterateRootWhenKeyIsSame() { final HttpRequest req = HttpRequest.of(HttpMethod.GET, "/"); final ServiceRequestContext serviceContext = ServiceRequestContext.of(req); try (SafeCloseable ignored = serviceContext.push()) { final ClientRequestContext clientContext = ClientRequestContext.of(req); final AttributeKey<String> fooKey = AttributeKey.valueOf(DefaultClientRequestContextTest.class, "foo"); clientContext.setAttr(fooKey, "foo"); serviceContext.setAttr(fooKey, "bar"); final Iterator<Entry<AttributeKey<?>, Object>> attrs = clientContext.attrs(); assertThat(attrs.next().getValue()).isEqualTo("foo"); assertThat(attrs.hasNext()).isFalse(); } }
Example 15
Source File: RequestContextExporterTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void shouldNotExportNullValue() { final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); ctx.setAttr(ATTR1, "1"); ctx.setAttr(ATTR2, null); ctx.logBuilder().endRequest(); ctx.logBuilder().endResponse(); final RequestContextExporter exporter = RequestContextExporter.builder() .addKeyPattern("*") .addAttribute("attrs.attr1", ATTR1) .addAttribute("attrs.attr2", ATTR2) .build(); assertThat(exporter.export(ctx)).containsOnlyKeys( BuiltInProperty.CLIENT_IP.key, BuiltInProperty.ELAPSED_NANOS.key, BuiltInProperty.LOCAL_HOST.key, BuiltInProperty.LOCAL_IP.key, BuiltInProperty.LOCAL_PORT.key, BuiltInProperty.REMOTE_HOST.key, BuiltInProperty.REMOTE_IP.key, BuiltInProperty.REMOTE_PORT.key, BuiltInProperty.REQ_NAME.key, BuiltInProperty.REQ_SERVICE_NAME.key, BuiltInProperty.REQ_AUTHORITY.key, BuiltInProperty.REQ_CONTENT_LENGTH.key, BuiltInProperty.REQ_DIRECTION.key, BuiltInProperty.REQ_METHOD.key, BuiltInProperty.REQ_PATH.key, BuiltInProperty.RES_CONTENT_LENGTH.key, BuiltInProperty.RES_STATUS_CODE.key, BuiltInProperty.REQ_ID.key, BuiltInProperty.SCHEME.key, "attrs.attr1"); }
Example 16
Source File: FramedGrpcServiceTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void missingContentType() throws Exception { final HttpRequest req = HttpRequest.of(HttpMethod.POST, "/grpc.testing.TestService.UnaryCall"); final ServiceRequestContext ctx = ServiceRequestContext.of(req); final HttpResponse response = grpcService.doPost(ctx, PooledHttpRequest.of(req)); assertThat(response.aggregate().get()).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE, HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8, HttpHeaderNames.CONTENT_LENGTH, 39), HttpData.ofUtf8("Missing or invalid Content-Type header."))); }
Example 17
Source File: RequestContextTest.java From armeria with Apache License 2.0 | 4 votes |
private static ServiceRequestContext createContext() { return ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); }
Example 18
Source File: ClientRequestContextTest.java From armeria with Apache License 2.0 | 4 votes |
private static ServiceRequestContext serviceRequestContext() { return ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); }
Example 19
Source File: DefaultAttributeMapTest.java From armeria with Apache License 2.0 | 4 votes |
@Test void attrsWithRoot() { final ServiceRequestContext root = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); final DefaultAttributeMap child = new DefaultAttributeMap(root); final AttributeKey<String> foo = AttributeKey.valueOf("foo"); // root: [foo], child: [] root.setAttr(foo, "foo"); Iterator<Entry<AttributeKey<?>, Object>> childIt = child.attrs(); final Entry<AttributeKey<?>, Object> next = childIt.next(); assertThat(next.getValue()).isEqualTo("foo"); assertThat(childIt.hasNext()).isFalse(); assertThatThrownBy(childIt::next).isInstanceOf(NoSuchElementException.class); // root: [foo], child: [foo1] next.setValue("foo1"); assertThat(child.attr(foo)).isEqualTo("foo1"); assertThat(child.ownAttr(foo)).isEqualTo("foo1"); // The value of entry is changed. assertThat(next.getValue()).isEqualTo("foo1"); // root attribute remains unaffected. assertThat(root.attr(foo)).isEqualTo("foo"); // Set a new attribute to child. final AttributeKey<String> bar = AttributeKey.valueOf("bar"); // root: [foo], child: [foo1, bar] child.setAttr(bar, "bar"); childIt = child.attrs(); final List<String> attributeValues = new ArrayList<>(2); Entry<AttributeKey<?>, Object> barEntry = null; for (int i = 0; i < 2; i++) { final Entry<AttributeKey<?>, Object> tempEntry = childIt.next(); attributeValues.add((String) tempEntry.getValue()); if ("bar".equals(tempEntry.getValue())) { barEntry = tempEntry; } } assertThat(childIt.hasNext()).isFalse(); assertThat(attributeValues).containsExactlyInAnyOrder("foo1", "bar"); assertThat(barEntry).isNotNull(); // root: [foo], child: [foo1, bar1] barEntry.setValue("bar1"); assertThat(child.attr(bar)).isEqualTo("bar1"); // Set a new attribute to root. final AttributeKey<String> baz = AttributeKey.valueOf("baz"); // root: [foo, baz], child: [foo1, bar1] root.setAttr(baz, "baz"); childIt = child.attrs(); attributeValues.clear(); attributeValues.add((String) childIt.next().getValue()); attributeValues.add((String) childIt.next().getValue()); assertThat(attributeValues).containsExactlyInAnyOrder("foo1", "bar1"); assertThat(childIt.next().getValue()).isEqualTo("baz"); // childIt does not yield foo in root. assertThat(childIt.hasNext()).isFalse(); // child own attrs() attributeValues.clear(); final Iterator<Entry<AttributeKey<?>, Object>> childOwnIt = child.ownAttrs(); for (int i = 0; i < 2; i++) { attributeValues.add((String) childOwnIt.next().getValue()); } assertThat(childOwnIt.hasNext()).isFalse(); assertThat(attributeValues).containsExactlyInAnyOrder("foo1", "bar1"); }
Example 20
Source File: DefaultAttributeMapTest.java From armeria with Apache License 2.0 | 4 votes |
@Test void hasAttributeInRoot() { final ServiceRequestContext root = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/")); final DefaultAttributeMap child = new DefaultAttributeMap(root); // root: [foo], child: [] final AttributeKey<String> foo = AttributeKey.valueOf("foo"); root.setAttr(foo, "foo"); assertThat(root.attr(foo)).isEqualTo("foo"); assertThat(child.attr(foo)).isEqualTo("foo"); assertThat(child.ownAttr(foo)).isNull(); // root: [foo], child: [foo2] child.setAttr(foo, "foo2"); assertThat(child.ownAttr(foo)).isEqualTo("foo2"); assertThat(child.attr(foo)).isEqualTo("foo2"); assertThat(root.attr(foo)).isEqualTo("foo"); final AttributeKey<String> bar = AttributeKey.valueOf("bar"); // root: [foo, bar], child: [foo2] root.setAttr(bar, "bar"); // root: [foo, bar], child: [foo2, bar2] assertThat(child.setAttrIfAbsent(bar, "bar2")).isNull(); assertThat(child.attr(bar)).isEqualTo("bar2"); assertThat(root.attr(bar)).isEqualTo("bar"); // Do not change. // root: [foo, bar], child: [foo2, bar2] assertThat(child.setAttrIfAbsent(bar, "bar3")).isEqualTo("bar2"); assertThat(child.attr(bar)).isEqualTo("bar2"); final AttributeKey<String> baz = AttributeKey.valueOf("baz"); // root: [foo, bar, baz], child: [foo2, bar2] root.setAttr(baz, "baz"); // root: [foo, bar, baz], child: [foo2, bar2, baz2] assertThat(child.computeAttrIfAbsent(baz, key -> "baz2")).isEqualTo("baz2"); assertThat(child.attr(baz)).isEqualTo("baz2"); assertThat(root.attr(baz)).isEqualTo("baz"); // Do not change. // root: [foo, bar, baz], child: [foo2, bar2, baz2] assertThat(child.computeAttrIfAbsent(baz, key -> "baz3")).isEqualTo("baz2"); assertThat(child.attr(baz)).isEqualTo("baz2"); }