@@ -142,7 +142,7 @@ Some headers from this project have not been included:
142
142
To facilitate the consistent usage of these headers, there is a reusable ` create_response ` function used when creating responses:
143
143
144
144
``` rust
145
- fn create_response (status_code : u16 , body : Vec <u8 >) -> HttpResponse <'static > {
145
+ fn create_response (status_code : StatusCode , body : Vec <u8 >) -> HttpResponse <'static > {
146
146
HttpResponse :: builder ()
147
147
. with_status_code (status_code )
148
148
. with_headers (vec! [
@@ -270,25 +270,32 @@ fn certify_list_todos_response() {
270
270
)
271
271
. encode ()
272
272
});
273
- let mut response = create_response (200 , body );
273
+ let mut response = create_response (StatusCode :: OK , body );
274
274
275
275
certify_response (request , & mut response , & TODOS_TREE_PATH );
276
276
}
277
277
278
278
fn certify_not_allowed_todo_responses () {
279
- [" HEAD" , " PUT" , " PATCH" , " OPTIONS" , " TRACE" , " CONNECT" ]
280
- . into_iter ()
281
- . for_each (| method | {
282
- let request = HttpRequest :: builder ()
283
- . with_method (method )
284
- . with_url (TODOS_PATH )
285
- . build ();
286
-
287
- let body = ErrorResponse :: not_allowed (). encode ();
288
- let mut response = create_response (405 , body );
289
-
290
- certify_response (request , & mut response , & TODOS_TREE_PATH );
291
- });
279
+ [
280
+ Method :: HEAD ,
281
+ Method :: PUT ,
282
+ Method :: PATCH ,
283
+ Method :: OPTIONS ,
284
+ Method :: TRACE ,
285
+ Method :: CONNECT ,
286
+ ]
287
+ . into_iter ()
288
+ . for_each (| method | {
289
+ let request = HttpRequest :: builder ()
290
+ . with_method (method )
291
+ . with_url (TODOS_PATH )
292
+ . build ();
293
+
294
+ let body = ErrorResponse :: not_allowed (). encode ();
295
+ let mut response = create_response (StatusCode :: METHOD_NOT_ALLOWED , body );
296
+
297
+ certify_response (request , & mut response , & TODOS_TREE_PATH );
298
+ });
292
299
}
293
300
```
294
301
@@ -301,7 +308,7 @@ Certifying the "Not found" response requires a slightly different procedure. Thi
301
308
``` rust
302
309
fn certify_not_found_response () {
303
310
let body = ErrorResponse :: not_found (). encode ();
304
- let mut response = create_response (404 , body );
311
+ let mut response = create_response (StatusCode :: NOT_FOUND , body );
305
312
306
313
let tree_path = HttpCertificationPath :: wildcard (NOT_FOUND_PATH );
307
314
@@ -401,7 +408,7 @@ When update calls are made to endpoints that do not update state, return an erro
401
408
402
409
``` rust
403
410
fn no_update_call_handler (_http_request : & HttpRequest , _params : & Params ) -> HttpResponse <'static > {
404
- create_response (405 , vec! [])
411
+ create_response (StatusCode :: BAD_REQUEST , vec! [])
405
412
}
406
413
```
407
414
@@ -447,7 +454,7 @@ fn create_todo_item_handler(req: &HttpRequest, _params: &Params) -> HttpResponse
447
454
certify_list_todos_response ();
448
455
449
456
let body = CreateTodoItemResponse :: ok (& todo_item ). encode ();
450
- create_response (201 , body )
457
+ create_response (StatusCode :: CREATED , body )
451
458
}
452
459
```
453
460
@@ -473,7 +480,7 @@ fn update_todo_item_handler(req: &HttpRequest, params: &Params) -> HttpResponse<
473
480
certify_list_todos_response ();
474
481
475
482
let body = UpdateTodoItemResponse :: ok (& ()). encode ();
476
- create_response (200 , body )
483
+ create_response (StatusCode :: OK , body )
477
484
}
478
485
```
479
486
@@ -490,7 +497,7 @@ fn delete_todo_item_handler(_req: &HttpRequest, params: &Params) -> HttpResponse
490
497
certify_list_todos_response ();
491
498
492
499
let body = DeleteTodoItemResponse :: ok (& ()). encode ();
493
- create_response (204 , body )
500
+ create_response (StatusCode :: NO_CONTENT , body )
494
501
}
495
502
```
496
503
0 commit comments