Refine search
@Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(mediaType); return headers; } };
@Override public HttpHeaders getMultipartHeaders(String paramOrFileName) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return headers; } };
public XhrClientSockJsSession(TransportRequest request, WebSocketHandler handler, XhrTransport transport, SettableListenableFuture<WebSocketSession> connectFuture) { super(request, handler, connectFuture); Assert.notNull(transport, "XhrTransport is required"); this.transport = transport; this.headers = request.getHttpRequestHeaders(); this.sendHeaders = new HttpHeaders(); this.sendHeaders.putAll(this.headers); this.sendHeaders.setContentType(MediaType.APPLICATION_JSON); this.sendUrl = request.getSockJsUrlInfo().getTransportUrl(TransportType.XHR_SEND); }
private MultiValueMap<String, Object> generateBody() { HttpHeaders fooHeaders = new HttpHeaders(); fooHeaders.setContentType(MediaType.TEXT_PLAIN); ClassPathResource fooResource = new ClassPathResource("org/springframework/http/codec/multipart/foo.txt"); HttpEntity<ClassPathResource> fooPart = new HttpEntity<>(fooResource, fooHeaders); HttpEntity<String> barPart = new HttpEntity<>("bar"); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("fooPart", fooPart); parts.add("barPart", barPart); return parts; }
@Override public HttpHeaders getMultipartHeaders(String paramOrFileName) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType("application", "octet-stream", StandardCharsets.ISO_8859_1)); return headers; } };
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); }
private void testCreate(String url, String basename) { MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("json-data", new HttpEntity<>(new TestData(basename))); parts.add("file-data", new ClassPathResource("logo.jpg", getClass())); parts.add("empty-data", new HttpEntity<>(new byte[0])); // SPR-12860 HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType("application", "octet-stream", StandardCharsets.ISO_8859_1)); parts.add("iso-8859-1-data", new HttpEntity<>(new byte[] {(byte) 0xC4}, headers)); // SPR-13096 URI location = restTemplate.postForLocation(url, parts); assertEquals("http://localhost:8080/test/" + basename + "/logo.jpg", location.toString()); }
<T> ResponseEntity<T> performPost(String url, MediaType in, Object body, MediaType out, ParameterizedTypeReference<T> type) throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(in); if (out != null) { headers.setAccept(Collections.singletonList(out)); } return getRestTemplate().exchange(preparePost(url, headers, body), type); }
<T> ResponseEntity<T> performPost(String url, MediaType in, Object body, MediaType out, Class<T> type) throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(in); if (out != null) { headers.setAccept(Collections.singletonList(out)); } return getRestTemplate().exchange(preparePost(url, headers, body), type); }
@Test public void postForLocation() throws Exception { HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1)); HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders); Future<URI> locationFuture = template.postForLocation(baseUrl + "/{method}", entity, "post"); URI location = locationFuture.get(); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), location); }
@Test public void postForLocationEntity() throws URISyntaxException { HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1)); HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders); URI location = template.postForLocation(baseUrl + "/{method}", entity, "post"); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), location); }
@Test public void exchangePost() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); requestHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<>(helloWorld, requestHeaders); HttpEntity<Void> result = template.exchange(baseUrl + "/{method}", POST, entity, Void.class, "post"); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation()); assertFalse(result.hasBody()); }
@Test public void exchangePost() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); requestHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders); Future<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post"); ResponseEntity<Void> result = resultFuture.get(); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation()); assertFalse(result.hasBody()); }
@Test public void postForLocationCallbackWithLambdas() throws Exception { HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1)); HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders); final URI expected = new URI(baseUrl + "/post/1"); ListenableFuture<URI> locationFuture = template.postForLocation(baseUrl + "/{method}", entity, "post"); locationFuture.addCallback(result -> assertEquals("Invalid location", expected, result), ex -> fail(ex.getMessage())); waitTillDone(locationFuture); }
@Test public void jsonPostForObjectWithJacksonView() throws URISyntaxException { HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8)); MySampleBean bean = new MySampleBean("with", "with", "without"); MappingJacksonValue jacksonValue = new MappingJacksonValue(bean); jacksonValue.setSerializationView(MyJacksonView1.class); HttpEntity<MappingJacksonValue> entity = new HttpEntity<>(jacksonValue, entityHeaders); String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class); assertTrue(s.contains("\"with1\":\"with\"")); assertFalse(s.contains("\"with2\":\"with\"")); assertFalse(s.contains("\"without\":\"without\"")); }
@Test public void headers() { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.put("foo", Arrays.asList("bar", "baz")); this.builder.headers(httpHeaders); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); List<String> headers = Collections.list(request.getHeaders("foo")); assertEquals(2, headers.size()); assertEquals("bar", headers.get(0)); assertEquals("baz", headers.get(1)); assertEquals(MediaType.APPLICATION_JSON.toString(), request.getHeader("Content-Type")); }
@Test public void jsonPostForObject() throws URISyntaxException { HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8)); MySampleBean bean = new MySampleBean(); bean.setWith1("with"); bean.setWith2("with"); bean.setWithout("without"); HttpEntity<MySampleBean> entity = new HttpEntity<>(bean, entityHeaders); String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class); assertTrue(s.contains("\"with1\":\"with\"")); assertTrue(s.contains("\"with2\":\"with\"")); assertTrue(s.contains("\"without\":\"without\"")); }
@Test public void exchangePostCallbackWithLambdas() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); requestHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders); ListenableFuture<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post"); final URI expected =new URI(baseUrl + "/post/1"); resultFuture.addCallback(result -> { assertEquals("Invalid location", expected, result.getHeaders().getLocation()); assertFalse(result.hasBody()); }, ex -> fail(ex.getMessage())); waitTillDone(resultFuture); }
@Test public void handleErrorException() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); given(this.response.getRawStatusCode()).willReturn(this.httpStatus.value()); given(this.response.getHeaders()).willReturn(headers); try { this.handler.handleError(this.response); fail("expected " + this.expectedExceptionClass.getSimpleName()); } catch (HttpStatusCodeException ex) { assertEquals("Expected " + this.expectedExceptionClass.getSimpleName(), this.expectedExceptionClass, ex.getClass()); } }
@Test public void sendMessage() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("foo", "bar"); requestHeaders.setContentType(MediaType.APPLICATION_JSON); TestXhrTransport transport = new TestXhrTransport(); transport.sendMessageResponseToReturn = new ResponseEntity<>(HttpStatus.NO_CONTENT); URI url = new URI("http://example.com"); transport.executeSendRequest(url, requestHeaders, new TextMessage("payload")); assertEquals(2, transport.actualSendRequestHeaders.size()); assertEquals("bar", transport.actualSendRequestHeaders.getFirst("foo")); assertEquals(MediaType.APPLICATION_JSON, transport.actualSendRequestHeaders.getContentType()); }