fix: 외부 수집 요청 헤더 중복 제거
This commit is contained in:
parent
dcc3eba2e0
commit
55f56fb390
@ -4,6 +4,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.time.Duration;
|
||||
@ -17,7 +18,11 @@ public class RestTemplateConfig {
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.readTimeout(Duration.ofSeconds(120))
|
||||
.additionalInterceptors((request, body, execution) -> {
|
||||
request.getHeaders().add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
|
||||
if (!request.getHeaders().containsKey(HttpHeaders.USER_AGENT)) {
|
||||
request.getHeaders().set(HttpHeaders.USER_AGENT,
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
||||
+ "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
|
||||
}
|
||||
return execution.execute(request, body);
|
||||
})
|
||||
.build();
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
package com.hlab.yanalyst.global.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
class RestTemplateConfigTest {
|
||||
|
||||
@Test
|
||||
void preservesCallerUserAgentWithoutAddingDuplicateHeader() {
|
||||
RestTemplate restTemplate = new RestTemplateConfig().restTemplate(new RestTemplateBuilder());
|
||||
MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();
|
||||
server.expect(requestTo("https://example.com/feed"))
|
||||
.andExpect(request -> assertThat(request.getHeaders().get(HttpHeaders.USER_AGENT))
|
||||
.isEqualTo(List.of("social-feed-agent")))
|
||||
.andRespond(withSuccess("ok", MediaType.TEXT_PLAIN));
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set(HttpHeaders.USER_AGENT, "social-feed-agent");
|
||||
var response = restTemplate.exchange("https://example.com/feed", HttpMethod.GET,
|
||||
new HttpEntity<>(headers), String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
server.verify();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user