Server: Avoids unwrap() in test

pull/300/head
Tpt 2 years ago committed by Thomas Tanon
parent 127ffc3547
commit ecd4b423dc
  1. 579
      server/src/main.rs

@ -143,9 +143,7 @@ pub fn main() -> anyhow::Result<()> {
.unwrap_or_else(|(status, message)| error(status, message)) .unwrap_or_else(|(status, message)| error(status, message))
}); });
server.set_global_timeout(HTTP_TIMEOUT); server.set_global_timeout(HTTP_TIMEOUT);
server server.set_server_name(concat!("Oxigraph/", env!("CARGO_PKG_VERSION")))?;
.set_server_name(concat!("Oxigraph/", env!("CARGO_PKG_VERSION")))
.unwrap();
eprintln!("Listening for requests at http://{}", &bind); eprintln!("Listening for requests at http://{}", &bind);
server.listen(bind)?; server.listen(bind)?;
Ok(()) Ok(())
@ -1152,318 +1150,279 @@ mod tests {
} }
#[test] #[test]
fn get_ui() { fn get_ui() -> Result<()> {
ServerTest::new().test_status( ServerTest::new()?.test_status(
Request::builder(Method::GET, "http://localhost/".parse().unwrap()).build(), Request::builder(Method::GET, "http://localhost/".parse()?).build(),
Status::OK, Status::OK,
) )
} }
#[test] #[test]
fn post_dataset_file() { fn post_dataset_file() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/store".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/store".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/trig") .with_header(HeaderName::CONTENT_TYPE, "application/trig")?
.unwrap()
.with_body("<http://example.com> <http://example.com> <http://example.com> ."); .with_body("<http://example.com> <http://example.com> <http://example.com> .");
ServerTest::new().test_status(request, Status::NO_CONTENT) ServerTest::new()?.test_status(request, Status::NO_CONTENT)
} }
#[test] #[test]
fn post_wrong_file() { fn post_wrong_file() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/store".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/store".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/trig") .with_header(HeaderName::CONTENT_TYPE, "application/trig")?
.unwrap()
.with_body("<http://example.com>"); .with_body("<http://example.com>");
ServerTest::new().test_status(request, Status::BAD_REQUEST) ServerTest::new()?.test_status(request, Status::BAD_REQUEST)
} }
#[test] #[test]
fn post_unsupported_file() { fn post_unsupported_file() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/store".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/store".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "text/foo") .with_header(HeaderName::CONTENT_TYPE, "text/foo")?
.unwrap()
.build(); .build();
ServerTest::new().test_status(request, Status::UNSUPPORTED_MEDIA_TYPE) ServerTest::new()?.test_status(request, Status::UNSUPPORTED_MEDIA_TYPE)
} }
#[test] #[test]
fn get_query() { fn get_query() -> Result<()> {
let server = ServerTest::new(); let server = ServerTest::new()?;
let request = Request::builder(Method::POST, "http://localhost/store".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/store".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/trig") .with_header(HeaderName::CONTENT_TYPE, "application/trig")?
.unwrap()
.with_body("<http://example.com> <http://example.com> <http://example.com> ."); .with_body("<http://example.com> <http://example.com> <http://example.com> .");
server.test_status(request, Status::NO_CONTENT); server.test_status(request, Status::NO_CONTENT)?;
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}" "http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}"
.parse() .parse()?,
.unwrap(),
) )
.with_header(HeaderName::ACCEPT, "text/csv") .with_header(HeaderName::ACCEPT, "text/csv")?
.unwrap()
.build(); .build();
server.test_body( server.test_body(
request, request,
"s,p,o\r\nhttp://example.com,http://example.com,http://example.com", "s,p,o\r\nhttp://example.com,http://example.com,http://example.com",
); )
} }
#[test] #[test]
fn get_query_accept_star() { fn get_query_accept_star() -> Result<()> {
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}" "http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}"
.parse() .parse()?,
.unwrap(),
) )
.with_header(HeaderName::ACCEPT, "*/*") .with_header(HeaderName::ACCEPT, "*/*")?
.unwrap()
.build(); .build();
ServerTest::new().test_body( ServerTest::new()?.test_body(
request, request,
"{\"head\":{\"vars\":[\"s\",\"p\",\"o\"]},\"results\":{\"bindings\":[]}}", "{\"head\":{\"vars\":[\"s\",\"p\",\"o\"]},\"results\":{\"bindings\":[]}}",
); )
} }
#[test] #[test]
fn get_query_accept_good() { fn get_query_accept_good() -> Result<()> {
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}" "http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}"
.parse() .parse()?,
.unwrap(),
) )
.with_header( .with_header(
HeaderName::ACCEPT, HeaderName::ACCEPT,
"application/sparql-results+json;charset=utf-8", "application/sparql-results+json;charset=utf-8",
) )?
.unwrap()
.build(); .build();
ServerTest::new().test_body( ServerTest::new()?.test_body(
request, request,
"{\"head\":{\"vars\":[\"s\",\"p\",\"o\"]},\"results\":{\"bindings\":[]}}", "{\"head\":{\"vars\":[\"s\",\"p\",\"o\"]},\"results\":{\"bindings\":[]}}",
); )
} }
#[test] #[test]
fn get_query_accept_bad() { fn get_query_accept_bad() -> Result<()> {
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/query?query=SELECT%20*%20WHERE%20{%20?s%20?p%20?o%20}" "http://localhost/query?query=SELECT%20*%20WHERE%20{%20?s%20?p%20?o%20}".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::ACCEPT, "application/foo") .with_header(HeaderName::ACCEPT, "application/foo")?
.unwrap()
.build(); .build();
ServerTest::new().test_status(request, Status::NOT_ACCEPTABLE); ServerTest::new()?.test_status(request, Status::NOT_ACCEPTABLE)
} }
#[test] #[test]
fn get_bad_query() { fn get_bad_query() -> Result<()> {
ServerTest::new().test_status( ServerTest::new()?.test_status(
Request::builder( Request::builder(Method::GET, "http://localhost/query?query=SELECT".parse()?).build(),
Method::GET,
"http://localhost/query?query=SELECT".parse().unwrap(),
)
.build(),
Status::BAD_REQUEST, Status::BAD_REQUEST,
); )
} }
#[test] #[test]
fn get_query_union_graph() { fn get_query_union_graph() -> Result<()> {
let server = ServerTest::new(); let server = ServerTest::new()?;
let request = Request::builder(Method::PUT, "http://localhost/store/1".parse().unwrap()) let request = Request::builder(Method::PUT, "http://localhost/store/1".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "text/turtle") .with_header(HeaderName::CONTENT_TYPE, "text/turtle")?
.unwrap()
.with_body("<http://example.com> <http://example.com> <http://example.com> ."); .with_body("<http://example.com> <http://example.com> <http://example.com> .");
server.test_status(request, Status::CREATED); server.test_status(request, Status::CREATED)?;
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}&union-default-graph" "http://localhost/query?query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}&union-default-graph"
.parse() .parse()
.unwrap(), ?,
).with_header(HeaderName::ACCEPT, "text/csv") ).with_header(HeaderName::ACCEPT, "text/csv")
.unwrap() ?
.build(); .build();
server.test_body( server.test_body(
request, request,
"s,p,o\r\nhttp://example.com,http://example.com,http://example.com", "s,p,o\r\nhttp://example.com,http://example.com,http://example.com",
); )
} }
#[test] #[test]
fn get_query_union_graph_in_url_and_urlencoded() { fn get_query_union_graph_in_url_and_urlencoded() -> Result<()> {
let server = ServerTest::new(); let server = ServerTest::new()?;
let request = Request::builder(Method::PUT, "http://localhost/store/1".parse().unwrap()) let request = Request::builder(Method::PUT, "http://localhost/store/1".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "text/turtle") .with_header(HeaderName::CONTENT_TYPE, "text/turtle")?
.unwrap()
.with_body("<http://example.com> <http://example.com> <http://example.com> ."); .with_body("<http://example.com> <http://example.com> <http://example.com> .");
server.test_status(request, Status::CREATED); server.test_status(request, Status::CREATED)?;
let request = Request::builder( let request = Request::builder(
Method::POST, Method::POST,
"http://localhost/query?union-default-graph" "http://localhost/query?union-default-graph".parse()?,
.parse()
.unwrap(),
) )
.with_header( .with_header(
HeaderName::CONTENT_TYPE, HeaderName::CONTENT_TYPE,
"application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
) )?
.unwrap() .with_header(HeaderName::ACCEPT, "text/csv")?
.with_header(HeaderName::ACCEPT, "text/csv")
.unwrap()
.with_body("query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}"); .with_body("query=SELECT%20?s%20?p%20?o%20WHERE%20{%20?s%20?p%20?o%20}");
server.test_body( server.test_body(
request, request,
"s,p,o\r\nhttp://example.com,http://example.com,http://example.com", "s,p,o\r\nhttp://example.com,http://example.com,http://example.com",
); )
} }
#[test] #[test]
fn get_query_union_graph_and_default_graph() { fn get_query_union_graph_and_default_graph() -> Result<()> {
ServerTest::new().test_status(Request::builder( ServerTest::new()?.test_status(Request::builder(
Method::GET, Method::GET,
"http://localhost/query?query=SELECT%20*%20WHERE%20{%20?s%20?p%20?o%20}&union-default-graph&default-graph-uri=http://example.com".parse() "http://localhost/query?query=SELECT%20*%20WHERE%20{%20?s%20?p%20?o%20}&union-default-graph&default-graph-uri=http://example.com".parse()
.unwrap(), ?,
).build(), Status::BAD_REQUEST); ).build(), Status::BAD_REQUEST)
} }
#[test] #[test]
fn get_without_query() { fn get_without_query() -> Result<()> {
ServerTest::new().test_status( ServerTest::new()?.test_status(
Request::builder(Method::GET, "http://localhost/query".parse().unwrap()).build(), Request::builder(Method::GET, "http://localhost/query".parse()?).build(),
Status::BAD_REQUEST, Status::BAD_REQUEST,
); )
} }
#[test] #[test]
fn post_query() { fn post_query() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/query".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/query".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/sparql-query") .with_header(HeaderName::CONTENT_TYPE, "application/sparql-query")?
.unwrap()
.with_body("SELECT * WHERE { ?s ?p ?o }"); .with_body("SELECT * WHERE { ?s ?p ?o }");
ServerTest::new().test_status(request, Status::OK) ServerTest::new()?.test_status(request, Status::OK)
} }
#[test] #[test]
fn post_bad_query() { fn post_bad_query() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/query".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/query".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/sparql-query") .with_header(HeaderName::CONTENT_TYPE, "application/sparql-query")?
.unwrap()
.with_body("SELECT"); .with_body("SELECT");
ServerTest::new().test_status(request, Status::BAD_REQUEST) ServerTest::new()?.test_status(request, Status::BAD_REQUEST)
} }
#[test] #[test]
fn post_unknown_query() { fn post_unknown_query() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/query".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/query".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/sparql-todo") .with_header(HeaderName::CONTENT_TYPE, "application/sparql-todo")?
.unwrap()
.with_body("SELECT"); .with_body("SELECT");
ServerTest::new().test_status(request, Status::UNSUPPORTED_MEDIA_TYPE) ServerTest::new()?.test_status(request, Status::UNSUPPORTED_MEDIA_TYPE)
} }
#[test] #[test]
fn post_federated_query_wikidata() { fn post_federated_query_wikidata() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/query".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/query".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/sparql-query") .with_header(HeaderName::CONTENT_TYPE, "application/sparql-query")
.unwrap().with_body("SELECT * WHERE { SERVICE <https://query.wikidata.org/sparql> { <https://en.wikipedia.org/wiki/Paris> ?p ?o } }"); ?.with_body("SELECT * WHERE { SERVICE <https://query.wikidata.org/sparql> { <https://en.wikipedia.org/wiki/Paris> ?p ?o } }");
ServerTest::new().test_status(request, Status::OK) ServerTest::new()?.test_status(request, Status::OK)
} }
#[test] #[test]
fn post_federated_query_dbpedia() { fn post_federated_query_dbpedia() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/query".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/query".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/sparql-query") .with_header(HeaderName::CONTENT_TYPE, "application/sparql-query")
.unwrap().with_body("SELECT * WHERE { SERVICE <https://dbpedia.org/sparql> { <http://dbpedia.org/resource/Paris> ?p ?o } }"); ?.with_body("SELECT * WHERE { SERVICE <https://dbpedia.org/sparql> { <http://dbpedia.org/resource/Paris> ?p ?o } }");
ServerTest::new().test_status(request, Status::OK) ServerTest::new()?.test_status(request, Status::OK)
} }
#[test] #[test]
fn post_update() { fn post_update() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/update".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/update".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/sparql-update") .with_header(HeaderName::CONTENT_TYPE, "application/sparql-update")?
.unwrap()
.with_body( .with_body(
"INSERT DATA { <http://example.com> <http://example.com> <http://example.com> }", "INSERT DATA { <http://example.com> <http://example.com> <http://example.com> }",
); );
ServerTest::new().test_status(request, Status::NO_CONTENT) ServerTest::new()?.test_status(request, Status::NO_CONTENT)
} }
#[test] #[test]
fn post_bad_update() { fn post_bad_update() -> Result<()> {
let request = Request::builder(Method::POST, "http://localhost/update".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/update".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "application/sparql-update") .with_header(HeaderName::CONTENT_TYPE, "application/sparql-update")?
.unwrap()
.with_body("INSERT"); .with_body("INSERT");
ServerTest::new().test_status(request, Status::BAD_REQUEST) ServerTest::new()?.test_status(request, Status::BAD_REQUEST)
} }
#[test] #[test]
fn graph_store_url_normalization() { fn graph_store_url_normalization() -> Result<()> {
let server = ServerTest::new(); let server = ServerTest::new()?;
// PUT // PUT
let request = Request::builder( let request = Request::builder(
Method::PUT, Method::PUT,
"http://localhost/store?graph=http://example.com" "http://localhost/store?graph=http://example.com".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::CONTENT_TYPE, "text/turtle") .with_header(HeaderName::CONTENT_TYPE, "text/turtle")?
.unwrap()
.with_body("<http://example.com> <http://example.com> <http://example.com> ."); .with_body("<http://example.com> <http://example.com> <http://example.com> .");
server.test_status(request, Status::CREATED); server.test_status(request, Status::CREATED)?;
// GET good URI // GET good URI
server.test_status( server.test_status(
Request::builder( Request::builder(
Method::GET, Method::GET,
"http://localhost/store?graph=http://example.com" "http://localhost/store?graph=http://example.com".parse()?,
.parse()
.unwrap(),
) )
.build(), .build(),
Status::OK, Status::OK,
); )?;
// GET bad URI // GET bad URI
server.test_status( server.test_status(
Request::builder( Request::builder(
Method::GET, Method::GET,
"http://localhost/store?graph=http://example.com/" "http://localhost/store?graph=http://example.com/".parse()?,
.parse()
.unwrap(),
) )
.build(), .build(),
Status::NOT_FOUND, Status::NOT_FOUND,
); )
} }
#[test] #[test]
fn graph_store_protocol() { fn graph_store_protocol() -> Result<()> {
// Tests from https://www.w3.org/2009/sparql/docs/tests/data-sparql11/http-rdf-update/ // Tests from https://www.w3.org/2009/sparql/docs/tests/data-sparql11/http-rdf-update/
let server = ServerTest::new(); let server = ServerTest::new()?;
// PUT - Initial state // PUT - Initial state
let request = Request::builder( let request = Request::builder(Method::PUT, "http://localhost/store/person/1.ttl".parse()?)
Method::PUT, .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
"http://localhost/store/person/1.ttl".parse().unwrap(), .with_body(
) "
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")
.unwrap()
.with_body(
"
@prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix v: <http://www.w3.org/2006/vcard/ns#> . @prefix v: <http://www.w3.org/2006/vcard/ns#> .
@ -1473,50 +1432,35 @@ mod tests {
v:fn \"John Doe\" v:fn \"John Doe\"
]. ].
", ",
); );
server.test_status(request, Status::CREATED); server.test_status(request, Status::CREATED)?;
// GET of PUT - Initial state // GET of PUT - Initial state
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/store?graph=/store/person/1.ttl" "http://localhost/store?graph=/store/person/1.ttl".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::ACCEPT, "text/turtle") .with_header(HeaderName::ACCEPT, "text/turtle")?
.unwrap()
.build(); .build();
server.test_status(request, Status::OK); server.test_status(request, Status::OK)?;
// HEAD on an existing graph // HEAD on an existing graph
server.test_status( server.test_status(
Request::builder( Request::builder(Method::HEAD, "http://localhost/store/person/1.ttl".parse()?).build(),
Method::HEAD,
"http://localhost/store/person/1.ttl".parse().unwrap(),
)
.build(),
Status::OK, Status::OK,
); )?;
// HEAD on a non-existing graph // HEAD on a non-existing graph
server.test_status( server.test_status(
Request::builder( Request::builder(Method::HEAD, "http://localhost/store/person/4.ttl".parse()?).build(),
Method::HEAD,
"http://localhost/store/person/4.ttl".parse().unwrap(),
)
.build(),
Status::NOT_FOUND, Status::NOT_FOUND,
); )?;
// PUT - graph already in store // PUT - graph already in store
let request = Request::builder( let request = Request::builder(Method::PUT, "http://localhost/store/person/1.ttl".parse()?)
Method::PUT, .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
"http://localhost/store/person/1.ttl".parse().unwrap(), .with_body(
) "
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")
.unwrap()
.with_body(
"
@prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix v: <http://www.w3.org/2006/vcard/ns#> . @prefix v: <http://www.w3.org/2006/vcard/ns#> .
@ -1526,28 +1470,20 @@ mod tests {
v:fn \"Jane Doe\" v:fn \"Jane Doe\"
]. ].
", ",
); );
server.test_status(request, Status::NO_CONTENT); server.test_status(request, Status::NO_CONTENT)?;
// GET of PUT - graph already in store // GET of PUT - graph already in store
let request = Request::builder( let request = Request::builder(Method::GET, "http://localhost/store/person/1.ttl".parse()?)
Method::GET, .with_header(HeaderName::ACCEPT, "text/turtle")?
"http://localhost/store/person/1.ttl".parse().unwrap(), .build();
) server.test_status(request, Status::OK)?;
.with_header(HeaderName::ACCEPT, "text/turtle")
.unwrap()
.build();
server.test_status(request, Status::OK);
// PUT - default graph // PUT - default graph
let request = Request::builder( let request = Request::builder(Method::PUT, "http://localhost/store?default".parse()?)
Method::PUT, .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
"http://localhost/store?default".parse().unwrap(), .with_body(
) "
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")
.unwrap()
.with_body(
"
@prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix v: <http://www.w3.org/2006/vcard/ns#> . @prefix v: <http://www.w3.org/2006/vcard/ns#> .
@ -1557,58 +1493,38 @@ mod tests {
v:given-name \"Alice\" v:given-name \"Alice\"
] . ] .
", ",
); );
server.test_status(request, Status::NO_CONTENT); // The default graph always exists in Oxigraph server.test_status(request, Status::NO_CONTENT)?; // The default graph always exists in Oxigraph
// GET of PUT - default graph // GET of PUT - default graph
let request = Request::builder( let request = Request::builder(Method::GET, "http://localhost/store?default".parse()?)
Method::GET, .with_header(HeaderName::ACCEPT, "text/turtle")?
"http://localhost/store?default".parse().unwrap(), .build();
) server.test_status(request, Status::OK)?;
.with_header(HeaderName::ACCEPT, "text/turtle")
.unwrap()
.build();
server.test_status(request, Status::OK);
// PUT - mismatched payload // PUT - mismatched payload
let request = Request::builder( let request = Request::builder(Method::PUT, "http://localhost/store/person/1.ttl".parse()?)
Method::PUT, .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
"http://localhost/store/person/1.ttl".parse().unwrap(), .with_body("@prefix fo");
) server.test_status(request, Status::BAD_REQUEST)?;
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")
.unwrap()
.with_body("@prefix fo");
server.test_status(request, Status::BAD_REQUEST);
// PUT - empty graph // PUT - empty graph
let request = Request::builder( let request = Request::builder(Method::PUT, "http://localhost/store/person/2.ttl".parse()?)
Method::PUT, .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
"http://localhost/store/person/2.ttl".parse().unwrap(), .build();
) server.test_status(request, Status::CREATED)?;
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")
.unwrap()
.build();
server.test_status(request, Status::CREATED);
// GET of PUT - empty graph // GET of PUT - empty graph
let request = Request::builder( let request = Request::builder(Method::GET, "http://localhost/store/person/2.ttl".parse()?)
Method::GET, .with_header(HeaderName::ACCEPT, "text/turtle")?
"http://localhost/store/person/2.ttl".parse().unwrap(), .build();
) server.test_status(request, Status::OK)?;
.with_header(HeaderName::ACCEPT, "text/turtle")
.unwrap()
.build();
server.test_status(request, Status::OK);
// PUT - replace empty graph // PUT - replace empty graph
let request = Request::builder( let request = Request::builder(Method::PUT, "http://localhost/store/person/2.ttl".parse()?)
Method::PUT, .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
"http://localhost/store/person/2.ttl".parse().unwrap(), .with_body(
) "
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")
.unwrap()
.with_body(
"
@prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix v: <http://www.w3.org/2006/vcard/ns#> . @prefix v: <http://www.w3.org/2006/vcard/ns#> .
@ -1618,66 +1534,53 @@ mod tests {
v:given-name \"Alice\" v:given-name \"Alice\"
] . ] .
", ",
); );
server.test_status(request, Status::NO_CONTENT); server.test_status(request, Status::NO_CONTENT)?;
// GET of replacement for empty graph // GET of replacement for empty graph
let request = Request::builder( let request = Request::builder(Method::GET, "http://localhost/store/person/2.ttl".parse()?)
Method::GET, .with_header(HeaderName::ACCEPT, "text/turtle")?
"http://localhost/store/person/2.ttl".parse().unwrap(), .build();
) server.test_status(request, Status::OK)?;
.with_header(HeaderName::ACCEPT, "text/turtle")
.unwrap()
.build();
server.test_status(request, Status::OK);
// DELETE - existing graph // DELETE - existing graph
server.test_status( server.test_status(
Request::builder( Request::builder(
Method::DELETE, Method::DELETE,
"http://localhost/store/person/2.ttl".parse().unwrap(), "http://localhost/store/person/2.ttl".parse()?,
) )
.build(), .build(),
Status::NO_CONTENT, Status::NO_CONTENT,
); )?;
// GET of DELETE - existing graph // GET of DELETE - existing graph
server.test_status( server.test_status(
Request::builder( Request::builder(Method::GET, "http://localhost/store/person/2.ttl".parse()?).build(),
Method::GET,
"http://localhost/store/person/2.ttl".parse().unwrap(),
)
.build(),
Status::NOT_FOUND, Status::NOT_FOUND,
); )?;
// DELETE - non-existent graph // DELETE - non-existent graph
server.test_status( server.test_status(
Request::builder( Request::builder(
Method::DELETE, Method::DELETE,
"http://localhost/store/person/2.ttl".parse().unwrap(), "http://localhost/store/person/2.ttl".parse()?,
) )
.build(), .build(),
Status::NOT_FOUND, Status::NOT_FOUND,
); )?;
// POST - existing graph // POST - existing graph
let request = Request::builder( let request = Request::builder(Method::PUT, "http://localhost/store/person/1.ttl".parse()?)
Method::PUT, .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
"http://localhost/store/person/1.ttl".parse().unwrap(), .build();
) server.test_status(request, Status::NO_CONTENT)?;
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")
.unwrap()
.build();
server.test_status(request, Status::NO_CONTENT);
// TODO: POST - multipart/form-data // TODO: POST - multipart/form-data
// TODO: GET of POST - multipart/form-data // TODO: GET of POST - multipart/form-data
// POST - create new graph // POST - create new graph
let request = Request::builder(Method::POST, "http://localhost/store".parse().unwrap()) let request = Request::builder(Method::POST, "http://localhost/store".parse()?)
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8") .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
.unwrap()
.with_body( .with_body(
" "
@prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> .
@ -1692,37 +1595,30 @@ mod tests {
); );
let response = server.exec(request); let response = server.exec(request);
assert_eq!(response.status(), Status::CREATED); assert_eq!(response.status(), Status::CREATED);
let location = response let location = response.header(&HeaderName::LOCATION).unwrap().to_str()?;
.header(&HeaderName::LOCATION)
.unwrap()
.to_str()
.unwrap();
// GET of POST - create new graph // GET of POST - create new graph
let request = Request::builder(Method::GET, location.parse().unwrap()) let request = Request::builder(Method::GET, location.parse()?)
.with_header(HeaderName::ACCEPT, "text/turtle") .with_header(HeaderName::ACCEPT, "text/turtle")?
.unwrap()
.build(); .build();
server.test_status(request, Status::OK); server.test_status(request, Status::OK)?;
// POST - empty graph to existing graph // POST - empty graph to existing graph
let request = Request::builder(Method::PUT, location.parse().unwrap()) let request = Request::builder(Method::PUT, location.parse()?)
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8") .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
.unwrap()
.build(); .build();
server.test_status(request, Status::NO_CONTENT); server.test_status(request, Status::NO_CONTENT)?;
// GET of POST - after noop // GET of POST - after noop
let request = Request::builder(Method::GET, location.parse().unwrap()) let request = Request::builder(Method::GET, location.parse()?)
.with_header(HeaderName::ACCEPT, "text/turtle") .with_header(HeaderName::ACCEPT, "text/turtle")?
.unwrap()
.build(); .build();
server.test_status(request, Status::OK); server.test_status(request, Status::OK)
} }
#[test] #[test]
fn graph_store_lenient_bulk() { fn graph_store_lenient_bulk() -> Result<()> {
let server = ServerTest::new(); let server = ServerTest::new()?;
let invalid_data = " let invalid_data = "
@prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix v: <http://www.w3.org/2006/vcard/ns#> . @prefix v: <http://www.w3.org/2006/vcard/ns#> .
@ -1732,106 +1628,77 @@ mod tests {
// POST // POST
let request = Request::builder( let request = Request::builder(
Method::POST, Method::POST,
"http://localhost/store/person/1.ttl?no_transaction&lenient" "http://localhost/store/person/1.ttl?no_transaction&lenient".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8") .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
.unwrap()
.with_body(invalid_data); .with_body(invalid_data);
server.test_status(request, Status::NO_CONTENT); server.test_status(request, Status::NO_CONTENT)?;
// GET of POST // GET of POST
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/store?graph=/store/person/1.ttl" "http://localhost/store?graph=/store/person/1.ttl".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::ACCEPT, "text/turtle") .with_header(HeaderName::ACCEPT, "text/turtle")?
.unwrap()
.build(); .build();
server.test_status(request, Status::OK); server.test_status(request, Status::OK)?;
// POST dataset // POST dataset
let request = Request::builder( let request = Request::builder(
Method::POST, Method::POST,
"http://localhost/store?lenient&no_transaction" "http://localhost/store?lenient&no_transaction".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::CONTENT_TYPE, "application/trig; charset=utf-8") .with_header(HeaderName::CONTENT_TYPE, "application/trig; charset=utf-8")?
.unwrap()
.with_body(invalid_data); .with_body(invalid_data);
server.test_status(request, Status::NO_CONTENT); server.test_status(request, Status::NO_CONTENT)?;
// GET of POST dataset // GET of POST dataset
let request = Request::builder( let request = Request::builder(Method::GET, "http://localhost/store?default".parse()?)
Method::GET, .with_header(HeaderName::ACCEPT, "text/turtle")?
"http://localhost/store?default".parse().unwrap(), .build();
) server.test_status(request, Status::OK)?;
.with_header(HeaderName::ACCEPT, "text/turtle")
.unwrap()
.build();
server.test_status(request, Status::OK);
// PUT // PUT
let request = Request::builder( let request = Request::builder(
Method::PUT, Method::PUT,
"http://localhost/store/person/1.ttl?lenient&no_transaction" "http://localhost/store/person/1.ttl?lenient&no_transaction".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8") .with_header(HeaderName::CONTENT_TYPE, "text/turtle; charset=utf-8")?
.unwrap()
.with_body(invalid_data); .with_body(invalid_data);
server.test_status(request, Status::NO_CONTENT); server.test_status(request, Status::NO_CONTENT)?;
// GET of PUT - Initial state // GET of PUT - Initial state
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/store?graph=/store/person/1.ttl" "http://localhost/store?graph=/store/person/1.ttl".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::ACCEPT, "text/turtle") .with_header(HeaderName::ACCEPT, "text/turtle")?
.unwrap()
.build(); .build();
server.test_status(request, Status::OK); server.test_status(request, Status::OK)?;
// PUT dataset // PUT dataset
let request = Request::builder( let request = Request::builder(
Method::PUT, Method::PUT,
"http://localhost/store?lenient&no_transaction" "http://localhost/store?lenient&no_transaction".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::CONTENT_TYPE, "application/trig; charset=utf-8") .with_header(HeaderName::CONTENT_TYPE, "application/trig; charset=utf-8")?
.unwrap()
.with_body(invalid_data); .with_body(invalid_data);
server.test_status(request, Status::NO_CONTENT); server.test_status(request, Status::NO_CONTENT)?;
// GET of PUT dataset // GET of PUT dataset
let request = Request::builder( let request = Request::builder(Method::GET, "http://localhost/store?default".parse()?)
Method::GET, .with_header(HeaderName::ACCEPT, "text/turtle")?
"http://localhost/store?default".parse().unwrap(), .build();
) server.test_status(request, Status::OK)?;
.with_header(HeaderName::ACCEPT, "text/turtle")
.unwrap()
.build();
server.test_status(request, Status::OK);
// GET of PUT dataset - replacement // GET of PUT dataset - replacement
let request = Request::builder( let request = Request::builder(
Method::GET, Method::GET,
"http://localhost/store?graph=/store/person/1.ttl" "http://localhost/store?graph=/store/person/1.ttl".parse()?,
.parse()
.unwrap(),
) )
.with_header(HeaderName::ACCEPT, "text/turtle") .with_header(HeaderName::ACCEPT, "text/turtle")?
.unwrap()
.build(); .build();
server.test_status(request, Status::NOT_FOUND); server.test_status(request, Status::NOT_FOUND)
} }
struct ServerTest { struct ServerTest {
@ -1839,10 +1706,10 @@ mod tests {
} }
impl ServerTest { impl ServerTest {
fn new() -> Self { fn new() -> Result<Self> {
Self { Ok(Self {
store: Store::new().unwrap(), store: Store::new()?,
} })
} }
fn exec(&self, mut request: Request) -> Response { fn exec(&self, mut request: Request) -> Response {
@ -1850,19 +1717,21 @@ mod tests {
.unwrap_or_else(|(status, message)| error(status, message)) .unwrap_or_else(|(status, message)| error(status, message))
} }
fn test_status(&self, request: Request, expected_status: Status) { fn test_status(&self, request: Request, expected_status: Status) -> Result<()> {
let mut response = self.exec(request); let mut response = self.exec(request);
let mut buf = String::new(); let mut buf = String::new();
response.body_mut().read_to_string(&mut buf).unwrap(); response.body_mut().read_to_string(&mut buf)?;
assert_eq!(response.status(), expected_status, "Error message: {}", buf); assert_eq!(response.status(), expected_status, "Error message: {}", buf);
Ok(())
} }
fn test_body(&self, request: Request, expected_body: &str) { fn test_body(&self, request: Request, expected_body: &str) -> Result<()> {
let mut response = self.exec(request); let mut response = self.exec(request);
let mut buf = String::new(); let mut buf = String::new();
response.body_mut().read_to_string(&mut buf).unwrap(); response.body_mut().read_to_string(&mut buf)?;
assert_eq!(response.status(), Status::OK, "Error message: {}", buf); assert_eq!(response.status(), Status::OK, "Error message: {}", buf);
assert_eq!(&buf, expected_body); assert_eq!(&buf, expected_body);
Ok(())
} }
} }

Loading…
Cancel
Save