/** * Return the response headers received from the server. */ public HttpHeaders getResponseHeaders() { return this.response.getHeaders(); }
/** * Return the status of the executed request. */ public HttpStatus getStatus() { return this.response.getStatusCode(); }
/** * Return response cookies received from the server. */ public MultiValueMap<String, ResponseCookie> getResponseCookies() { return this.response.getCookies(); }
private void logResponse(ClientHttpResponse response, String logPrefix) { LogFormatUtils.traceDebug(logger, traceOn -> { int code = response.getRawStatusCode(); HttpStatus status = HttpStatus.resolve(code); return logPrefix + "Response " + (status != null ? status : code) + (traceOn ? ", headers=" + formatHeaders(response.getHeaders()) : ""); }); }
private void mockTextPlainResponse(Flux<DataBuffer> body) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.TEXT_PLAIN); when(mockResponse.getHeaders()).thenReturn(httpHeaders); when(mockResponse.getStatusCode()).thenReturn(HttpStatus.OK); when(mockResponse.getRawStatusCode()).thenReturn(HttpStatus.OK.value()); when(mockResponse.getBody()).thenReturn(body); }
@Test public void adaptResponse() throws Exception { ResponseCookie cookie = ResponseCookie.from("custom-cookie", "c0").build(); TestHttpHandler handler = new TestHttpHandler(response -> { response.setStatusCode(HttpStatus.OK); response.getHeaders().put("custom-header", Arrays.asList("h0", "h1")); response.addCookie(cookie); return response.writeWith(Mono.just(toDataBuffer("Custom body"))); }); ClientHttpResponse response = new HttpHandlerConnector(handler) .connect(HttpMethod.GET, URI.create("/custom-path"), ReactiveHttpOutputMessage::setComplete) .block(Duration.ofSeconds(5)); assertEquals(HttpStatus.OK, response.getStatusCode()); HttpHeaders headers = response.getHeaders(); assertEquals(Arrays.asList("h0", "h1"), headers.get("custom-header")); assertEquals(cookie, response.getCookies().getFirst("custom-cookie")); assertEquals(Collections.singletonList("custom-cookie=c0"), headers.get(HttpHeaders.SET_COOKIE)); DataBuffer buffer = response.getBody().blockFirst(Duration.ZERO); assertEquals("Custom body", DataBufferTestUtils.dumpString(buffer, UTF_8)); }
@Override public int getRawStatusCode() { return this.delegate.getRawStatusCode(); }
@Override public Flux<DataBuffer> getBody() { return this.delegate.getBody(); }
@Test public void toEntityListWithUnknownStatusCode() throws Exception { DefaultDataBufferFactory factory = new DefaultDataBufferFactory(); DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8))); Flux<DataBuffer> body = Flux.just(dataBuffer); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.TEXT_PLAIN); when(mockResponse.getHeaders()).thenReturn(httpHeaders); when(mockResponse.getStatusCode()).thenThrow(new IllegalArgumentException("999")); when(mockResponse.getRawStatusCode()).thenReturn(999); when(mockResponse.getBody()).thenReturn(body); List<HttpMessageReader<?>> messageReaders = Collections .singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders); ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block(); assertEquals(Collections.singletonList("foo"), result.getBody()); try { result.getStatusCode(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException ex) { // do nothing } assertEquals(999, result.getStatusCodeValue()); assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType()); }
@Override public int rawStatusCode() { return this.response.getRawStatusCode(); }
@Override public Flux<DataBuffer> getBody() { return this.delegate.getBody(); }
@Test public void toEntityWithUnknownStatusCode() throws Exception { DefaultDataBufferFactory factory = new DefaultDataBufferFactory(); DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8))); Flux<DataBuffer> body = Flux.just(dataBuffer); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.TEXT_PLAIN); when(mockResponse.getHeaders()).thenReturn(httpHeaders); when(mockResponse.getStatusCode()).thenThrow(new IllegalArgumentException("999")); when(mockResponse.getRawStatusCode()).thenReturn(999); when(mockResponse.getBody()).thenReturn(body); List<HttpMessageReader<?>> messageReaders = Collections .singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())); when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders); ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block(); assertEquals("foo", result.getBody()); try { result.getStatusCode(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException ex) { // do nothing } assertEquals(999, result.getStatusCodeValue()); assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType()); }
@Override public HttpHeaders getHeaders() { return this.delegate.getHeaders(); }
@Override public HttpStatus getStatusCode() { return this.delegate.getStatusCode(); }
@Override public MultiValueMap<String, ResponseCookie> cookies() { return this.response.getCookies(); }
@Override public int getRawStatusCode() { return this.delegate.getRawStatusCode(); }
@SuppressWarnings("unchecked") private <T> Mono<T> consumeAndCancel() { return (Mono<T>) this.response.getBody() .map(buffer -> { DataBufferUtils.release(buffer); throw new ReadCancellationException(); }) .onErrorResume(ReadCancellationException.class, ex -> Mono.empty()) .then(); }
private HttpHeaders delegate() { return response.getHeaders(); }
@Override public HttpStatus statusCode() { return this.response.getStatusCode(); }
@Override public MultiValueMap<String, ResponseCookie> getCookies() { return this.delegate.getCookies(); }