From ccbd3ff2f99495d7b5e7cad1bd3a4798be225f0a Mon Sep 17 00:00:00 2001 From: nazeh Date: Sun, 22 Dec 2024 15:41:00 +0300 Subject: [PATCH] feat(js): pass all unit tests --- pubky/pkg/test/http.js | 24 ++++++++++++++++++++---- pubky/src/wasm/http.rs | 22 +++++++++++----------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/pubky/pkg/test/http.js b/pubky/pkg/test/http.js index abe0618..2119bca 100644 --- a/pubky/pkg/test/http.js +++ b/pubky/pkg/test/http.js @@ -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); +}) + diff --git a/pubky/src/wasm/http.rs b/pubky/src/wasm/http.rs index 88f536b..7e5a261 100644 --- a/pubky/src/wasm/http.rs +++ b/pubky/src/wasm/http.rs @@ -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 = 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()); } } }