Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /range/:start/:end endpoint #291

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl Server {
let app = Router::new()
.route("/", get(Self::root))
.route("/block/:hash", get(Self::block))
.route("/range/:start/:end", get(Self::range))
.route("/ordinal/:ordinal", get(Self::ordinal))
.route("/api/list/:outpoint", get(Self::api_list))
.route("/status", get(Self::status))
Expand Down Expand Up @@ -135,6 +136,28 @@ impl Server {
(StatusCode::OK, Html(format!("{ordinal}")))
}

async fn range(
extract::Path((DeserializeOrdinalFromStr(start), DeserializeOrdinalFromStr(end))): extract::Path<
(DeserializeOrdinalFromStr, DeserializeOrdinalFromStr),
>,
) -> impl IntoResponse {
if start == end {
return (StatusCode::BAD_REQUEST, Html("Empty Range".to_string()));
}

if start > end {
return (
StatusCode::BAD_REQUEST,
Html("Range Start Greater Than Range End".to_string()),
);
}

(
StatusCode::OK,
Html(format!("<a href='/ordinal/{start}'>first</a>")),
)
}

async fn root(index: extract::Extension<Arc<Index>>) -> impl IntoResponse {
match index.all() {
Ok(blocks) => (
Expand Down
58 changes: 58 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,64 @@ fn continuously_index_ranges() -> Result {
.run_server(port)
}

#[test]
fn range_end_before_range_start_returns_400() -> Result {
let port = free_port()?;

Test::new()?
.command(&format!("server --address 127.0.0.1 --http-port {port}"))
.request("range/1/0", 400, "Range Start Greater Than Range End")
.run_server(port)
}

#[test]
fn invalid_range_start_returns_400() -> Result {
let port = free_port()?;

Test::new()?
.command(&format!("server --address 127.0.0.1 --http-port {port}"))
.request(
"range/foo/0",
400,
"Invalid URL: invalid digit found in string",
)
.run_server(port)
}

#[test]
fn invalid_range_end_returns_400() -> Result {
let port = free_port()?;

Test::new()?
.command(&format!("server --address 127.0.0.1 --http-port {port}"))
.request(
"range/0/foo",
400,
"Invalid URL: invalid digit found in string",
)
.run_server(port)
}

#[test]
fn empty_range_returns_400() -> Result {
let port = free_port()?;

Test::new()?
.command(&format!("server --address 127.0.0.1 --http-port {port}"))
.request("range/0/0", 400, "Empty Range")
.run_server(port)
}

#[test]
fn range_links_to_first() -> Result {
let port = free_port()?;

Test::new()?
.command(&format!("server --address 127.0.0.1 --http-port {port}"))
.request("range/0/1", 200, "<a href='/ordinal/0'>first</a>")
.run_server(port)
}

#[test]
fn ordinal_number() -> Result {
let port = free_port()?;
Expand Down