Skip to content

Commit

Permalink
Use a single prefix during test
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraSuegami committed Nov 5, 2024
1 parent fa07a6d commit 57167b4
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/dns_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ pub async fn get_dkim_public_key(
return Err("Invalid selector".to_string());
}

#[cfg(not(debug_assertions))]
let prefixes = vec![
"https://cloudflare-dns.com/dns-query",
"https://dns.google/resolve",
"https://dns.nextdns.io/dns-query",
];
#[cfg(debug_assertions)]
let prefixes = vec!["https://dns.google/resolve"];

let mut errors = vec![];
for prefix in prefixes {
Expand All @@ -79,7 +82,7 @@ pub async fn get_dkim_public_key(
"[Access to {prefix}] The http_request resulted into error. RejectionCode: {r:?}, Error: {m}."
);
errors.push(message);

continue;
// Return the error as a string and end the method.
// return Err(message);
}
Expand All @@ -102,7 +105,12 @@ pub async fn get_dkim_public_key(
fn transform(raw: TransformArgs) -> HttpResponse {
match _transform(raw) {
Ok(res) => res,
Err(e) => panic!("{}", e),
Err(e) => HttpResponse {
status: Nat::from(500u64),
body: e.as_bytes().to_vec(),
headers: _transform_headers(),
..Default::default()
},
}
}

Expand Down Expand Up @@ -145,33 +153,6 @@ fn _construct_request(prefix: &str, selector: &str, domain: &str) -> CanisterHtt
///
/// A result containing the structured `HttpResponse`, or an error message.
fn _transform(raw: TransformArgs) -> Result<HttpResponse, String> {
let headers = vec![
HttpHeader {
name: "Content-Security-Policy".to_string(),
value: "default-src 'self'".to_string(),
},
HttpHeader {
name: "Referrer-Policy".to_string(),
value: "strict-origin".to_string(),
},
HttpHeader {
name: "Permissions-Policy".to_string(),
value: "geolocation=(self)".to_string(),
},
HttpHeader {
name: "Strict-Transport-Security".to_string(),
value: "max-age=63072000".to_string(),
},
HttpHeader {
name: "X-Frame-Options".to_string(),
value: "DENY".to_string(),
},
HttpHeader {
name: "X-Content-Type-Options".to_string(),
value: "nosniff".to_string(),
},
];

if raw.response.status != Nat::from(200u64) {
return Err(format!(
"Received an error with code {} from google dns: err = {:?}",
Expand Down Expand Up @@ -209,14 +190,43 @@ fn _transform(raw: TransformArgs) -> Result<HttpResponse, String> {
return Ok(HttpResponse {
status: raw.response.status.clone(),
body: pubkey_bytes.n().to_bytes_be(),
headers,
headers: _transform_headers(),
..Default::default()
});
}
}
Err("No key found".to_string())
}

fn _transform_headers() -> Vec<HttpHeader> {
vec![
HttpHeader {
name: "Content-Security-Policy".to_string(),
value: "default-src 'self'".to_string(),
},
HttpHeader {
name: "Referrer-Policy".to_string(),
value: "strict-origin".to_string(),
},
HttpHeader {
name: "Permissions-Policy".to_string(),
value: "geolocation=(self)".to_string(),
},
HttpHeader {
name: "Strict-Transport-Security".to_string(),
value: "max-age=63072000".to_string(),
},
HttpHeader {
name: "X-Frame-Options".to_string(),
value: "DENY".to_string(),
},
HttpHeader {
name: "X-Content-Type-Options".to_string(),
value: "nosniff".to_string(),
},
]
}

ic_cdk::export_candid!();

#[cfg(test)]
Expand Down

0 comments on commit 57167b4

Please sign in to comment.