@InternalApi("Visible for testing") static SpanName getSpanName(@Nonnull ApiMethodDescriptor<?, ?> methodDescriptor) { Matcher matcher = FULL_METHOD_NAME_REGEX.matcher(methodDescriptor.getFullMethodName()); Preconditions.checkArgument(matcher.matches(), "Invalid fullMethodName"); return SpanName.of(matcher.group(1), matcher.group(2)); } }
@Override public MockLowLevelHttpResponse getHttpResponse(String httpMethod, String fullTargetUrl) { MockLowLevelHttpResponse httpResponse = new MockLowLevelHttpResponse(); Preconditions.checkArgument( serviceMethodDescriptors != null, "MockHttpService has null serviceMethodDescriptors."); String relativePath = getRelativePath(fullTargetUrl); for (ApiMethodDescriptor methodDescriptor : serviceMethodDescriptors) { if (!httpMethod.equals(methodDescriptor.getHttpMethod())) { continue; } PathTemplate pathTemplate = methodDescriptor.getRequestFormatter().getPathTemplate(); // Server figures out which RPC method is called based on the endpoint path pattern. if (!pathTemplate.matches(relativePath)) { continue; } // Emulate the server's creation of an HttpResponse from the response message instance. String httpContent = methodDescriptor.getResponseParser().serialize(response); httpResponse.setContent(httpContent.getBytes()); httpResponse.setStatusCode(200); return httpResponse; } // Return 404 when none of this server's endpoint templates match the given URL. httpResponse.setContent( String.format("Method not found for path '%s'", relativePath).getBytes()); httpResponse.setStatusCode(404); return httpResponse; } });
getApiMethodDescriptor().getRequestFormatter(); requestFactory.buildRequest(getApiMethodDescriptor().getHttpMethod(), url, jsonHttpContent); for (HttpJsonHeaderEnhancer enhancer : getHeaderEnhancers()) { enhancer.enhance(httpRequest.getHeaders());
@Override public void run() { try { HttpRequest httpRequest = createHttpRequest(); HttpResponse httpResponse = httpRequest.execute(); if (!httpResponse.isSuccessStatusCode()) { ApiExceptionFactory.createException( null, HttpJsonStatusCode.of(httpResponse.getStatusCode(), httpResponse.getStatusMessage()), false); } if (getApiMethodDescriptor().getResponseParser() != null) { ResponseT response = getApiMethodDescriptor().getResponseParser().parse(httpResponse.getContent()); getResponseFuture().set(response); } else { getResponseFuture().set(null); } } catch (Exception e) { getResponseFuture().setException(e); } }
@Test public void testGetSpanName() { Map<String, SpanName> validNames = ImmutableMap.of( "compute.projects.disableXpnHost", SpanName.of("compute.projects", "disableXpnHost"), "client.method", SpanName.of("client", "method")); for (Entry<String, SpanName> entry : validNames.entrySet()) { @SuppressWarnings("unchecked") ApiMethodDescriptor descriptor = ApiMethodDescriptor.newBuilder() .setFullMethodName(entry.getKey()) .setHttpMethod(HttpMethods.POST) .setRequestFormatter(Mockito.mock(HttpRequestFormatter.class)) .setResponseParser(Mockito.mock(HttpResponseParser.class)) .build(); SpanName actualSpanName = HttpJsonCallableFactory.getSpanName(descriptor); assertThat(actualSpanName).isEqualTo(entry.getValue()); } }
getApiMethodDescriptor().getRequestFormatter(); requestFactory.buildRequest(getApiMethodDescriptor().getHttpMethod(), url, jsonHttpContent); for (HttpJsonHeaderEnhancer enhancer : getHeaderEnhancers()) { enhancer.enhance(httpRequest.getHeaders());
@Override public void run() { try { HttpRequest httpRequest = createHttpRequest(); HttpResponse httpResponse = httpRequest.execute(); if (!httpResponse.isSuccessStatusCode()) { ApiExceptionFactory.createException( null, HttpJsonStatusCode.of(httpResponse.getStatusCode(), httpResponse.getStatusMessage()), false); } if (getApiMethodDescriptor().getResponseParser() != null) { ResponseT response = getApiMethodDescriptor().getResponseParser().parse(httpResponse.getContent()); getResponseFuture().set(response); } else { getResponseFuture().set(null); } } catch (Exception e) { getResponseFuture().setException(e); } }
@Test public void testGetSpanNameInvalid() { List<String> invalidNames = ImmutableList.of("no_split", ".no_client"); for (String invalidName : invalidNames) { @SuppressWarnings("unchecked") ApiMethodDescriptor descriptor = ApiMethodDescriptor.newBuilder() .setFullMethodName(invalidName) .setHttpMethod(HttpMethods.POST) .setRequestFormatter(Mockito.mock(HttpRequestFormatter.class)) .setResponseParser(Mockito.mock(HttpResponseParser.class)) .build(); IllegalArgumentException actualError = null; try { SpanName spanName = HttpJsonCallableFactory.getSpanName(descriptor); assertWithMessage("Invalid method descriptor should not have a valid span name") .fail("%s should not generate the spanName: %s", invalidName, spanName); } catch (IllegalArgumentException e) { actualError = e; } assertThat(actualError).isNotNull(); } } }
@InternalApi("Visible for testing") static SpanName getSpanName(@Nonnull ApiMethodDescriptor<?, ?> methodDescriptor) { Matcher matcher = FULL_METHOD_NAME_REGEX.matcher(methodDescriptor.getFullMethodName()); Preconditions.checkArgument(matcher.matches(), "Invalid fullMethodName"); return SpanName.of(matcher.group(1), matcher.group(2)); } }
ApiMethodDescriptor.<CatMessage, Void>newBuilder() .setFullMethodName("house.cat.get") .setHttpMethod(null)
ApiMethodDescriptor.<InsertFrogRequest, Void>newBuilder() .setFullMethodName("house.details.get") .setHttpMethod(null)