Skip to content

Commit 911afa6

Browse files
authored
feat(ic-http-certification): add http response convenience methods (#398)
Most changes are just using the new convenience methods, I've commented the main file were the meaningful changes are.
1 parent 673d319 commit 911afa6

File tree

11 files changed

+644
-298
lines changed

11 files changed

+644
-298
lines changed

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ fn create_asset_response(
158158
) -> HttpResponse {
159159
let headers = get_asset_headers(additional_headers, body.len(), cel_expr);
160160

161-
HttpResponse::builder()
162-
.with_status_code(StatusCode::OK)
163-
.with_headers(headers)
164-
.with_body(body)
165-
.build()
161+
HttpResponse::ok(body, headers).build()
166162
}
167163
```
168164

@@ -544,11 +540,7 @@ fn create_uncertified_response() -> HttpResponse<'static> {
544540
DefaultCelBuilder::skip_certification().to_string(),
545541
);
546542

547-
HttpResponse::builder()
548-
.with_status_code(StatusCode::OK)
549-
.with_headers(headers)
550-
.with_body(body)
551-
.build()
543+
HttpResponse::ok(body, headers).build()
552544
}
553545
```
554546

examples/http-certification/custom-assets/src/backend/src/lib.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ic_cdk::{
55
use ic_http_certification::{
66
utils::add_v2_certificate_header, DefaultCelBuilder, DefaultResponseCertification,
77
DefaultResponseOnlyCelExpression, HeaderField, HttpCertification, HttpCertificationPath,
8-
HttpCertificationTree, HttpCertificationTreeEntry, HttpRequest, HttpResponse, StatusCode,
8+
HttpCertificationTree, HttpCertificationTreeEntry, HttpRequest, HttpResponse,
99
CERTIFICATE_EXPRESSION_HEADER_NAME,
1010
};
1111
use include_dir::{include_dir, Dir};
@@ -375,11 +375,7 @@ fn create_uncertified_response() -> HttpResponse<'static> {
375375
DefaultCelBuilder::skip_certification().to_string(),
376376
);
377377

378-
HttpResponse::builder()
379-
.with_status_code(StatusCode::OK)
380-
.with_headers(headers)
381-
.with_body(body)
382-
.build()
378+
HttpResponse::ok(body, headers).build()
383379
}
384380

385381
fn get_asset_headers(
@@ -412,9 +408,5 @@ fn create_asset_response(
412408
) -> HttpResponse {
413409
let headers = get_asset_headers(additional_headers, body.len(), cel_expr);
414410

415-
HttpResponse::builder()
416-
.with_status_code(StatusCode::OK)
417-
.with_headers(headers)
418-
.with_body(body)
419-
.build()
411+
HttpResponse::ok(body, headers).build()
420412
}

examples/http-certification/skip-certification/src/backend/src/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ic_cdk::{
44
};
55
use ic_http_certification::{
66
utils::{add_skip_certification_header, skip_certification_certified_data},
7-
HttpResponse, StatusCode,
7+
HttpResponse,
88
};
99

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

57-
HttpResponse::builder()
58-
.with_status_code(StatusCode::OK)
59-
.with_headers(headers)
60-
.with_body(body)
61-
.build()
57+
HttpResponse::ok(body, headers).build()
6258
}

packages/ic-http-certification-tests/tests/v1_response_verification.rs

+56-56
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod tests {
22
use ic_certificate_verification::CertificateVerificationError;
33
use ic_certification_testing::{CertificateBuilder, CertificateData};
4-
use ic_http_certification::{HttpRequest, HttpResponse, StatusCode, CERTIFICATE_HEADER_NAME};
4+
use ic_http_certification::{HttpRequest, HttpResponse, CERTIFICATE_HEADER_NAME};
55
use ic_response_verification::types::{VerificationInfo, VerifiedResponse};
66
use ic_response_verification::verify_request_response_pair;
77
use ic_response_verification::ResponseVerificationError;
@@ -41,11 +41,11 @@ mod tests {
4141

4242
let request = HttpRequest::get(path).build();
4343

44-
let response = HttpResponse::builder()
45-
.with_status_code(StatusCode::OK)
46-
.with_body(body.as_bytes())
47-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
48-
.build();
44+
let response = HttpResponse::ok(
45+
body.as_bytes(),
46+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
47+
)
48+
.build();
4949
let expected_response = VerifiedResponse {
5050
status_code: None,
5151
body: response.body().to_vec(),
@@ -99,11 +99,11 @@ mod tests {
9999

100100
let request = HttpRequest::get(encoded_path).build();
101101

102-
let response = HttpResponse::builder()
103-
.with_status_code(StatusCode::OK)
104-
.with_body(body.as_bytes())
105-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
106-
.build();
102+
let response = HttpResponse::ok(
103+
body.as_bytes(),
104+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
105+
)
106+
.build();
107107
let expected_response = VerifiedResponse {
108108
status_code: None,
109109
body: response.body().to_vec(),
@@ -156,11 +156,11 @@ mod tests {
156156

157157
let request = HttpRequest::get("/").build();
158158

159-
let response = HttpResponse::builder()
160-
.with_status_code(StatusCode::OK)
161-
.with_body(body.as_bytes())
162-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
163-
.build();
159+
let response = HttpResponse::ok(
160+
body.as_bytes(),
161+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
162+
)
163+
.build();
164164
let expected_response = VerifiedResponse {
165165
status_code: None,
166166
body: response.body().to_vec(),
@@ -213,11 +213,11 @@ mod tests {
213213

214214
let request = HttpRequest::get(path).build();
215215

216-
let response = HttpResponse::builder()
217-
.with_status_code(StatusCode::OK)
218-
.with_body(b"Hello IC!")
219-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
220-
.build();
216+
let response = HttpResponse::ok(
217+
b"Hello IC!",
218+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
219+
)
220+
.build();
221221

222222
let result = verify_request_response_pair(
223223
request,
@@ -262,11 +262,11 @@ mod tests {
262262

263263
let request = HttpRequest::get(path).build();
264264

265-
let response = HttpResponse::builder()
266-
.with_status_code(StatusCode::OK)
267-
.with_body(body.as_bytes())
268-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
269-
.build();
265+
let response = HttpResponse::ok(
266+
body.as_bytes(),
267+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
268+
)
269+
.build();
270270

271271
let result = verify_request_response_pair(
272272
request,
@@ -315,11 +315,11 @@ mod tests {
315315

316316
let request = HttpRequest::get(path).build();
317317

318-
let response = HttpResponse::builder()
319-
.with_status_code(StatusCode::OK)
320-
.with_body(body.as_bytes())
321-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
322-
.build();
318+
let response = HttpResponse::ok(
319+
body.as_bytes(),
320+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
321+
)
322+
.build();
323323

324324
let result = verify_request_response_pair(
325325
request,
@@ -370,11 +370,11 @@ mod tests {
370370

371371
let request = HttpRequest::get(path).build();
372372

373-
let response = HttpResponse::builder()
374-
.with_status_code(StatusCode::OK)
375-
.with_body(body.as_bytes())
376-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
377-
.build();
373+
let response = HttpResponse::ok(
374+
body.as_bytes(),
375+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
376+
)
377+
.build();
378378

379379
let result = verify_request_response_pair(
380380
request,
@@ -423,11 +423,11 @@ mod tests {
423423

424424
let request = HttpRequest::get(path).build();
425425

426-
let response = HttpResponse::builder()
427-
.with_status_code(StatusCode::OK)
428-
.with_body(body.as_bytes())
429-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
430-
.build();
426+
let response = HttpResponse::ok(
427+
body.as_bytes(),
428+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
429+
)
430+
.build();
431431

432432
let result = verify_request_response_pair(
433433
request,
@@ -473,11 +473,11 @@ mod tests {
473473

474474
let request = HttpRequest::get(path).build();
475475

476-
let response = HttpResponse::builder()
477-
.with_status_code(StatusCode::OK)
478-
.with_body(body.as_bytes())
479-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
480-
.build();
476+
let response = HttpResponse::ok(
477+
body.as_bytes(),
478+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
479+
)
480+
.build();
481481

482482
let result = verify_request_response_pair(
483483
request,
@@ -521,11 +521,11 @@ mod tests {
521521

522522
let request = HttpRequest::get(path).build();
523523

524-
let response = HttpResponse::builder()
525-
.with_status_code(StatusCode::OK)
526-
.with_body(body.as_bytes())
527-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
528-
.build();
524+
let response = HttpResponse::ok(
525+
body.as_bytes(),
526+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
527+
)
528+
.build();
529529

530530
let result = verify_request_response_pair(
531531
request,
@@ -569,11 +569,11 @@ mod tests {
569569

570570
let request = HttpRequest::get(path).build();
571571

572-
let response = HttpResponse::builder()
573-
.with_status_code(StatusCode::OK)
574-
.with_body(body.as_bytes())
575-
.with_headers(vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)])
576-
.build();
572+
let response = HttpResponse::ok(
573+
body.as_bytes(),
574+
vec![(CERTIFICATE_HEADER_NAME.into(), certificate_header)],
575+
)
576+
.build();
577577

578578
let result = verify_request_response_pair(
579579
request,

0 commit comments

Comments
 (0)