1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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.
15
15
*/
16
16
package org .springframework .web .accept ;
17
17
18
- import java .util .Arrays ;
19
18
import java .util .Collections ;
20
19
import java .util .HashMap ;
21
20
import java .util .Map ;
25
24
26
25
import org .springframework .http .MediaType ;
27
26
import org .springframework .mock .web .test .MockHttpServletRequest ;
27
+ import org .springframework .mock .web .test .MockServletContext ;
28
+ import org .springframework .util .StringUtils ;
28
29
import org .springframework .web .HttpMediaTypeNotAcceptableException ;
29
30
import org .springframework .web .context .request .NativeWebRequest ;
30
31
import org .springframework .web .context .request .ServletWebRequest ;
31
32
32
- import static org .junit .Assert .* ;
33
+ import static org .junit .Assert .assertEquals ;
33
34
34
35
/**
35
36
* Test fixture for {@link ContentNegotiationManagerFactoryBean} tests.
@@ -46,7 +47,10 @@ public class ContentNegotiationManagerFactoryBeanTests {
46
47
47
48
@ Before
48
49
public void setup () {
49
- this .servletRequest = new MockHttpServletRequest ();
50
+ TestServletContext servletContext = new TestServletContext ();
51
+ servletContext .getMimeTypes ().put ("foo" , "application/foo" );
52
+
53
+ this .servletRequest = new MockHttpServletRequest (servletContext );
50
54
this .webRequest = new ServletWebRequest (this .servletRequest );
51
55
52
56
this .factoryBean = new ContentNegotiationManagerFactoryBean ();
@@ -62,7 +66,7 @@ public void defaultSettings() throws Exception {
62
66
this .servletRequest .setRequestURI ("/flower.gif" );
63
67
64
68
assertEquals ("Should be able to resolve file extensions by default" ,
65
- Arrays . asList (MediaType .IMAGE_GIF ), manager .resolveMediaTypes (this .webRequest ));
69
+ Collections . singletonList (MediaType .IMAGE_GIF ), manager .resolveMediaTypes (this .webRequest ));
66
70
67
71
this .servletRequest .setRequestURI ("/flower.xyz" );
68
72
@@ -79,26 +83,46 @@ public void defaultSettings() throws Exception {
79
83
this .servletRequest .addHeader ("Accept" , MediaType .IMAGE_GIF_VALUE );
80
84
81
85
assertEquals ("Should resolve Accept header by default" ,
82
- Arrays . asList (MediaType .IMAGE_GIF ), manager .resolveMediaTypes (this .webRequest ));
86
+ Collections . singletonList (MediaType .IMAGE_GIF ), manager .resolveMediaTypes (this .webRequest ));
83
87
}
84
88
85
89
@ Test
86
- public void addMediaTypes () throws Exception {
87
- Map <String , MediaType > mediaTypes = new HashMap <>();
88
- mediaTypes .put ("json" , MediaType .APPLICATION_JSON );
89
- this .factoryBean .addMediaTypes (mediaTypes );
90
+ public void favorPath () throws Exception {
91
+ this .factoryBean .setFavorPathExtension (true );
92
+ this .factoryBean .addMediaTypes (Collections .singletonMap ("bar" , new MediaType ("application" , "bar" )));
93
+ this .factoryBean .afterPropertiesSet ();
94
+ ContentNegotiationManager manager = this .factoryBean .getObject ();
95
+
96
+ this .servletRequest .setRequestURI ("/flower.foo" );
97
+ assertEquals (Collections .singletonList (new MediaType ("application" , "foo" )),
98
+ manager .resolveMediaTypes (this .webRequest ));
99
+
100
+ this .servletRequest .setRequestURI ("/flower.bar" );
101
+ assertEquals (Collections .singletonList (new MediaType ("application" , "bar" )),
102
+ manager .resolveMediaTypes (this .webRequest ));
103
+
104
+ this .servletRequest .setRequestURI ("/flower.gif" );
105
+ assertEquals (Collections .singletonList (MediaType .IMAGE_GIF ), manager .resolveMediaTypes (this .webRequest ));
106
+ }
90
107
108
+ @ Test
109
+ public void favorPathWithJafTurnedOff () throws Exception {
110
+ this .factoryBean .setFavorPathExtension (true );
111
+ this .factoryBean .setUseJaf (false );
91
112
this .factoryBean .afterPropertiesSet ();
92
113
ContentNegotiationManager manager = this .factoryBean .getObject ();
93
114
94
- this .servletRequest .setRequestURI ("/flower.json" );
95
- assertEquals (Arrays .asList (MediaType .APPLICATION_JSON ), manager .resolveMediaTypes (this .webRequest ));
115
+ this .servletRequest .setRequestURI ("/flower.foo" );
116
+ assertEquals (Collections .emptyList (), manager .resolveMediaTypes (this .webRequest ));
117
+
118
+ this .servletRequest .setRequestURI ("/flower.gif" );
119
+ assertEquals (Collections .emptyList (), manager .resolveMediaTypes (this .webRequest ));
96
120
}
97
121
98
122
// SPR-10170
99
123
100
124
@ Test (expected = HttpMediaTypeNotAcceptableException .class )
101
- public void favorPathExtensionWithUnknownMediaType () throws Exception {
125
+ public void favorPathWithIgnoreUnknownPathExtensionTurnedOff () throws Exception {
102
126
this .factoryBean .setFavorPathExtension (true );
103
127
this .factoryBean .setIgnoreUnknownPathExtensions (false );
104
128
this .factoryBean .afterPropertiesSet ();
@@ -124,7 +148,8 @@ public void favorParameter() throws Exception {
124
148
this .servletRequest .setRequestURI ("/flower" );
125
149
this .servletRequest .addParameter ("format" , "json" );
126
150
127
- assertEquals (Arrays .asList (MediaType .APPLICATION_JSON ), manager .resolveMediaTypes (this .webRequest ));
151
+ assertEquals (Collections .singletonList (MediaType .APPLICATION_JSON ),
152
+ manager .resolveMediaTypes (this .webRequest ));
128
153
}
129
154
130
155
// SPR-10170
@@ -159,26 +184,48 @@ public void setDefaultContentType() throws Exception {
159
184
this .factoryBean .afterPropertiesSet ();
160
185
ContentNegotiationManager manager = this .factoryBean .getObject ();
161
186
162
- assertEquals (Arrays .asList (MediaType .APPLICATION_JSON ), manager .resolveMediaTypes (this .webRequest ));
187
+ assertEquals (Collections .singletonList (MediaType .APPLICATION_JSON ),
188
+ manager .resolveMediaTypes (this .webRequest ));
163
189
164
190
// SPR-10513
165
191
166
192
this .servletRequest .addHeader ("Accept" , MediaType .ALL_VALUE );
167
193
168
- assertEquals (Arrays .asList (MediaType .APPLICATION_JSON ), manager .resolveMediaTypes (this .webRequest ));
194
+ assertEquals (Collections .singletonList (MediaType .APPLICATION_JSON ),
195
+ manager .resolveMediaTypes (this .webRequest ));
169
196
}
170
197
171
198
// SPR-12286
199
+
172
200
@ Test
173
201
public void setDefaultContentTypeWithStrategy () throws Exception {
174
202
this .factoryBean .setDefaultContentTypeStrategy (new FixedContentNegotiationStrategy (MediaType .APPLICATION_JSON ));
175
203
this .factoryBean .afterPropertiesSet ();
176
204
ContentNegotiationManager manager = this .factoryBean .getObject ();
177
205
178
- assertEquals (Arrays .asList (MediaType .APPLICATION_JSON ), manager .resolveMediaTypes (this .webRequest ));
206
+ assertEquals (Collections .singletonList (MediaType .APPLICATION_JSON ),
207
+ manager .resolveMediaTypes (this .webRequest ));
179
208
180
209
this .servletRequest .addHeader ("Accept" , MediaType .ALL_VALUE );
181
- assertEquals (Arrays .asList (MediaType .APPLICATION_JSON ), manager .resolveMediaTypes (this .webRequest ));
210
+ assertEquals (Collections .singletonList (MediaType .APPLICATION_JSON ),
211
+ manager .resolveMediaTypes (this .webRequest ));
212
+ }
213
+
214
+
215
+ private static class TestServletContext extends MockServletContext {
216
+
217
+ private final Map <String , String > mimeTypes = new HashMap <>();
218
+
219
+
220
+ public Map <String , String > getMimeTypes () {
221
+ return this .mimeTypes ;
222
+ }
223
+
224
+ @ Override
225
+ public String getMimeType (String filePath ) {
226
+ String extension = StringUtils .getFilenameExtension (filePath );
227
+ return getMimeTypes ().get (extension );
228
+ }
182
229
}
183
230
184
231
}
0 commit comments