Skip to content

Commit

Permalink
feat(ic-http-certification): add request method enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev committed Nov 28, 2024
1 parent 7e3f70d commit cea276c
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 98 deletions.
4 changes: 2 additions & 2 deletions examples/http-certification/custom-assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn create_asset_response(
let headers = get_asset_headers(additional_headers, body.len(), cel_expr);

HttpResponse::builder()
.with_status_code(200)
.with_status_code(StatusCode::OK)
.with_headers(headers)
.with_body(body)
.build()
Expand Down Expand Up @@ -569,7 +569,7 @@ fn create_uncertified_response() -> HttpResponse<'static> {
);

HttpResponse::builder()
.with_status_code(200)
.with_status_code(StatusCode::OK)
.with_headers(headers)
.with_body(body)
.build()
Expand Down
47 changes: 27 additions & 20 deletions examples/http-certification/json-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Some headers from this project have not been included:
To facilitate the consistent usage of these headers, there is a reusable `create_response` function used when creating responses:

```rust
fn create_response(status_code: u16, body: Vec<u8>) -> HttpResponse<'static> {
fn create_response(status_code: StatusCode, body: Vec<u8>) -> HttpResponse<'static> {
HttpResponse::builder()
.with_status_code(status_code)
.with_headers(vec![
Expand Down Expand Up @@ -270,25 +270,32 @@ fn certify_list_todos_response() {
)
.encode()
});
let mut response = create_response(200, body);
let mut response = create_response(StatusCode::OK, body);

certify_response(request, &mut response, &TODOS_TREE_PATH);
}

fn certify_not_allowed_todo_responses() {
["HEAD", "PUT", "PATCH", "OPTIONS", "TRACE", "CONNECT"]
.into_iter()
.for_each(|method| {
let request = HttpRequest::builder()
.with_method(method)
.with_url(TODOS_PATH)
.build();

let body = ErrorResponse::not_allowed().encode();
let mut response = create_response(405, body);

certify_response(request, &mut response, &TODOS_TREE_PATH);
});
[
Method::HEAD,
Method::PUT,
Method::PATCH,
Method::OPTIONS,
Method::TRACE,
Method::CONNECT,
]
.into_iter()
.for_each(|method| {
let request = HttpRequest::builder()
.with_method(method)
.with_url(TODOS_PATH)
.build();

let body = ErrorResponse::not_allowed().encode();
let mut response = create_response(StatusCode::METHOD_NOT_ALLOWED, body);

certify_response(request, &mut response, &TODOS_TREE_PATH);
});
}
```

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

let tree_path = HttpCertificationPath::wildcard(NOT_FOUND_PATH);

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

```rust
fn no_update_call_handler(_http_request: &HttpRequest, _params: &Params) -> HttpResponse<'static> {
create_response(405, vec![])
create_response(StatusCode::BAD_REQUEST, vec![])
}
```

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

let body = CreateTodoItemResponse::ok(&todo_item).encode();
create_response(201, body)
create_response(StatusCode::CREATED, body)
}
```

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

let body = UpdateTodoItemResponse::ok(&()).encode();
create_response(200, body)
create_response(StatusCode::OK, body)
}
```

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

let body = DeleteTodoItemResponse::ok(&()).encode();
create_response(204, body)
create_response(StatusCode::NO_CONTENT, body)
}
```

Expand Down
43 changes: 27 additions & 16 deletions examples/http-certification/json-api/src/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ic_http_certification::{
utils::add_v2_certificate_header, DefaultCelBuilder, DefaultFullCelExpression,
DefaultResponseCertification, DefaultResponseOnlyCelExpression, HttpCertification,
HttpCertificationPath, HttpCertificationTree, HttpCertificationTreeEntry, HttpRequest,
HttpResponse, StatusCode, CERTIFICATE_EXPRESSION_HEADER_NAME,
HttpResponse, Method, StatusCode, CERTIFICATE_EXPRESSION_HEADER_NAME,
};
use lazy_static::lazy_static;
use matchit::{Params, Router};
Expand Down Expand Up @@ -46,7 +46,9 @@ fn http_request(req: HttpRequest) -> HttpResponse<'static> {
let req_path = req.get_path().expect("Failed to get req path");

QUERY_ROUTER.with_borrow(|query_router| {
let method_router = query_router.get(&req.method().to_uppercase()).unwrap();
let method_router = query_router
.get(&req.method().as_str().to_uppercase())
.unwrap();
let handler_match = method_router.at(&req_path).unwrap();
let handler = handler_match.value;

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

UPDATE_ROUTER.with_borrow(|update_router| {
let method_router = update_router.get(&req.method().to_uppercase()).unwrap();
let method_router = update_router
.get(&req.method().as_str().to_uppercase())
.unwrap();
let handler_match = method_router.at(&req_path).unwrap();
let handler = handler_match.value;

Expand Down Expand Up @@ -182,19 +186,26 @@ fn certify_list_todos_response() {
}

fn certify_not_allowed_todo_responses() {
["HEAD", "PUT", "PATCH", "OPTIONS", "TRACE", "CONNECT"]
.into_iter()
.for_each(|method| {
let request = HttpRequest::builder()
.with_method(method)
.with_url(TODOS_PATH)
.build();

let body = ErrorResponse::not_allowed().encode();
let mut response = create_response(StatusCode::METHOD_NOT_ALLOWED, body);

certify_response(request, &mut response, &TODOS_TREE_PATH);
});
[
Method::HEAD,
Method::PUT,
Method::PATCH,
Method::OPTIONS,
Method::TRACE,
Method::CONNECT,
]
.into_iter()
.for_each(|method| {
let request = HttpRequest::builder()
.with_method(method)
.with_url(TODOS_PATH)
.build();

let body = ErrorResponse::not_allowed().encode();
let mut response = create_response(StatusCode::METHOD_NOT_ALLOWED, body);

certify_response(request, &mut response, &TODOS_TREE_PATH);
});
}

fn certify_not_found_response() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn http_request() -> HttpResponse<'static> {
#[update]
fn http_request_update() -> HttpUpdateResponse<'static> {
HttpResponse::builder()
.with_status_code(418)
.with_status_code(StatusCode::IM_A_TEAPOT)
.with_body(b"I'm a teapot")
.build_update()
}
Expand Down
Loading

0 comments on commit cea276c

Please sign in to comment.