feat(js): pass all unit tests

This commit is contained in:
nazeh
2024-12-22 15:41:00 +03:00
parent 4ccb06006a
commit ccbd3ff2f9
2 changed files with 31 additions and 15 deletions

View File

@@ -4,22 +4,38 @@ import { Client, Keypair, PublicKey } from '../index.cjs'
const TLD = '8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo';
// TODO: test HTTPs too somehow.
test("basic fetch", async (t) => {
let client = Client.testnet();
// Normal TLD
{
let response = await client.fetch(`http://relay.pkarr.org/`);
let response = await client.fetch(`https://relay.pkarr.org/`);
t.equal(response.status, 200);
}
// Pubky
let response = await client.fetch(`http://${TLD}/`);
let response = await client.fetch(`https://${TLD}/`);
t.equal(response.status, 200);
})
test("fetch failed", async (t) => {
let client = Client.testnet();
// Normal TLD
{
let response = await client.fetch(`https://nonexistent.domain/`).catch(e => e);
t.ok(response instanceof Error);
}
// Pubky
let response = await client.fetch(`https://1pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ew1/`).catch(e => e);
t.ok(response instanceof Error);
})

View File

@@ -111,10 +111,7 @@ impl Client {
self.transform_url(url).await;
pubky_host = Some(host);
log::debug!("Transformed URL to: {}", url.as_str());
};
log::debug!("Prepare request");
pubky_host
}
@@ -122,19 +119,19 @@ impl Client {
pub async fn transform_url(&self, url: &mut Url) {
let clone = url.clone();
let qname = clone.host_str().unwrap_or("");
log::debug!("Prepare request {}, {}", url.as_str(), qname);
log::debug!("Prepare request {}", url.as_str());
let mut stream = self.pkarr.resolve_https_endpoints(qname);
let mut so_far: Option<Endpoint> = None;
// TODO: currently we return the first thing we can see,
// in the future we might want to failover to other endpoints
while so_far.is_none() {
while let Some(endpoint) = stream.next().await {
if endpoint.domain() != "." {
so_far = Some(endpoint);
}
while let Some(endpoint) = stream.next().await {
if endpoint.domain() != "." {
so_far = Some(endpoint);
// TODO: currently we return the first thing we can see,
// in the future we might want to failover to other endpoints
break;
}
}
@@ -149,8 +146,11 @@ impl Client {
.expect("coultdn't use the resolved endpoint's domain");
url.set_port(Some(e.port()))
.expect("coultdn't use the resolved endpoint's port");
log::debug!("Transformed URL to: {}", url.as_str());
} else {
// TODO: didn't find any domain, what to do?
log::debug!("Could not resolve Pubky URL to clearnet {}", url.as_str());
}
}
}