commit
df58b7b521
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue