API Tests Using n3 Rules
Last active
January 21, 2025 08:00
-
-
Save tpluscode/6ee5016834a4012813ad843a2f509cf6 to your computer and use it in GitHub Desktop.
API Tun3r
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 . | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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