Skip to content

Commit

Permalink
feat: setting media type of file uploaded in multipart request
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Feb 6, 2025
1 parent 38b7bd1 commit c907949
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-points-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api-tuner": patch
---

When uploading files in a multipart request, their media type can be defined using a third argument like `tuner:form ( "name" <file:path> "metia/type" )`
44 changes: 33 additions & 11 deletions rules/curl-body.n3
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,8 @@ prefix math: <http://www.w3.org/2000/10/swap/math#>
(
?formField
{
?multipartBody tuner:form ( ?name ?value ) .

(
{ ?value log:rawType log:Literal }
{
( " -F " ?name "=" ?value ) string:concatenation ?formField .
}
{
( " -F " ?name "=" ?value!file:curlFileReference ) string:concatenation ?formField .
}
) log:ifThenElseIn [] .
?multipartBody tuner:form ?list .
?list </#field> ?formField .
}
?formFields
) log:collectAllIn [] .
Expand All @@ -57,3 +48,34 @@ prefix math: <http://www.w3.org/2000/10/swap/math#>
?formFields!string:concatenation
) string:concatenation ?curlArgs .
} .

{
( ?name ?value ) </#field> ?formField .
} <= {
{
?value log:rawType log:Literal .
( " -F " ?name "=" ?value ) string:concatenation ?formField .
} log:callWithCut true .
} .

{
( ?name ?value ) </#field> ?formField .
} <= {
( ?name ?value "" ) </#field> ?formField .
} .

{
( ?name ?value ?type ) </#field> ?formField .
} <= {
(
{ ?type!string:length math:greaterThan 0 }
{
( "';type=" ?type "'" ) string:concatenation ?typeParam
}
{
?typeParam log:equalTo "" .
}
) log:ifThenElseIn [] .

( " -F " ?name "=" ?value!file:curlFileReference ?typeParam ) string:concatenation ?formField .
} .
32 changes: 32 additions & 0 deletions tests/bodies/multipart-media-type.n3
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
PREFIX ex: <http://example.org/>
prefix : <https://api-tuner.described.at/test#bodies/>
PREFIX earl: <http://www.w3.org/ns/earl#>
PREFIX tuner: <https://api-tuner.described.at/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX log: <http://www.w3.org/2000/10/swap/log#>
PREFIX string: <http://www.w3.org/2000/10/swap/string#>

:multiPartBodyTestCase
a earl:TestCase ;
rdfs:label "Multipart form upload" ;
tuner:request [
a tuner:Request ;
tuner:url <http://localhost:1080/body-multipart> ;
tuner:method "POST" ;
tuner:body [
tuner:form ( "upload" <file:tests/bodies/foobar.ttl> "text/turtle" ) ;
] ;
]
.

{
:multiPartBodyTestCase!tuner:request tuner:response ?res .

?res tuner:http_code 200 .

?res!tuner:body
string:matches 'Content-Disposition: form-data; name=.upload.; filename=.foobar.ttl.\r\nContent-Type: text/turtle' ;
.
} => {
:multiPartBodyTestCase earl:outcome earl:passed .
} .

0 comments on commit c907949

Please sign in to comment.