Merge pull request #9907 from tarunjain01/master

BAEL-4404
This commit is contained in:
davidmartinezbarua 2020-09-23 23:07:06 -03:00 committed by GitHub
commit df58b7b521
3 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package com.baeldung.charencoding;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.CharacterEncodingFilter;
@SpringBootApplication
public class CharacterEncodingDemo {
public static void main(String[] args) {
SpringApplication.run(CharacterEncodingDemo.class, args);
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.charencoding.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class CharEncodingCheckController {
@GetMapping("/ping")
public String ping() {
return "path";
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.charencoding.controller;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Test;
import org.springframework.web.filter.CharacterEncodingFilter;
class CharEncodingCheckControllerUnitTest {
@Test
void whenCharEncodingFilter_thenVerifyEncoding() throws ServletException, IOException {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = mock(FilterChain.class);
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
filter.doFilter(request, response, chain);
verify(request).setCharacterEncoding("UTF-8");
verify(response).setCharacterEncoding("UTF-8");
}
}