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 hasFormMultipartContentType for checking Content-Type: multipart/form-data #2256

Merged
merged 1 commit into from
Jun 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ trait HeaderChecks[+A] { self: HeaderOps[A] with A =>
final def hasFormUrlencodedContentType: Boolean =
hasContentType(MediaType.application.`x-www-form-urlencoded`.fullType)

final def hasFormMultipartContentType: Boolean =
hasContentType(MediaType.multipart.`form-data`.fullType)

final def hasHeader(name: CharSequence): Boolean =
rawHeader(name).nonEmpty

Expand Down
15 changes: 15 additions & 0 deletions zio-http/src/test/scala/zio/http/HeaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ object HeaderSpec extends ZIOSpecDefault {
assert(actual)(isFalse)
},
),
suite("isFormMultipartContentType")(
test("should return true if content-type is multipart/form-data") {
val actual = contentTypeFormMultipart.hasFormMultipartContentType
assert(actual)(isTrue)
},
test("should return false if content-type is not multipart/form-data") {
val actual = contentTypeTextPlain.hasFormMultipartContentType
assert(actual)(isFalse)
},
test("should return false if content-type doesn't exist") {
val actual = acceptJson.hasFormMultipartContentType
assert(actual)(isFalse)
},
),
)

private val acceptJson = Headers(Header.Accept(MediaType.application.json))
Expand All @@ -174,6 +188,7 @@ object HeaderSpec extends ZIOSpecDefault {
private val contentTypeXml = Headers(Header.ContentType(MediaType.application.xml))
private val contentTypeJson = Headers(Header.ContentType(MediaType.application.json))
private val contentTypeFormUrlEncoded = Headers(Header.ContentType(MediaType.application.`x-www-form-urlencoded`))
private val contentTypeFormMultipart = Headers(Header.ContentType(MediaType.multipart.`form-data`))
private def customAcceptJsonHeader = Header.Accept(MediaType.application.json)
private def customContentJsonHeader = Header.ContentType(MediaType.application.json)
private def customHeaders: Headers = Headers(customContentJsonHeader, customAcceptJsonHeader)
Expand Down