Skip to content

Commit 3644d3f

Browse files
authored
feat(ic-http-certification): add request method enum (#397)
1 parent 7e3f70d commit 3644d3f

File tree

7 files changed

+165
-98
lines changed

7 files changed

+165
-98
lines changed

examples/http-certification/custom-assets/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn create_asset_response(
173173
let headers = get_asset_headers(additional_headers, body.len(), cel_expr);
174174

175175
HttpResponse::builder()
176-
.with_status_code(200)
176+
.with_status_code(StatusCode::OK)
177177
.with_headers(headers)
178178
.with_body(body)
179179
.build()
@@ -569,7 +569,7 @@ fn create_uncertified_response() -> HttpResponse<'static> {
569569
);
570570

571571
HttpResponse::builder()
572-
.with_status_code(200)
572+
.with_status_code(StatusCode::OK)
573573
.with_headers(headers)
574574
.with_body(body)
575575
.build()

examples/http-certification/json-api/README.md

+27-20
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Some headers from this project have not been included:
142142
To facilitate the consistent usage of these headers, there is a reusable `create_response` function used when creating responses:
143143

144144
```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> {
146146
HttpResponse::builder()
147147
.with_status_code(status_code)
148148
.with_headers(vec![
@@ -270,25 +270,32 @@ fn certify_list_todos_response() {
270270
)
271271
.encode()
272272
});
273-
let mut response = create_response(200, body);
273+
let mut response = create_response(StatusCode::OK, body);
274274

275275
certify_response(request, &mut response, &TODOS_TREE_PATH);
276276
}
277277

278278
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+
});
292299
}
293300
```
294301

@@ -301,7 +308,7 @@ Certifying the "Not found" response requires a slightly different procedure. Thi
301308
```rust
302309
fn certify_not_found_response() {
303310
let body = ErrorResponse::not_found().encode();
304-
let mut response = create_response(404, body);
311+
let mut response = create_response(StatusCode::NOT_FOUND, body);
305312

306313
let tree_path = HttpCertificationPath::wildcard(NOT_FOUND_PATH);
307314

@@ -401,7 +408,7 @@ When update calls are made to endpoints that do not update state, return an erro
401408

402409
```rust
403410
fn no_update_call_handler(_http_request: &HttpRequest, _params: &Params) -> HttpResponse<'static> {
404-
create_response(405, vec![])
411+
create_response(StatusCode::BAD_REQUEST, vec![])
405412
}
406413
```
407414

@@ -447,7 +454,7 @@ fn create_todo_item_handler(req: &HttpRequest, _params: &Params) -> HttpResponse
447454
certify_list_todos_response();
448455

449456
let body = CreateTodoItemResponse::ok(&todo_item).encode();
450-
create_response(201, body)
457+
create_response(StatusCode::CREATED, body)
451458
}
452459
```
453460

@@ -473,7 +480,7 @@ fn update_todo_item_handler(req: &HttpRequest, params: &Params) -> HttpResponse<
473480
certify_list_todos_response();
474481

475482
let body = UpdateTodoItemResponse::ok(&()).encode();
476-
create_response(200, body)
483+
create_response(StatusCode::OK, body)
477484
}
478485
```
479486

@@ -490,7 +497,7 @@ fn delete_todo_item_handler(_req: &HttpRequest, params: &Params) -> HttpResponse
490497
certify_list_todos_response();
491498

492499
let body = DeleteTodoItemResponse::ok(&()).encode();
493-
create_response(204, body)
500+
create_response(StatusCode::NO_CONTENT, body)
494501
}
495502
```
496503

examples/http-certification/json-api/src/backend/src/lib.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ic_http_certification::{
66
utils::add_v2_certificate_header, DefaultCelBuilder, DefaultFullCelExpression,
77
DefaultResponseCertification, DefaultResponseOnlyCelExpression, HttpCertification,
88
HttpCertificationPath, HttpCertificationTree, HttpCertificationTreeEntry, HttpRequest,
9-
HttpResponse, StatusCode, CERTIFICATE_EXPRESSION_HEADER_NAME,
9+
HttpResponse, Method, StatusCode, CERTIFICATE_EXPRESSION_HEADER_NAME,
1010
};
1111
use lazy_static::lazy_static;
1212
use matchit::{Params, Router};
@@ -46,7 +46,9 @@ fn http_request(req: HttpRequest) -> HttpResponse<'static> {
4646
let req_path = req.get_path().expect("Failed to get req path");
4747

4848
QUERY_ROUTER.with_borrow(|query_router| {
49-
let method_router = query_router.get(&req.method().to_uppercase()).unwrap();
49+
let method_router = query_router
50+
.get(&req.method().as_str().to_uppercase())
51+
.unwrap();
5052
let handler_match = method_router.at(&req_path).unwrap();
5153
let handler = handler_match.value;
5254

@@ -59,7 +61,9 @@ fn http_request_update(req: HttpRequest) -> HttpResponse<'static> {
5961
let req_path = req.get_path().expect("Failed to get req path");
6062

6163
UPDATE_ROUTER.with_borrow(|update_router| {
62-
let method_router = update_router.get(&req.method().to_uppercase()).unwrap();
64+
let method_router = update_router
65+
.get(&req.method().as_str().to_uppercase())
66+
.unwrap();
6367
let handler_match = method_router.at(&req_path).unwrap();
6468
let handler = handler_match.value;
6569

@@ -182,19 +186,26 @@ fn certify_list_todos_response() {
182186
}
183187

184188
fn certify_not_allowed_todo_responses() {
185-
["HEAD", "PUT", "PATCH", "OPTIONS", "TRACE", "CONNECT"]
186-
.into_iter()
187-
.for_each(|method| {
188-
let request = HttpRequest::builder()
189-
.with_method(method)
190-
.with_url(TODOS_PATH)
191-
.build();
192-
193-
let body = ErrorResponse::not_allowed().encode();
194-
let mut response = create_response(StatusCode::METHOD_NOT_ALLOWED, body);
195-
196-
certify_response(request, &mut response, &TODOS_TREE_PATH);
197-
});
189+
[
190+
Method::HEAD,
191+
Method::PUT,
192+
Method::PATCH,
193+
Method::OPTIONS,
194+
Method::TRACE,
195+
Method::CONNECT,
196+
]
197+
.into_iter()
198+
.for_each(|method| {
199+
let request = HttpRequest::builder()
200+
.with_method(method)
201+
.with_url(TODOS_PATH)
202+
.build();
203+
204+
let body = ErrorResponse::not_allowed().encode();
205+
let mut response = create_response(StatusCode::METHOD_NOT_ALLOWED, body);
206+
207+
certify_response(request, &mut response, &TODOS_TREE_PATH);
208+
});
198209
}
199210

200211
fn certify_not_found_response() {

examples/http-certification/upgrade-to-update-call/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn http_request() -> HttpResponse<'static> {
3636
#[update]
3737
fn http_request_update() -> HttpUpdateResponse<'static> {
3838
HttpResponse::builder()
39-
.with_status_code(418)
39+
.with_status_code(StatusCode::IM_A_TEAPOT)
4040
.with_body(b"I'm a teapot")
4141
.build_update()
4242
}

0 commit comments

Comments
 (0)