Skip to content

Instantly share code, notes, and snippets.

@tpluscode
Last active January 21, 2025 08:00
Show Gist options
  • Save tpluscode/6ee5016834a4012813ad843a2f509cf6 to your computer and use it in GitHub Desktop.
Save tpluscode/6ee5016834a4012813ad843a2f509cf6 to your computer and use it in GitHub Desktop.
API Tun3r

API Tun3r

API Tests Using n3 Rules

# Get response from a GET request
[
http:absolutePath "/things" ;
http:method http-methods:GET ;
http:header ( "accept" "text/turtle" ) ;
] http:resp ?res .
# Check OK response and
{
?res http:status http:OK ;
?res http:body ?collection ;
?collection hydra:member ?member . # how to count there are 10 members?
?collection hydra:view [
a hydra:PagedCollectionView ;
hydra:first <http://localhost:1429/things?page=1> ;
hydra:next <http://localhost:1429/things?page=2> ;
] ;
} =>
{
earl:passed true
}
# Send a new resource to collection
[
http:url "http://localhost:1429/things" ;
http:method http-methods:POST ;
http:header ( "accept" "text/turtle" ) ;
http:header ( "Content-Type" "application/n-triples" );
http:body {
[] a ex:NewResource ;
# schema:name "foo bar" ; # missing name
}
] http:response ?res .
# Expect for SHACL violation
{
?res http:status http-status:BadRequest .
?res http:body {
[
a sh:ValidationResult ;
sh:path schema:name ;
sh:sourceConstraintComponent sh:MinCountConstraintComponent ;
sh:resultSeverity sh:Violation ;
] .
} .
} => {
earl:passed true .
}
# Send a new resource to collection
[
http:url "http://localhost:1429/things" ;
http:method http-methods:POST ;
http:header ( "accept" "text/turtle" ) ;
http:header ( "Content-Type" "application/n-triples" );
http:body {
a ex:NewResource ;
schema:name "foo bar" ;
}
] http:response ?res .
{
?res http:status http-status:Created ;
?res http:header ( "Location" ?newResourceUrl ) ;
?newResourceUrl log:equalTo <http://localhost:1429/thing/foo-bar> .
} => {
[
http:requestUrl ?newResourceUrl ;
http:method http-methods:GET ;
] http:resp ?newResourceRes ;
}
{
?newResourceRes http:body {
?newResourceUrl
a owl:Thing, ex:NewResource ;
schema:name "foo bar" ;
schema:identifier "foo-bar" ;
.
} .
} => {
earl:passed true ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment