private int getContentLength(HttpHeaders headers) { // Until this is fixed https://github.com/synchronoss/nio-multipart/issues/10 long length = headers.getContentLength(); return (int) length == length ? (int) length : -1; } }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException { byte[] bytes = this.bufferedOutput.toByteArray(); if (headers.getContentLength() < 0) { headers.setContentLength(bytes.length); } ClientHttpResponse result = executeInternal(headers, bytes); this.bufferedOutput = new ByteArrayOutputStream(0); return result; }
/** * Expect a "Content-Length" header with the given value. */ public WebTestClient.ResponseSpec contentLength(long contentLength) { return assertHeader("Content-Length", contentLength, getHeaders().getContentLength()); }
@Test public void byteBufferResponseBodyWithMono() throws Exception { String expected = "Hello!"; ResponseEntity<String> responseEntity = performGet("/raw-response/mono", new HttpHeaders(), String.class); assertEquals(6, responseEntity.getHeaders().getContentLength()); assertEquals(expected, responseEntity.getBody()); }
@Test // SPR-16172 public void personResponseBodyWithMonoResponseEntityXml() throws Exception { String url = "/person-response/mono-response-entity-xml"; ResponseEntity<String> entity = performGet(url, new HttpHeaders(), String.class); String actual = entity.getBody(); assertEquals(91, entity.getHeaders().getContentLength()); assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<person><name>Robert</name></person>", actual); }
@Test // SPR-17506 public void personResponseBodyWithEmptyMono() throws Exception { ResponseEntity<Person> responseEntity = performGet("/person-response/mono-empty", JSON, Person.class); assertEquals(0, responseEntity.getHeaders().getContentLength()); assertNull(responseEntity.getBody()); // As we're on the same connection, the 2nd request proves server response handling // did complete after the 1st request.. responseEntity = performGet("/person-response/mono-empty", JSON, Person.class); assertEquals(0, responseEntity.getHeaders().getContentLength()); assertNull(responseEntity.getBody()); }
@Test public void personResponseBodyWithList() throws Exception { List<?> expected = asList(new Person("Robert"), new Person("Marie")); ResponseEntity<List<Person>> entity = performGet("/person-response/list", JSON, PERSON_LIST); assertEquals(36, entity.getHeaders().getContentLength()); assertEquals(expected, entity.getBody()); }
@Test public void setContentLengthForMonoBody() { DefaultDataBufferFactory factory = new DefaultDataBufferFactory(); DataBuffer buffer = factory.wrap("body".getBytes(StandardCharsets.UTF_8)); HttpMessageWriter<String> writer = getWriter(Flux.just(buffer), MimeTypeUtils.TEXT_PLAIN); writer.write(Mono.just("body"), forClass(String.class), TEXT_PLAIN, this.response, NO_HINTS).block(); assertEquals(4, this.response.getHeaders().getContentLength()); }
@Test public void personResponseBody() throws Exception { Person expected = new Person("Robert"); ResponseEntity<Person> responseEntity = performGet("/person-response/person", JSON, Person.class); assertEquals(17, responseEntity.getHeaders().getContentLength()); assertEquals(expected, responseEntity.getBody()); }
@Test public void personResponseBodyWithSingle() throws Exception { Person expected = new Person("Robert"); ResponseEntity<Person> entity = performGet("/person-response/single", JSON, Person.class); assertEquals(17, entity.getHeaders().getContentLength()); assertEquals(expected, entity.getBody()); }
@Test public void httpHead() { String url = "http://localhost:" + this.port + "/text"; HttpHeaders headers = getRestTemplate().headForHeaders(url); String contentType = headers.getFirst("Content-Type"); assertNotNull(contentType); assertEquals("text/html;charset=utf-8", contentType.toLowerCase()); assertEquals(3, headers.getContentLength()); }
@Test // SPR-8867 public void writeOverrideRequestedContentType() throws IOException { String body = "H\u00e9llo W\u00f6rld"; MediaType requestedContentType = new MediaType("text", "html"); HttpHeaders headers = this.outputMessage.getHeaders(); headers.setContentType(TEXT_PLAIN_UTF_8); this.converter.write(body, requestedContentType, this.outputMessage); assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)); assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength()); assertFalse(headers.getAcceptCharset().isEmpty()); }
@Test public void postForLocationNull() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); template.postForLocation("http://example.com", null); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Test public void putNull() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(PUT, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); template.put("http://example.com", null); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Test public void contentLength() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).contentLength(42).build(); StepVerifier.create(result) .expectNextMatches(response -> Long.valueOf(42).equals(response.headers().getContentLength())) .expectComplete() .verify(); }
@Test public void getContentLength() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF; this.converter.write(this.testMsg, contentType, outputMessage); assertEquals(-1, outputMessage.getHeaders().getContentLength()); }
@Test public void contentLength() { long length = 42L; headers.setContentLength(length); assertEquals("Invalid Content-Length header", length, headers.getContentLength()); assertEquals("Invalid Content-Length header", "42", headers.getFirst("Content-Length")); }
@Test // SPR-13620 @SuppressWarnings("unchecked") public void writeContentInputStreamThrowingNullPointerException() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource resource = mock(Resource.class); InputStream in = mock(InputStream.class); given(resource.getInputStream()).willReturn(in); given(in.read(any())).willThrow(NullPointerException.class); converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage); assertEquals(0, outputMessage.getHeaders().getContentLength()); }
@Test public void writeResource() throws Exception { testWrite(get("/").build()); assertThat(this.response.getHeaders().getContentType(), is(TEXT_PLAIN)); assertThat(this.response.getHeaders().getContentLength(), is(39L)); assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes")); String content = "Spring Framework test resource content."; StepVerifier.create(this.response.getBodyAsString()).expectNext(content).expectComplete().verify(); }
@Test public void writeSingleRegion() throws Exception { testWrite(get("/").range(of(0, 5)).build()); assertThat(this.response.getHeaders().getContentType(), is(TEXT_PLAIN)); assertThat(this.response.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/39")); assertThat(this.response.getHeaders().getContentLength(), is(6L)); StepVerifier.create(this.response.getBodyAsString()).expectNext("Spring").expectComplete().verify(); }