Skip to content

Commit

Permalink
Merge branch 'main' into implement-ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegoes authored Jun 8, 2024
2 parents a490218 + da5bb31 commit d35d4c1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Some of the key features of ZIO HTTP are:
Setup via `build.sbt`:

```scala
libraryDependencies += "dev.zio" %% "zio-http" % "3.0.0-RC8"
libraryDependencies += "dev.zio" %% "zio-http" % "3.0.0-RC7"
```

**NOTES ON VERSIONING:**
Expand Down Expand Up @@ -103,7 +103,7 @@ object GreetingClient extends ZIOAppDefault {

## Documentation

Learn more on the [ZIO Http Docs](https://zio.dev/zio-http/)!
Learn more on the [ZIO Http homepage](https://github.com/zio/zio-http)!

## Contributing

Expand Down
33 changes: 33 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/endpoint/MultipartSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import zio.test._

import zio.stream.ZStream

import zio.schema.{DeriveSchema, Schema}

import zio.http.Method._
import zio.http._
import zio.http.codec._
Expand Down Expand Up @@ -275,6 +277,37 @@ object MultipartSpec extends ZIOHttpSpec {
)
}
},
test("override default content type if set explicitly") {
import zio._
import zio.http._
import zio.http.codec._
import zio.http.endpoint.EndpointMiddleware.None
import zio.schema.DeriveSchema.gen
import zio.stream.ZStream

val endpoint: Endpoint[Int, Int, ZNothing, (Book, ZStream[Any, Nothing, Byte]), None] =
Endpoint(RoutePattern.GET / "books" / PathCodec.int("id"))
.outCodec(
HttpCodec.content[Book]("book", MediaType.application.`json`) ++
HttpCodec.binaryStream("file", MediaType.application.`octet-stream`) ++
HeaderCodec.contentType.expect(Header.ContentType(MediaType.multipart.`mixed`)),
)
for {
result <- (endpoint
.implement(handler { (id: Int) =>
(Book("John's Book", List("John Doe")), ZStream.from(Chunk.fromArray("the book file".getBytes)))
})
.toRoutes @@ Middleware.debug).run(path = Path.root / "books" / "123")
} yield assertTrue(
result.status == Status.Ok,
result.headers.getAll(Header.ContentType).map(_.mediaType) == Chunk(MediaType.multipart.`mixed`),
)
},
),
)

case class Book(title: String, authors: List[String])
object Book {
implicit val schema: Schema[Book] = DeriveSchema.gen[Book]
}
}
7 changes: 7 additions & 0 deletions zio-http/shared/src/main/scala/zio/http/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ object Server extends ServerPlatformSpecific {
ZIO.never
}

def serve[R](
route: Route[R, Response],
routes: Route[R, Response]*,
)(implicit trace: Trace, tag: EnvironmentTag[R]): URIO[R with Server, Nothing] = {
serve(Routes(route, routes: _*))
}

def install[R](
httpApp: Routes[R, Response],
)(implicit trace: Trace, tag: EnvironmentTag[R]): URIO[R with Server, Int] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,11 @@ private[codec] object EncoderDecoder {
val status = encodeStatus(inputs.status)
val method = encodeMethod(inputs.method)
val headers = encodeHeaders(inputs.header)
val contentTypeHeaders = encodeContentType(inputs.content, outputTypes)
def contentTypeHeaders = encodeContentType(inputs.content, outputTypes)
val body = encodeBody(inputs.content, outputTypes)

f(URL(path, queryParams = query), status, method, headers ++ contentTypeHeaders, body)
val headers0 = if (headers.contains("content-type")) headers else headers ++ contentTypeHeaders
f(URL(path, queryParams = query), status, method, headers0, body)
}

private def decodePaths(path: Path, inputs: Array[Any]): Unit = {
Expand Down

0 comments on commit d35d4c1

Please sign in to comment.