Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ic-http-certification): add http response convenience methods #398

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions examples/http-certification/custom-assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,7 @@ fn create_asset_response(
) -> HttpResponse {
let headers = get_asset_headers(additional_headers, body.len(), cel_expr);

HttpResponse::builder()
.with_status_code(200)
.with_headers(headers)
.with_body(body)
.build()
HttpResponse::ok(body, headers).build()
}
```

Expand Down Expand Up @@ -568,11 +564,7 @@ fn create_uncertified_response() -> HttpResponse<'static> {
DefaultCelBuilder::skip_certification().to_string(),
);

HttpResponse::builder()
.with_status_code(200)
.with_headers(headers)
.with_body(body)
.build()
HttpResponse::ok(body, headers).build()
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ic_cdk::{
use ic_http_certification::{
utils::add_v2_certificate_header, DefaultCelBuilder, DefaultResponseCertification,
DefaultResponseOnlyCelExpression, HeaderField, HttpCertification, HttpCertificationPath,
HttpCertificationTree, HttpCertificationTreeEntry, HttpRequest, HttpResponse, StatusCode,
HttpCertificationTree, HttpCertificationTreeEntry, HttpRequest, HttpResponse,
CERTIFICATE_EXPRESSION_HEADER_NAME,
};
use include_dir::{include_dir, Dir};
Expand Down Expand Up @@ -398,11 +398,7 @@ fn create_uncertified_response() -> HttpResponse<'static> {
DefaultCelBuilder::skip_certification().to_string(),
);

HttpResponse::builder()
.with_status_code(StatusCode::OK)
.with_headers(headers)
.with_body(body)
.build()
HttpResponse::ok(body, headers).build()
}

fn get_asset_headers(
Expand Down Expand Up @@ -435,9 +431,5 @@ fn create_asset_response(
) -> HttpResponse {
let headers = get_asset_headers(additional_headers, body.len(), cel_expr);

HttpResponse::builder()
.with_status_code(StatusCode::OK)
.with_headers(headers)
.with_body(body)
.build()
HttpResponse::ok(body, headers).build()
}
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 @@ -4,7 +4,7 @@ use ic_cdk::{
};
use ic_http_certification::{
utils::{add_skip_certification_header, skip_certification_certified_data},
HttpResponse, StatusCode,
HttpResponse,
};

#[init]
Expand Down Expand Up @@ -54,9 +54,5 @@ fn create_response() -> HttpResponse<'static> {
("content-type".to_string(), "text/html".to_string())
];

HttpResponse::builder()
.with_status_code(StatusCode::OK)
.with_headers(headers)
.with_body(body)
.build()
HttpResponse::ok(body, headers).build()
}
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