code commit for BAEL-4869

This commit is contained in:
Bhabani Prasad Patel 2021-05-27 02:12:22 +05:30
parent b0464bd691
commit a40d2ff3b3
5 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package com.baeldung.sampleapp.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@ComponentScan({ "com.baeldung.sampleapp.web" })
public class MaxHTTPHeaderSizeConfig implements WebMvcConfigurer {
public MaxHTTPHeaderSizeConfig() {
super();
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.sampleapp.web.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/request-header-test")
public class MaxHttpHeaderSizeController {
@GetMapping
public boolean testMaxHTTPHeaderSize(@RequestHeader(value = "token") String token) {
return true;
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.web.controller;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.sampleapp.config.WebConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebConfig.class)
@WebAppConfiguration
public class MaxHttpHeaderSizeControllerIntegrationTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void givenTokenWithLessThan8KBLegth_whenSendGetRequest_thenReturnsOK() throws Exception {
mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE)
.with(httpBasic("user", "password")).header("token", "token")).andExpect(status().isOk());
}
@Test
public void givenTokenIsMissingInHeade_whenSendGetRequest_thenThrowsBadRequest() throws Exception {
mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE)
.with(httpBasic("user", "password"))).andExpect(status().isBadRequest());
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.web.controller;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import com.baeldung.sampleapp.config.MaxHTTPHeaderSizeConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MaxHTTPHeaderSizeConfig.class }, loader = AnnotationConfigContextLoader.class)
@ActiveProfiles("test")
// Start MaxHttpHeaderSizeController Spring Boot App(MainApplication) first
public class MaxHttpHeaderSizeControllerLiveTest {
@Test(expected = HttpClientErrorException.class)
public void givenTokenWithGreaterThan8KBLegth_whenSendGetRequest_thenThrowsBadRequest() throws Exception {
final String url = "http://localhost:8080/request-header-test";
HttpHeaders headers = new HttpHeaders();
headers.set("token", readRandomStringFromFile());
HttpEntity entity = new HttpEntity(headers);
final ResponseEntity<String> response = new RestTemplate().exchange(url, HttpMethod.GET, entity, String.class);
}
static String readRandomStringFromFile() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/randomSringForheader.txt"));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
reader.close();
String content = stringBuilder.toString();
return content;
}
}

File diff suppressed because one or more lines are too long