Here are the examples of the java api org.springframework.http.HttpHeaders.getFirst() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
196 Examples
19
Source : LogFilter.java
with GNU General Public License v3.0
from xuanbg
with GNU General Public License v3.0
from xuanbg
/**
* 获取客户端IP
*
* @param headers 请求头
* @return 客户端IP
*/
private String getIp(HttpHeaders headers) {
if (headers.isEmpty()) {
return null;
}
AtomicReference<String> ip = new AtomicReference<>(headers.getFirst("X-Real-IP"));
if (ip.get() == null || ip.get().isEmpty()) {
ip.set(headers.getFirst("X-Forwarded-For"));
}
if (ip.get() == null || ip.get().isEmpty()) {
ip.set(headers.getFirst("Proxy-Client-IP"));
}
if (ip.get() == null || ip.get().isEmpty()) {
ip.set(headers.getFirst("WL-Proxy-Client-IP"));
}
return ip.get();
}
19
Source : RestTemplateTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void postForLocationEnreplacedyCustomHeader() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockTextPlainHttpMessageConverter();
mockResponseStatus(HttpStatus.OK);
HttpHeaders responseHeaders = new HttpHeaders();
URI expected = new URI("https://example.com/hotels");
responseHeaders.setLocation(expected);
given(response.getHeaders()).willReturn(responseHeaders);
HttpHeaders enreplacedyHeaders = new HttpHeaders();
enreplacedyHeaders.set("MyHeader", "MyValue");
HttpEnreplacedy<String> enreplacedy = new HttpEnreplacedy<>("Hello World", enreplacedyHeaders);
URI result = template.postForLocation("https://example.com", enreplacedy);
replacedertEquals("Invalid POST result", expected, result);
replacedertEquals("No custom header set", "MyValue", requestHeaders.getFirst("MyHeader"));
verify(response).close();
}
19
Source : MockHttpServletRequest.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private void updateAcceptLanguageHeader() {
HttpHeaders headers = new HttpHeaders();
headers.setAcceptLanguageAsLocales(this.locales);
doAddHeaderValue(HttpHeaders.ACCEPT_LANGUAGE, headers.getFirst(HttpHeaders.ACCEPT_LANGUAGE), true);
}
19
Source : ProxyExchange.java
with Apache License 2.0
from spring-cloud
with Apache License 2.0
from spring-cloud
private void appendForwarded(URI uri) {
String forwarded = headers.getFirst("forwarded");
if (forwarded != null) {
forwarded = forwarded + ",";
} else {
forwarded = "";
}
forwarded = forwarded + forwarded(uri, exchange.getRequest().getHeaders().getFirst("host"));
headers.set("forwarded", forwarded);
}
19
Source : ProxyExchange.java
with Apache License 2.0
from spring-cloud
with Apache License 2.0
from spring-cloud
private void appendXForwarded(URI uri) {
// Append the legacy headers if they were already added upstream
String host = headers.getFirst("x-forwarded-host");
if (host == null) {
return;
}
host = host + "," + uri.getHost();
headers.set("x-forwarded-host", host);
String proto = headers.getFirst("x-forwarded-proto");
if (proto == null) {
return;
}
proto = proto + "," + uri.getScheme();
headers.set("x-forwarded-proto", proto);
}
19
Source : ProxyExchange.java
with Apache License 2.0
from spring-cloud
with Apache License 2.0
from spring-cloud
private void appendForwarded(URI uri) {
String forwarded = headers.getFirst("forwarded");
if (forwarded != null) {
forwarded = forwarded + ",";
} else {
forwarded = "";
}
forwarded = forwarded + forwarded(uri, webRequest.getHeader("host"));
headers.set("forwarded", forwarded);
}
19
Source : RestTemplateTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void postForLocationEnreplacedyCustomHeader() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockTextPlainHttpMessageConverter();
mockResponseStatus(HttpStatus.OK);
HttpHeaders responseHeaders = new HttpHeaders();
URI expected = new URI("https://example.com/hotels");
responseHeaders.setLocation(expected);
given(response.getHeaders()).willReturn(responseHeaders);
HttpHeaders enreplacedyHeaders = new HttpHeaders();
enreplacedyHeaders.set("MyHeader", "MyValue");
HttpEnreplacedy<String> enreplacedy = new HttpEnreplacedy<>("Hello World", enreplacedyHeaders);
URI result = template.postForLocation("https://example.com", enreplacedy);
replacedertThat(result).as("Invalid POST result").isEqualTo(expected);
replacedertThat(requestHeaders.getFirst("MyHeader")).as("No custom header set").isEqualTo("MyValue");
verify(response).close();
}
19
Source : RestTemplateTests.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@Test
public void postForLocationEnreplacedyCustomHeader() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "http://example.com", requestHeaders);
mockTextPlainHttpMessageConverter();
mockResponseStatus(HttpStatus.OK);
HttpHeaders responseHeaders = new HttpHeaders();
URI expected = new URI("http://example.com/hotels");
responseHeaders.setLocation(expected);
given(response.getHeaders()).willReturn(responseHeaders);
HttpHeaders enreplacedyHeaders = new HttpHeaders();
enreplacedyHeaders.set("MyHeader", "MyValue");
HttpEnreplacedy<String> enreplacedy = new HttpEnreplacedy<>("Hello World", enreplacedyHeaders);
URI result = template.postForLocation("http://example.com", enreplacedy);
replacedertEquals("Invalid POST result", expected, result);
replacedertEquals("No custom header set", "MyValue", requestHeaders.getFirst("MyHeader"));
verify(response).close();
}
19
Source : SpringCloudGatewayParamParserTest.java
with Apache License 2.0
from eacdy
with Apache License 2.0
from eacdy
private void mockSingleHeader(/*@Mock*/
ServerHttpRequest request, String key, String value) {
HttpHeaders headers = mock(HttpHeaders.clreplaced);
when(request.getHeaders()).thenReturn(headers);
when(headers.getFirst(key)).thenReturn(value);
}
19
Source : SpringCloudGatewayParamParserTest.java
with Apache License 2.0
from eacdy
with Apache License 2.0
from eacdy
private void mockHeaders(/*@Mock*/
ServerHttpRequest request, Map<String, String> headerMap) {
HttpHeaders headers = mock(HttpHeaders.clreplaced);
when(request.getHeaders()).thenReturn(headers);
for (Map.Entry<String, String> e : headerMap.entrySet()) {
when(headers.getFirst(e.getKey())).thenReturn(e.getValue());
}
}
18
Source : WebUtils.java
with GNU General Public License v3.0
from wehotel
with GNU General Public License v3.0
from wehotel
public static void request2stringBuilder(String reqId, HttpMethod method, String uri, HttpHeaders headers, Object body, StringBuilder b) {
b.append(reqId).append(Constants.Symbol.SPACE).append(method).append(Constants.Symbol.SPACE).append(uri);
if (headers != null) {
final boolean[] f = { false };
logHeaderSet.forEach(h -> {
String v = headers.getFirst(h);
if (v != null) {
if (!f[0]) {
b.append(Constants.Symbol.LINE_SEPARATOR);
f[0] = true;
}
Utils.addTo(b, h, Constants.Symbol.EQUAL, v, Constants.Symbol.TWO_SPACE_STR);
}
});
}
// body to b
}
18
Source : ResourceWebHandlerTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void getResource() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertEquals(MediaType.parseMediaType("text/css"), headers.getContentType());
replacedertEquals(17, headers.getContentLength());
replacedertEquals("max-age=3600", headers.getCacheControl());
replacedertTrue(headers.containsKey("Last-Modified"));
replacedertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("test/foo.css") / 1000);
replacedertEquals("bytes", headers.getFirst("Accept-Ranges"));
replacedertEquals(1, headers.get("Accept-Ranges").size());
replacedertResponseBody(exchange, "h1 { color:red; }");
}
18
Source : ResourceWebHandlerTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void getResourceFromAlternatePath() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "baz.css");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertEquals(MediaType.parseMediaType("text/css"), headers.getContentType());
replacedertEquals(17, headers.getContentLength());
replacedertEquals("max-age=3600", headers.getCacheControl());
replacedertTrue(headers.containsKey("Last-Modified"));
replacedertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("testalternatepath/baz.css") / 1000);
replacedertEquals("bytes", headers.getFirst("Accept-Ranges"));
replacedertEquals(1, headers.get("Accept-Ranges").size());
replacedertResponseBody(exchange, "h1 { color:red; }");
}
18
Source : ResourceWebHandlerTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void getResourceHttpHeader() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.head(""));
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.handle(exchange).block(TIMEOUT);
replacedertNull(exchange.getResponse().getStatusCode());
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertEquals(MediaType.parseMediaType("text/css"), headers.getContentType());
replacedertEquals(17, headers.getContentLength());
replacedertEquals("max-age=3600", headers.getCacheControl());
replacedertTrue(headers.containsKey("Last-Modified"));
replacedertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("test/foo.css") / 1000);
replacedertEquals("bytes", headers.getFirst("Accept-Ranges"));
replacedertEquals(1, headers.get("Accept-Ranges").size());
StepVerifier.create(exchange.getResponse().getBody()).expectErrorMatches(ex -> ex.getMessage().startsWith("No content was written")).verify();
}
18
Source : UndertowWebSocketClient.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private HandshakeInfo createHandshakeInfo(URI url, DefaultNegotiation negotiation) {
HttpHeaders responseHeaders = negotiation.getResponseHeaders();
String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
return new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
}
18
Source : StandardWebSocketClient.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private HandshakeInfo createHandshakeInfo(URI url, DefaultConfigurator configurator) {
HttpHeaders responseHeaders = configurator.getResponseHeaders();
String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
return new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
}
18
Source : RestTemplateTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void postForEnreplacedy() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
ResponseEnreplacedy<String> result = template.postForEnreplacedy("https://example.com", "Hello World", String.clreplaced);
replacedertEquals("Invalid POST result", expected, result.getBody());
replacedertEquals("Invalid Content-Type", MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
replacedertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void postForObject() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
String result = template.postForObject("https://example.com", "Hello World", String.clreplaced);
replacedertEquals("Invalid POST result", expected, result);
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void getForEnreplacedy() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(GET, "https://example.com", requestHeaders);
mockTextPlainHttpMessageConverter();
mockResponseStatus(HttpStatus.OK);
String expected = "Hello World";
mockTextResponseBody(expected);
ResponseEnreplacedy<String> result = template.getForEnreplacedy("https://example.com", String.clreplaced);
replacedertEquals("Invalid GET result", expected, result.getBody());
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
replacedertEquals("Invalid Content-Type header", MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
replacedertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void getForObject() throws Exception {
String expected = "Hello World";
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(GET, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
mockTextResponseBody("Hello World");
String result = template.getForObject("https://example.com", String.clreplaced);
replacedertEquals("Invalid GET result", expected, result);
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void exchange() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
HttpHeaders enreplacedyHeaders = new HttpHeaders();
enreplacedyHeaders.set("MyHeader", "MyValue");
HttpEnreplacedy<String> enreplacedy = new HttpEnreplacedy<>("Hello World", enreplacedyHeaders);
ResponseEnreplacedy<String> result = template.exchange("https://example.com", POST, enreplacedy, String.clreplaced);
replacedertEquals("Invalid POST result", expected, result.getBody());
replacedertEquals("Invalid Content-Type", MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
replacedertEquals("Invalid custom header", "MyValue", requestHeaders.getFirst("MyHeader"));
replacedertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void patchForObject() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(PATCH, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody("42", MediaType.TEXT_PLAIN);
String result = template.patchForObject("https://example.com", "Hello World", String.clreplaced);
replacedertEquals("Invalid POST result", expected, result);
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
verify(response).close();
}
18
Source : UriComponentsBuilder.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private boolean isForwardedSslOn(HttpHeaders headers) {
String forwardedSsl = headers.getFirst("X-Forwarded-Ssl");
return StringUtils.hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on");
}
18
Source : ResourceWebHandlerTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void getResourceFromAlternatePath() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "baz.css");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertThat(headers.getContentType()).isEqualTo(MediaType.parseMediaType("text/css"));
replacedertThat(headers.getContentLength()).isEqualTo(17);
replacedertThat(headers.getCacheControl()).isEqualTo("max-age=3600");
replacedertThat(headers.containsKey("Last-Modified")).isTrue();
replacedertThat(resourceLastModifiedDate("testalternatepath/baz.css") / 1000).isEqualTo(headers.getLastModified() / 1000);
replacedertThat(headers.getFirst("Accept-Ranges")).isEqualTo("bytes");
replacedertThat(headers.get("Accept-Ranges").size()).isEqualTo(1);
replacedertResponseBody(exchange, "h1 { color:red; }");
}
18
Source : ResourceWebHandlerTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void getResourceHttpHeader() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.head(""));
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.handle(exchange).block(TIMEOUT);
replacedertThat((Object) exchange.getResponse().getStatusCode()).isNull();
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertThat(headers.getContentType()).isEqualTo(MediaType.parseMediaType("text/css"));
replacedertThat(headers.getContentLength()).isEqualTo(17);
replacedertThat(headers.getCacheControl()).isEqualTo("max-age=3600");
replacedertThat(headers.containsKey("Last-Modified")).isTrue();
replacedertThat(resourceLastModifiedDate("test/foo.css") / 1000).isEqualTo(headers.getLastModified() / 1000);
replacedertThat(headers.getFirst("Accept-Ranges")).isEqualTo("bytes");
replacedertThat(headers.get("Accept-Ranges").size()).isEqualTo(1);
StepVerifier.create(exchange.getResponse().getBody()).expectErrorMatches(ex -> ex.getMessage().startsWith("No content was written")).verify();
}
18
Source : ResourceWebHandlerTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void getResource() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertThat(headers.getContentType()).isEqualTo(MediaType.parseMediaType("text/css"));
replacedertThat(headers.getContentLength()).isEqualTo(17);
replacedertThat(headers.getCacheControl()).isEqualTo("max-age=3600");
replacedertThat(headers.containsKey("Last-Modified")).isTrue();
replacedertThat(resourceLastModifiedDate("test/foo.css") / 1000).isEqualTo(headers.getLastModified() / 1000);
replacedertThat(headers.getFirst("Accept-Ranges")).isEqualTo("bytes");
replacedertThat(headers.get("Accept-Ranges").size()).isEqualTo(1);
replacedertResponseBody(exchange, "h1 { color:red; }");
}
18
Source : RestTemplateTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void postForEnreplacedy() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
ResponseEnreplacedy<String> result = template.postForEnreplacedy("https://example.com", "Hello World", String.clreplaced);
replacedertThat(result.getBody()).as("Invalid POST result").isEqualTo(expected);
replacedertThat(result.getHeaders().getContentType()).as("Invalid Content-Type").isEqualTo(MediaType.TEXT_PLAIN);
replacedertThat(requestHeaders.getFirst("Accept")).as("Invalid Accept header").isEqualTo(MediaType.TEXT_PLAIN_VALUE);
replacedertThat(result.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
verify(response).close();
}
18
Source : RestTemplateTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void exchange() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
HttpHeaders enreplacedyHeaders = new HttpHeaders();
enreplacedyHeaders.set("MyHeader", "MyValue");
HttpEnreplacedy<String> enreplacedy = new HttpEnreplacedy<>("Hello World", enreplacedyHeaders);
ResponseEnreplacedy<String> result = template.exchange("https://example.com", POST, enreplacedy, String.clreplaced);
replacedertThat(result.getBody()).as("Invalid POST result").isEqualTo(expected);
replacedertThat(result.getHeaders().getContentType()).as("Invalid Content-Type").isEqualTo(MediaType.TEXT_PLAIN);
replacedertThat(requestHeaders.getFirst("Accept")).as("Invalid Accept header").isEqualTo(MediaType.TEXT_PLAIN_VALUE);
replacedertThat(requestHeaders.getFirst("MyHeader")).as("Invalid custom header").isEqualTo("MyValue");
replacedertThat(result.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
verify(response).close();
}
18
Source : RestTemplateTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void getForEnreplacedy() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(GET, "https://example.com", requestHeaders);
mockTextPlainHttpMessageConverter();
mockResponseStatus(HttpStatus.OK);
String expected = "Hello World";
mockTextResponseBody(expected);
ResponseEnreplacedy<String> result = template.getForEnreplacedy("https://example.com", String.clreplaced);
replacedertThat(result.getBody()).as("Invalid GET result").isEqualTo(expected);
replacedertThat(requestHeaders.getFirst("Accept")).as("Invalid Accept header").isEqualTo(MediaType.TEXT_PLAIN_VALUE);
replacedertThat(result.getHeaders().getContentType()).as("Invalid Content-Type header").isEqualTo(MediaType.TEXT_PLAIN);
replacedertThat(result.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
verify(response).close();
}
18
Source : RestTemplateTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void getForObject() throws Exception {
String expected = "Hello World";
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(GET, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
mockTextResponseBody("Hello World");
String result = template.getForObject("https://example.com", String.clreplaced);
replacedertThat(result).as("Invalid GET result").isEqualTo(expected);
replacedertThat(requestHeaders.getFirst("Accept")).as("Invalid Accept header").isEqualTo(MediaType.TEXT_PLAIN_VALUE);
verify(response).close();
}
18
Source : RestTemplateTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void patchForObject() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(PATCH, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody("42", MediaType.TEXT_PLAIN);
String result = template.patchForObject("https://example.com", "Hello World", String.clreplaced);
replacedertThat(result).as("Invalid POST result").isEqualTo(expected);
replacedertThat(requestHeaders.getFirst("Accept")).as("Invalid Accept header").isEqualTo(MediaType.TEXT_PLAIN_VALUE);
verify(response).close();
}
18
Source : RestTemplateTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void postForObject() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
String result = template.postForObject("https://example.com", "Hello World", String.clreplaced);
replacedertThat(result).as("Invalid POST result").isEqualTo(expected);
replacedertThat(requestHeaders.getFirst("Accept")).as("Invalid Accept header").isEqualTo(MediaType.TEXT_PLAIN_VALUE);
verify(response).close();
}
18
Source : RequestHandlerUtilsTest.java
with Apache License 2.0
from onap
with Apache License 2.0
from onap
@Test
public void setCamundaHeadersTest() throws ContactCamundaException, RequestDbFailureException {
// user:preplacedword
String encryptedAuth = "015E7ACF706C6BBF85F2079378BDD2896E226E09D13DC2784BA309E27D59AB9FAD3A5E039DF0BB8408";
String key = "07a7159d3bf51a0e53be7a8f89699be7";
HttpHeaders headers = camundaRequestHandler.setCamundaHeaders(encryptedAuth, key);
List<org.springframework.http.MediaType> acceptedType = headers.getAccept();
String expectedAcceptedType = "application/json";
replacedertEquals(expectedAcceptedType, acceptedType.get(0).toString());
String basicAuth = headers.getFirst(HttpHeaders.AUTHORIZATION);
String expectedBasicAuth = "Basic dXNlcjpwYXNzd29yZA==";
replacedertEquals(expectedBasicAuth, basicAuth);
}
18
Source : RestTemplateTests.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@Test
public void patchForObject() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(PATCH, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody("42", MediaType.TEXT_PLAIN);
String result = template.patchForObject("http://example.com", "Hello World", String.clreplaced);
replacedertEquals("Invalid POST result", expected, result);
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@Test
public void postForObject() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
String result = template.postForObject("http://example.com", "Hello World", String.clreplaced);
replacedertEquals("Invalid POST result", expected, result);
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@Test
public void getForEnreplacedy() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(GET, "http://example.com", requestHeaders);
mockTextPlainHttpMessageConverter();
mockResponseStatus(HttpStatus.OK);
String expected = "Hello World";
mockTextResponseBody(expected);
ResponseEnreplacedy<String> result = template.getForEnreplacedy("http://example.com", String.clreplaced);
replacedertEquals("Invalid GET result", expected, result.getBody());
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
replacedertEquals("Invalid Content-Type header", MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
replacedertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@Test
public void getForObject() throws Exception {
String expected = "Hello World";
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(GET, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
mockTextResponseBody("Hello World");
String result = template.getForObject("http://example.com", String.clreplaced);
replacedertEquals("Invalid GET result", expected, result);
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@Test
public void postForEnreplacedy() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
ResponseEnreplacedy<String> result = template.postForEnreplacedy("http://example.com", "Hello World", String.clreplaced);
replacedertEquals("Invalid POST result", expected, result.getBody());
replacedertEquals("Invalid Content-Type", MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
replacedertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
verify(response).close();
}
18
Source : RestTemplateTests.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@Test
public void exchange() throws Exception {
mockTextPlainHttpMessageConverter();
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
String expected = "42";
mockResponseBody(expected, MediaType.TEXT_PLAIN);
HttpHeaders enreplacedyHeaders = new HttpHeaders();
enreplacedyHeaders.set("MyHeader", "MyValue");
HttpEnreplacedy<String> enreplacedy = new HttpEnreplacedy<>("Hello World", enreplacedyHeaders);
ResponseEnreplacedy<String> result = template.exchange("http://example.com", POST, enreplacedy, String.clreplaced);
replacedertEquals("Invalid POST result", expected, result.getBody());
replacedertEquals("Invalid Content-Type", MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
replacedertEquals("Invalid custom header", "MyValue", requestHeaders.getFirst("MyHeader"));
replacedertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
verify(response).close();
}
18
Source : OkHttpClientHttpRequest.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
private MediaType getContentType(HttpHeaders headers) {
String rawContentType = headers.getFirst("Content-Type");
return (StringUtils.hasText(rawContentType) ? MediaType.parse(rawContentType) : null);
}
18
Source : BrightUtil.java
with Apache License 2.0
from JacksonTu
with Apache License 2.0
from JacksonTu
/**
* 获取请求IP
*
* @param request ServerHttpRequest
* @return String IP
*/
public static String getServerHttpRequestIpAddress(ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
String ip = headers.getFirst("x-forwarded-for");
if (ip != null && ip.length() != 0 && !UNKNOW.equalsIgnoreCase(ip)) {
if (ip.contains(StringConstant.COMMA)) {
ip = ip.split(StringConstant.COMMA)[0];
}
}
if (ip == null || ip.length() == 0 || UNKNOW.equalsIgnoreCase(ip)) {
ip = headers.getFirst("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOW.equalsIgnoreCase(ip)) {
ip = headers.getFirst("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOW.equalsIgnoreCase(ip)) {
ip = headers.getFirst("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || UNKNOW.equalsIgnoreCase(ip)) {
ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || UNKNOW.equalsIgnoreCase(ip)) {
ip = headers.getFirst("X-Real-IP");
}
if (ip == null || ip.length() == 0 || UNKNOW.equalsIgnoreCase(ip)) {
ip = Objects.requireNonNull(request.getRemoteAddress()).getAddress().getHostAddress();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
}
18
Source : UserController.java
with Apache License 2.0
from didclab
with Apache License 2.0
from didclab
@GetMapping
public Object getHistory(@RequestHeader HttpHeaders headers) {
return userService.getHistory(headers.getFirst(ODSConstants.COOKIE));
}
18
Source : CredController.java
with Apache License 2.0
from didclab
with Apache License 2.0
from didclab
/**
* Handler for the POST request for saving the OAuth Credentials once the user toggles it in account preferences
* to save it.
* @param headers - Request header
* @param credentials - List of Credentials to save
* @return
*/
@PostMapping("/saveCredentials")
public Mono<Void> saveCredentials(@RequestHeader HttpHeaders headers, @RequestBody List<OAuthCredential> credentials) {
String cookie = headers.getFirst(ODSConstants.COOKIE);
return userService.saveUserCredentials(cookie, credentials);
}
18
Source : RestTemplateRequest.java
with Apache License 2.0
from crnk-project
with Apache License 2.0
from crnk-project
@Override
public String getHeaderValue(String name) {
return headers.getFirst(name);
}
18
Source : ServerHelper.java
with Apache License 2.0
from bliblidotcom
with Apache License 2.0
from bliblidotcom
public static String getValueFromQueryOrHeader(HttpHeaders httpHeaders, MultiValueMap<String, String> queryParams, String headerKey, String queryParamKey) {
if (StringUtils.isEmpty(httpHeaders.getFirst(headerKey))) {
return queryParams.getFirst(queryParamKey);
} else {
return httpHeaders.getFirst(headerKey);
}
}
17
Source : ApiConfigService.java
with GNU General Public License v3.0
from wehotel
with GNU General Public License v3.0
from wehotel
public Mono<Object> canAccess(ServerWebExchange exchange) {
ServerHttpRequest req = exchange.getRequest();
HttpHeaders hdrs = req.getHeaders();
LogService.setBizId(req.getId());
return canAccess(exchange, WebUtils.getAppId(exchange), WebUtils.getOriginIp(exchange), hdrs.getFirst(timestampHeader), hdrs.getFirst(signHeader), WebUtils.getClientService(exchange), req.getMethod(), WebUtils.getClientReqPath(exchange));
}
17
Source : AbstractSockJsIntegrationTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
// SPR-13254
@Test
public void echoXhrWithHeaders() throws Exception {
AbstractXhrTransport xhrTransport = createXhrTransport();
xhrTransport.setXhrStreamingDisabled(true);
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
headers.add("auth", "123");
testEcho(10, xhrTransport, headers);
for (Map.Entry<String, HttpHeaders> entry : this.testFilter.requests.entrySet()) {
HttpHeaders httpHeaders = entry.getValue();
replacedertEquals("No auth header for: " + entry.getKey(), "123", httpHeaders.getFirst("auth"));
}
}
17
Source : ResourceWebHandlerTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void getResourceWithHtmlMediaType() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, "foo.html");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
replacedertEquals(MediaType.TEXT_HTML, headers.getContentType());
replacedertEquals("max-age=3600", headers.getCacheControl());
replacedertTrue(headers.containsKey("Last-Modified"));
replacedertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("test/foo.html") / 1000);
replacedertEquals("bytes", headers.getFirst("Accept-Ranges"));
replacedertEquals(1, headers.get("Accept-Ranges").size());
}
17
Source : RestTemplateTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
@SuppressWarnings("rawtypes")
public void exchangeParameterizedType() throws Exception {
GenericHttpMessageConverter converter = mock(GenericHttpMessageConverter.clreplaced);
template.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));
ParameterizedTypeReference<List<Integer>> intList = new ParameterizedTypeReference<List<Integer>>() {
};
given(converter.canRead(intList.getType(), null, null)).willReturn(true);
given(converter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(converter.canWrite(String.clreplaced, String.clreplaced, null)).willReturn(true);
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "https://example.com", requestHeaders);
List<Integer> expected = Collections.singletonList(42);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.TEXT_PLAIN);
responseHeaders.setContentLength(10);
mockResponseStatus(HttpStatus.OK);
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getBody()).willReturn(new ByteArrayInputStream(Integer.toString(42).getBytes()));
given(converter.canRead(intList.getType(), null, MediaType.TEXT_PLAIN)).willReturn(true);
given(converter.read(eq(intList.getType()), eq(null), any(HttpInputMessage.clreplaced))).willReturn(expected);
HttpHeaders enreplacedyHeaders = new HttpHeaders();
enreplacedyHeaders.set("MyHeader", "MyValue");
HttpEnreplacedy<String> requestEnreplacedy = new HttpEnreplacedy<>("Hello World", enreplacedyHeaders);
ResponseEnreplacedy<List<Integer>> result = template.exchange("https://example.com", POST, requestEnreplacedy, intList);
replacedertEquals("Invalid POST result", expected, result.getBody());
replacedertEquals("Invalid Content-Type", MediaType.TEXT_PLAIN, result.getHeaders().getContentType());
replacedertEquals("Invalid Accept header", MediaType.TEXT_PLAIN_VALUE, requestHeaders.getFirst("Accept"));
replacedertEquals("Invalid custom header", "MyValue", requestHeaders.getFirst("MyHeader"));
replacedertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
verify(response).close();
}
17
Source : ForwardedHeaderTransformer.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Nullable
private static String getForwardedPrefix(ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
String prefix = headers.getFirst("X-Forwarded-Prefix");
if (prefix != null) {
int endIndex = prefix.length();
while (endIndex > 1 && prefix.charAt(endIndex - 1) == '/') {
endIndex--;
}
prefix = (endIndex != prefix.length() ? prefix.substring(0, endIndex) : prefix);
}
return prefix;
}
See More Examples