3 import org.junit.jupiter.api.Test;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.beans.factory.annotation.Value;
6 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
7 import org.springframework.boot.test.context.SpringBootTest;
8 import org.springframework.test.web.servlet.MockMvc;
10 import static de.juplo.demo.BackendVersionInterceptor.BACKEND_VERSION;
11 import static org.springframework.http.HttpHeaders.ACCEPT;
12 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
13 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
14 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
15 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18 @SpringBootTest(properties = { "application.name=FOO", "build.version=BAR" })
20 class DemoApplicationTests
23 @Value("${build.version}")
24 String projectVersion;
34 void getWelcomeResponseIsOkIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
36 mvc.perform(get("/")).andExpect(status().isOk());
40 void getWelcomeResponseIsOkIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception
44 .header(BACKEND_VERSION, projectVersion))
45 .andExpect(status().isOk());
49 void getWelcomeResponseIsGoneIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception
53 .header(BACKEND_VERSION, "FOO"))
54 .andExpect(status().isGone());
58 void getGreetingIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
61 .perform(post("/peter")
62 .header(ACCEPT, "application/json")
63 .header(BACKEND_VERSION, projectVersion)
64 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
65 .andExpect(status().isOk())
66 .andExpect(content().json("{\"greeting\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
70 void getGreetingIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception
73 .perform(post("/peter")
74 .header(ACCEPT, "application/json")
75 .header(BACKEND_VERSION, "FOO")
76 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
77 .andExpect(status().isGone());
81 void getGreetingIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception
84 .perform(post("/peter")
85 .header(ACCEPT, "application/json")
86 .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
87 .andExpect(status().isOk())
88 .andExpect(content().json("{\"greeting\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));