1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
31
31
import static org .junit .Assert .*;
32
32
33
33
/**
34
+ * Tests for {@link HiddenHttpMethodFilter}.
35
+ *
34
36
* @author Arjen Poutsma
37
+ * @author Brian Clozel
35
38
*/
36
39
public class HiddenHttpMethodFilterTests {
37
40
38
41
private final HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter ();
39
42
40
43
@ Test
41
44
public void filterWithParameter () throws IOException , ServletException {
42
- MockHttpServletRequest request = new MockHttpServletRequest ("POST" , "/hotels" );
43
- request .addParameter ("_method" , "delete" );
44
- MockHttpServletResponse response = new MockHttpServletResponse ();
45
-
46
- FilterChain filterChain = new FilterChain () {
45
+ filterWithParameterForMethod ("delete" , "DELETE" );
46
+ filterWithParameterForMethod ("put" , "PUT" );
47
+ filterWithParameterForMethod ("patch" , "PATCH" );
48
+ }
47
49
48
- @ Override
49
- public void doFilter (ServletRequest filterRequest ,
50
- ServletResponse filterResponse ) throws IOException , ServletException {
51
- assertEquals ("Invalid method" , "DELETE" ,
52
- ((HttpServletRequest ) filterRequest ).getMethod ());
53
- }
54
- };
55
- filter .doFilter (request , response , filterChain );
50
+ @ Test
51
+ public void filterWithParameterDisallowedMethods () throws IOException , ServletException {
52
+ filterWithParameterForMethod ("trace" , "POST" );
53
+ filterWithParameterForMethod ("head" , "POST" );
54
+ filterWithParameterForMethod ("options" , "POST" );
56
55
}
57
56
58
57
@ Test
59
58
public void filterWithNoParameter () throws IOException , ServletException {
59
+ filterWithParameterForMethod (null , "POST" );
60
+ }
61
+
62
+ private void filterWithParameterForMethod (String methodParam , String expectedMethod )
63
+ throws IOException , ServletException {
60
64
MockHttpServletRequest request = new MockHttpServletRequest ("POST" , "/hotels" );
65
+ if (methodParam != null ) {
66
+ request .addParameter ("_method" , methodParam );
67
+ }
61
68
MockHttpServletResponse response = new MockHttpServletResponse ();
62
69
63
70
FilterChain filterChain = new FilterChain () {
64
71
65
72
@ Override
66
73
public void doFilter (ServletRequest filterRequest ,
67
74
ServletResponse filterResponse ) throws IOException , ServletException {
68
- assertEquals ("Invalid method" , "POST" ,
75
+ assertEquals ("Invalid method" , expectedMethod ,
69
76
((HttpServletRequest ) filterRequest ).getMethod ());
70
77
}
71
78
};
72
- filter .doFilter (request , response , filterChain );
79
+ this . filter .doFilter (request , response , filterChain );
73
80
}
74
81
75
82
}
0 commit comments