Skip to content

Commit

Permalink
Add search-by-path endpoint at /search/QUERY (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Sep 20, 2022
1 parent fa2588b commit 1971363
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
57 changes: 50 additions & 7 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ impl Server {
.route("/ordinal/:ordinal", get(Self::ordinal))
.route("/output/:output", get(Self::output))
.route("/range/:start/:end", get(Self::range))
.route("/search", get(Self::search))
.route("/search", get(Self::search_by_query))
.route("/search/:query", get(Self::search_by_path))
.route("/static/*path", get(Self::static_asset))
.route("/status", get(Self::status))
.route("/tx/:txid", get(Self::transaction))
Expand Down Expand Up @@ -422,7 +423,11 @@ impl Server {
)
}

async fn search(search: extract::Query<Search>) -> Redirect {
async fn search_by_query(search: extract::Query<Search>) -> Redirect {
Redirect::to(&format!("/ordinal/{}", search.query))
}

async fn search_by_path(search: extract::Path<Search>) -> Redirect {
Redirect::to(&format!("/ordinal/{}", search.query))
}

Expand Down Expand Up @@ -513,7 +518,7 @@ mod tests {
.port();

let (options, server) = parse_server_args(&format!(
"ord --chain regtest --rpc-url http://127.0.0.1:{} --cookie-file {} --data-dir {} server --http-port {}",
"ord --chain regtest --rpc-url http://127.0.0.1:{} --cookie-file {} --data-dir {} server --http-port {} --address 127.0.0.1",
bitcoin_rpc_server_handle.port,
cookiefile.to_str().unwrap(),
tempdir.path().to_str().unwrap(),
Expand Down Expand Up @@ -542,8 +547,8 @@ mod tests {
}
}

fn url_with_path(&self, path: &str) -> String {
format!("http://127.0.0.1:{}/{path}", self.port)
fn join_url(&self, url: &str) -> String {
format!("http://127.0.0.1:{}/{url}", self.port)
}
}

Expand Down Expand Up @@ -722,7 +727,7 @@ mod tests {
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap()
.get(test_server.url_with_path("bounties"))
.get(test_server.join_url("bounties"))
.send()
.unwrap();

Expand All @@ -741,7 +746,7 @@ mod tests {
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap()
.get(test_server.url_with_path("faq"))
.get(test_server.join_url("faq"))
.send()
.unwrap();

Expand All @@ -751,4 +756,42 @@ mod tests {
"https://docs.ordinals.com/faq/"
);
}

#[test]
fn search_by_query_returns_ordinal() {
let test_server = TestServer::new();

let response = reqwest::blocking::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap()
.get(test_server.join_url("search?query=0"))
.send()
.unwrap();

assert_eq!(response.status(), StatusCode::SEE_OTHER);
assert_eq!(
response.headers().get(header::LOCATION).unwrap(),
"/ordinal/0"
);
}

#[test]
fn search_by_path_returns_ordinal() {
let test_server = TestServer::new();

let response = reqwest::blocking::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap()
.get(test_server.join_url("search/0"))
.send()
.unwrap();

assert_eq!(response.status(), StatusCode::SEE_OTHER);
assert_eq!(
response.headers().get(header::LOCATION).unwrap(),
"/ordinal/0"
);
}
}
5 changes: 0 additions & 5 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,3 @@ fn clock_is_served_with_svg_extension() {
let mut state = State::new();
state.request_regex("clock.svg", 200, "<svg.*");
}

#[test]
fn search_returns_ordinal() {
State::new().request_regex("search?query=0", 200, ".*<h1>Ordinal 0</h1>.*");
}

0 comments on commit 1971363

Please sign in to comment.