formatting work
This commit is contained in:
parent
cbf878ba34
commit
76485ab399
@ -10,7 +10,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>1.4.2.RELEASE</version>
|
<version>1.4.3.RELEASE</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.baeldung.web.controller.mediatypes;
|
package org.baeldung.web.controller.mediatypes;
|
||||||
|
|
||||||
|
|
||||||
import org.baeldung.web.dto.BaeldungItem;
|
import org.baeldung.web.dto.BaeldungItem;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
@ -9,12 +8,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json",
|
@RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json", consumes = "application/vnd.baeldung.api.v1+json")
|
||||||
consumes = "application/vnd.baeldung.api.v1+json")
|
|
||||||
public class CustomMediaTypeController {
|
public class CustomMediaTypeController {
|
||||||
|
|
||||||
@RequestMapping(value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v1+json",
|
@RequestMapping(value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v1+json", consumes = "application/vnd.baeldung.api.v1+json")
|
||||||
consumes = "application/vnd.baeldung.api.v1+json")
|
|
||||||
public @ResponseBody ResponseEntity<BaeldungItem> getItem() {
|
public @ResponseBody ResponseEntity<BaeldungItem> getItem() {
|
||||||
return new ResponseEntity<>(new BaeldungItem("itemId1"), HttpStatus.OK);
|
return new ResponseEntity<>(new BaeldungItem("itemId1"), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.baeldung.web.dto;
|
package org.baeldung.web.dto;
|
||||||
|
|
||||||
|
|
||||||
public class BaeldungItem {
|
public class BaeldungItem {
|
||||||
private final String itemId;
|
private final String itemId;
|
||||||
|
|
||||||
|
@ -17,9 +17,7 @@ public class DefaultContentTypeInterceptor implements Interceptor {
|
|||||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||||
|
|
||||||
Request originalRequest = chain.request();
|
Request originalRequest = chain.request();
|
||||||
Request requestWithUserAgent = originalRequest.newBuilder()
|
Request requestWithUserAgent = originalRequest.newBuilder().header("Content-Type", contentType).build();
|
||||||
.header("Content-Type", contentType)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return chain.proceed(requestWithUserAgent);
|
return chain.proceed(requestWithUserAgent);
|
||||||
}
|
}
|
||||||
|
@ -24,10 +24,7 @@ public class OkHttpHeaderLiveTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSetHeader_thenCorrect() throws IOException {
|
public void whenSetHeader_thenCorrect() throws IOException {
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder().url(SAMPLE_URL).addHeader("Content-Type", "application/json").build();
|
||||||
.url(SAMPLE_URL)
|
|
||||||
.addHeader("Content-Type", "application/json")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Call call = client.newCall(request);
|
Call call = client.newCall(request);
|
||||||
Response response = call.execute();
|
Response response = call.execute();
|
||||||
@ -37,13 +34,9 @@ public class OkHttpHeaderLiveTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenSetDefaultHeader_thenCorrect() throws IOException {
|
public void whenSetDefaultHeader_thenCorrect() throws IOException {
|
||||||
|
|
||||||
OkHttpClient clientWithInterceptor = new OkHttpClient.Builder()
|
OkHttpClient clientWithInterceptor = new OkHttpClient.Builder().addInterceptor(new DefaultContentTypeInterceptor("application/json")).build();
|
||||||
.addInterceptor(new DefaultContentTypeInterceptor("application/json"))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder().url(SAMPLE_URL).build();
|
||||||
.url(SAMPLE_URL)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Call call = clientWithInterceptor.newCall(request);
|
Call call = clientWithInterceptor.newCall(request);
|
||||||
Response response = call.execute();
|
Response response = call.execute();
|
||||||
|
@ -49,11 +49,7 @@ public class OkHttpPostingLiveTest {
|
|||||||
public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException {
|
public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException {
|
||||||
final String postBody = "test post";
|
final String postBody = "test post";
|
||||||
|
|
||||||
final Request request = new Request.Builder()
|
final Request request = new Request.Builder().url(URL_SECURED_BY_BASIC_AUTHENTICATION).addHeader("Authorization", Credentials.basic("test", "test")).post(RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), "test post")).build();
|
||||||
.url(URL_SECURED_BY_BASIC_AUTHENTICATION)
|
|
||||||
.addHeader("Authorization", Credentials.basic("test", "test"))
|
|
||||||
.post(RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), "test post"))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
final Call call = client.newCall(request);
|
final Call call = client.newCall(request);
|
||||||
final Response response = call.execute();
|
final Response response = call.execute();
|
||||||
|
@ -17,13 +17,9 @@ public class OkHttpRedirectLiveTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenSetFollowRedirects_thenNotRedirected() throws IOException {
|
public void whenSetFollowRedirects_thenNotRedirected() throws IOException {
|
||||||
|
|
||||||
OkHttpClient client = new OkHttpClient().newBuilder()
|
OkHttpClient client = new OkHttpClient().newBuilder().followRedirects(false).build();
|
||||||
.followRedirects(false)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder().url("http://t.co/I5YYd9tddw").build();
|
||||||
.url("http://t.co/I5YYd9tddw")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Call call = client.newCall(request);
|
Call call = client.newCall(request);
|
||||||
Response response = call.execute();
|
Response response = call.execute();
|
||||||
|
@ -71,4 +71,3 @@ public class ProgressRequestWrapper extends RequestBody {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,32 +12,28 @@ public class SpringUriBuilderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructUri() {
|
public void constructUri() {
|
||||||
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com")
|
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/junit-5").build();
|
||||||
.path("/junit-5").build();
|
|
||||||
|
|
||||||
assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString());
|
assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructUriEncoded() {
|
public void constructUriEncoded() {
|
||||||
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com")
|
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/junit 5").build().encode();
|
||||||
.path("/junit 5").build().encode();
|
|
||||||
|
|
||||||
assertEquals("http://www.baeldung.com/junit%205", uriComponents.toUriString());
|
assertEquals("http://www.baeldung.com/junit%205", uriComponents.toUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructUriFromTemplate() {
|
public void constructUriFromTemplate() {
|
||||||
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com")
|
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/{article-name}").buildAndExpand("junit-5");
|
||||||
.path("/{article-name}").buildAndExpand("junit-5");
|
|
||||||
|
|
||||||
assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString());
|
assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructUriWithQueryParameter() {
|
public void constructUriWithQueryParameter() {
|
||||||
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.google.com")
|
UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.google.com").path("/").query("q={keyword}").buildAndExpand("baeldung");
|
||||||
.path("/").query("q={keyword}").buildAndExpand("baeldung");
|
|
||||||
|
|
||||||
assertEquals("http://www.google.com/?q=baeldung", uriComponents.toUriString());
|
assertEquals("http://www.google.com/?q=baeldung", uriComponents.toUriString());
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ import org.springframework.web.context.WebApplicationContext;
|
|||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = WebConfig.class)
|
@ContextConfiguration(classes = WebConfig.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ -33,9 +32,6 @@ public class CustomMediaTypeControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldSendRequestForItem() throws Exception {
|
public void shouldSendRequestForItem() throws Exception {
|
||||||
mockMvc.perform(get("/public/api/endpoint")
|
mockMvc.perform(get("/public/api/endpoint").contentType("application/vnd.baeldung.api.v1+json").accept("application/vnd.baeldung.api.v1+json")).andExpect(status().isOk());
|
||||||
.contentType("application/vnd.baeldung.api.v1+json")
|
|
||||||
.accept("application/vnd.baeldung.api.v1+json"))
|
|
||||||
.andExpect(status().isOk());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user