public ResponseEntity<T> put() { RequestEntity<Object> requestEntity = headers(RequestEntity.put(uri)) .body(body()); return exchange(requestEntity); }
public Mono<ResponseEntity<T>> put() { RequestEntity<Object> requestEntity = headers(RequestEntity.put(uri)) .body(body()); return exchange(requestEntity); }
@Test public void methods() throws URISyntaxException { URI url = new URI("http://example.com"); RequestEntity<?> entity = RequestEntity.get(url).build(); assertEquals(HttpMethod.GET, entity.getMethod()); entity = RequestEntity.post(url).build(); assertEquals(HttpMethod.POST, entity.getMethod()); entity = RequestEntity.head(url).build(); assertEquals(HttpMethod.HEAD, entity.getMethod()); entity = RequestEntity.options(url).build(); assertEquals(HttpMethod.OPTIONS, entity.getMethod()); entity = RequestEntity.put(url).build(); assertEquals(HttpMethod.PUT, entity.getMethod()); entity = RequestEntity.patch(url).build(); assertEquals(HttpMethod.PATCH, entity.getMethod()); entity = RequestEntity.delete(url).build(); assertEquals(HttpMethod.DELETE, entity.getMethod()); }
@Override public TokenResponse createToken(String clientId, TokenRequest tokenRequest) { RequestEntity<TokenRequest> requestEntity = RequestEntity .put(buildUri("authorizations/clients/" + clientId)).contentType(MediaType.APPLICATION_JSON) .body(tokenRequest); return getRestTemplate().exchange(requestEntity, TokenResponse.class).getBody(); }
private ResponseEntity<String> putBooking() throws URISyntaxException, JsonProcessingException { RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://api-sandbox.flight.com/api/bookings/1")) .contentType(APPLICATION_JSON) .body(HttpBodyConverter.OBJECT_MAPPER.writeValueAsString(booking)); return restTemplate.exchange(bookFlightRequest, String.class); } }
@Test public void shouldBeAbleToMatchBodyByXmlEquality() throws Exception { // Given final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.my-test.com/api/bookings/1")) .contentType(APPLICATION_XML) .body("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <flightId>1</flightId> <class>PREMIUM</class>"); // When final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class); // Then assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK); }
@Test public void shouldBeAbleToMatchBodyByJsonEqualityWithHttpBodyConverter() throws Exception { // Given final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.my-test.com/api/bookings/1")) .contentType(APPLICATION_JSON) .body(HttpBodyConverter.OBJECT_MAPPER.writeValueAsString(booking)); // When final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class); // Then assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK); }
@Test public void shouldBeAbleToMatchBodyByXmlEqualityWithHttpBodyConverter() throws Exception { // Given final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.my-test.com/api/bookings/1")) .contentType(APPLICATION_XML) .body(HttpBodyConverter.XML_MAPPER.writeValueAsString(booking)); // When final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class); // Then assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK); }
@Test public void shouldBeAbleToMatchBodyByJsonEquality() throws Exception { // Given final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.my-test.com/api/bookings/1")) .contentType(APPLICATION_JSON) .body("{\"class\": \"PREMIUM\", \"flightId\": \"1\"}"); // When final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class); // Then assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK); }
@Test public void shouldBeAbleToAmendABookingUsingHoverfly() throws URISyntaxException { // Given final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.other-anotherservice.com/api/bookings/1")) .contentType(APPLICATION_JSON) .body("{\"flightId\": \"1\", \"class\": \"PREMIUM\"}"); // When final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class); // Then assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK); }
@Test public void shouldBeAbleToChangeStatusCodeUsingHoverflyLocalMiddleware() throws URISyntaxException { requirePython(); // Given final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.other-anotherservice.com/api/bookings/1")) .contentType(APPLICATION_JSON) .body("{\"flightId\": \"1\", \"class\": \"PREMIUM\"}"); // When final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class); // Then assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.CREATED); } }
@Test public void shouldBeAbleToGenerateResponseFromRequestBody() { // Given URI uri = UriComponentsBuilder.fromHttpUrl("http://www.my-test.com") .path("/api/bookings/1") .build() .toUri(); RequestEntity<SimpleBooking> request = RequestEntity.put(uri) .contentType(MediaType.APPLICATION_JSON) .body(new SimpleBooking(1, "London", "Hong Kong", null)); // When final ResponseEntity<SimpleBooking> response = restTemplate.exchange(request, SimpleBooking.class); // Then assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); SimpleBooking body = response.getBody(); assertThat(body.getDestination()).isEqualTo("Hong Kong"); } }
@Test public void shouldBeAbleToSimulate() throws Exception { // From File final ResponseEntity<String> getBookingResponse = restTemplate.getForEntity("https://www.my-test.com/api/bookings/1", String.class); assertThat(getBookingResponse.getStatusCode()).isEqualTo(OK); // From DSL hoverflyRule.simulate(dsl(service("www.other-anotherservice.com") .put("/api/bookings/1").body("{\"flightId\": \"1\", \"class\": \"PREMIUM\"}") .willReturn(success()))); final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.other-anotherservice.com/api/bookings/1")) .contentType(APPLICATION_JSON) .body("{\"flightId\": \"1\", \"class\": \"PREMIUM\"}"); // When final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class); // Then assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK); }