@@ -27,7 +27,7 @@ const BROTLI_ENCODING: &str = "br";
27
27
const PATH_INFO_HEADER : & str = "spin-path-info" ;
28
28
29
29
/// Common Content Encodings
30
- #[ derive( PartialEq ) ]
30
+ #[ derive( PartialEq , Debug ) ]
31
31
pub enum ContentEncoding {
32
32
Brotli ,
33
33
//Deflate, // Could use flate2 for this
@@ -156,3 +156,115 @@ impl FileServer {
156
156
state. finish ( ) . to_string ( )
157
157
}
158
158
}
159
+
160
+ #[ cfg( test) ]
161
+ mod tests {
162
+ use http:: header:: { ACCEPT_ENCODING , IF_NONE_MATCH } ;
163
+
164
+ use super :: * ;
165
+
166
+ #[ test]
167
+ fn test_best_encoding_none ( ) {
168
+ let req = http:: Request :: builder ( )
169
+ . uri ( "http://thisistest.com" )
170
+ . body ( Some ( bytes:: Bytes :: default ( ) ) )
171
+ . unwrap ( ) ;
172
+ let enc = ContentEncoding :: best_encoding ( & req) . unwrap ( ) ;
173
+ assert_eq ! ( enc, ContentEncoding :: None ) ;
174
+ }
175
+
176
+ #[ test]
177
+ fn test_best_encoding_not_br ( ) {
178
+ let req = http:: Request :: builder ( )
179
+ . uri ( "http://thisistest.com" )
180
+ . header ( ACCEPT_ENCODING , "gzip" )
181
+ . body ( Some ( bytes:: Bytes :: default ( ) ) )
182
+ . unwrap ( ) ;
183
+ let enc = ContentEncoding :: best_encoding ( & req) . unwrap ( ) ;
184
+ assert_eq ! ( enc, ContentEncoding :: None ) ;
185
+ }
186
+
187
+ #[ test]
188
+ fn test_best_encoding_with_br ( ) {
189
+ let req = http:: Request :: builder ( )
190
+ . uri ( "http://thisistest.com" )
191
+ . header ( ACCEPT_ENCODING , "gzip,br" )
192
+ . body ( Some ( bytes:: Bytes :: default ( ) ) )
193
+ . unwrap ( ) ;
194
+ let enc = ContentEncoding :: best_encoding ( & req) . unwrap ( ) ;
195
+ assert_eq ! ( enc, ContentEncoding :: Brotli ) ;
196
+ }
197
+
198
+ #[ test]
199
+ fn test_serve_file_found ( ) {
200
+ let req = spin_http:: Request {
201
+ method : spin_http:: Method :: Get ,
202
+ uri : "http://thisistest.com" . to_string ( ) ,
203
+ headers : vec ! [ (
204
+ PATH_INFO_HEADER . to_string( ) ,
205
+ "./content/hello-test.txt" . to_string( ) ,
206
+ ) ] ,
207
+ params : vec ! [ ] ,
208
+ body : None ,
209
+ } ;
210
+ let rsp = <super :: SpinHttp as spin_http:: SpinHttp >:: handle_http_request ( req) ;
211
+ assert_eq ! ( rsp. status, 200 ) ;
212
+ }
213
+
214
+ #[ test]
215
+ fn test_serve_with_etag ( ) {
216
+ let req = spin_http:: Request {
217
+ method : spin_http:: Method :: Get ,
218
+ uri : "http://thisistest.com" . to_string ( ) ,
219
+ headers : vec ! [
220
+ (
221
+ PATH_INFO_HEADER . to_string( ) ,
222
+ "./content/hello-test.txt" . to_string( ) ,
223
+ ) ,
224
+ (
225
+ IF_NONE_MATCH . to_string( ) ,
226
+ "13946318585003701156" . to_string( ) ,
227
+ ) ,
228
+ ] ,
229
+ params : vec ! [ ] ,
230
+ body : None ,
231
+ } ;
232
+ let rsp = <super :: SpinHttp as spin_http:: SpinHttp >:: handle_http_request ( req) ;
233
+ assert_eq ! ( rsp. status, 304 ) ;
234
+ }
235
+
236
+ #[ test]
237
+ fn test_serve_with_not_matched_etag ( ) {
238
+ let req = spin_http:: Request {
239
+ method : spin_http:: Method :: Get ,
240
+ uri : "http://thisistest.com" . to_string ( ) ,
241
+ headers : vec ! [
242
+ (
243
+ PATH_INFO_HEADER . to_string( ) ,
244
+ "./content/hello-test.txt" . to_string( ) ,
245
+ ) ,
246
+ ( IF_NONE_MATCH . to_string( ) , "" . to_string( ) ) ,
247
+ ] ,
248
+ params : vec ! [ ] ,
249
+ body : None ,
250
+ } ;
251
+ let rsp = <super :: SpinHttp as spin_http:: SpinHttp >:: handle_http_request ( req) ;
252
+ assert_eq ! ( rsp. status, 200 ) ;
253
+ }
254
+
255
+ #[ test]
256
+ fn test_serve_file_not_found ( ) {
257
+ let req = spin_http:: Request {
258
+ method : spin_http:: Method :: Get ,
259
+ uri : "http://thisistest.com" . to_string ( ) ,
260
+ headers : vec ! [ (
261
+ PATH_INFO_HEADER . to_string( ) ,
262
+ "not-existent-file" . to_string( ) ,
263
+ ) ] ,
264
+ params : vec ! [ ] ,
265
+ body : None ,
266
+ } ;
267
+ let rsp = <super :: SpinHttp as spin_http:: SpinHttp >:: handle_http_request ( req) ;
268
+ assert_eq ! ( rsp. status, 404 ) ;
269
+ }
270
+ }
0 commit comments