|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.metrics.web.client;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.net.URI;
|
20 | 21 | import java.net.URISyntaxException;
|
21 | 22 |
|
|
29 | 30 |
|
30 | 31 | import org.springframework.boot.actuate.metrics.AutoTimer;
|
31 | 32 | import org.springframework.http.HttpMethod;
|
| 33 | +import org.springframework.http.HttpRequest; |
32 | 34 | import org.springframework.http.MediaType;
|
| 35 | +import org.springframework.http.client.ClientHttpRequestExecution; |
| 36 | +import org.springframework.http.client.ClientHttpRequestInterceptor; |
| 37 | +import org.springframework.http.client.ClientHttpResponse; |
33 | 38 | import org.springframework.test.web.client.MockRestServiceServer;
|
34 | 39 | import org.springframework.test.web.client.match.MockRestRequestMatchers;
|
35 | 40 | import org.springframework.test.web.client.response.MockRestResponseCreators;
|
@@ -107,4 +112,45 @@ void interceptRestTemplateWithUri() throws URISyntaxException {
|
107 | 112 | this.mockServer.verify();
|
108 | 113 | }
|
109 | 114 |
|
| 115 | + @Test |
| 116 | + void interceptNestedRequest() { |
| 117 | + this.mockServer.expect(MockRestRequestMatchers.requestTo("/test/123")) |
| 118 | + .andExpect(MockRestRequestMatchers.method(HttpMethod.GET)) |
| 119 | + .andRespond(MockRestResponseCreators.withSuccess("OK", MediaType.APPLICATION_JSON)); |
| 120 | + |
| 121 | + RestTemplate nestedRestTemplate = new RestTemplate(); |
| 122 | + MockRestServiceServer nestedMockServer = MockRestServiceServer.createServer(nestedRestTemplate); |
| 123 | + nestedMockServer.expect(MockRestRequestMatchers.requestTo("/nestedTest/124")) |
| 124 | + .andExpect(MockRestRequestMatchers.method(HttpMethod.GET)) |
| 125 | + .andRespond(MockRestResponseCreators.withSuccess("OK", MediaType.APPLICATION_JSON)); |
| 126 | + this.customizer.customize(nestedRestTemplate); |
| 127 | + |
| 128 | + TestInterceptor testInterceptor = new TestInterceptor(nestedRestTemplate); |
| 129 | + this.restTemplate.getInterceptors().add(testInterceptor); |
| 130 | + |
| 131 | + this.restTemplate.getForObject("/test/{id}", String.class, 123); |
| 132 | + this.registry.get("http.client.requests").tags("uri", "/test/{id}").timer(); |
| 133 | + this.registry.get("http.client.requests").tags("uri", "/nestedTest/{nestedId}").timer(); |
| 134 | + |
| 135 | + this.mockServer.verify(); |
| 136 | + nestedMockServer.verify(); |
| 137 | + } |
| 138 | + |
| 139 | + private static final class TestInterceptor implements ClientHttpRequestInterceptor { |
| 140 | + |
| 141 | + private final RestTemplate restTemplate; |
| 142 | + |
| 143 | + private TestInterceptor(RestTemplate restTemplate) { |
| 144 | + this.restTemplate = restTemplate; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) |
| 149 | + throws IOException { |
| 150 | + this.restTemplate.getForObject("/nestedTest/{nestedId}", String.class, 124); |
| 151 | + return execution.execute(request, body); |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
110 | 156 | }
|
0 commit comments