diff --git a/docs/website/docs/v1.x/dsl/headers.md b/docs/website/docs/v1.x/dsl/headers.md index f74b1fbab5..2cd797830d 100644 --- a/docs/website/docs/v1.x/dsl/headers.md +++ b/docs/website/docs/v1.x/dsl/headers.md @@ -155,7 +155,7 @@ val responseHeaders: Task[Headers] = Client.request(url).map(_.headers) _ <- Console.printLine { data } } yield () - override def run(args: List[String]): URIO[Any, ExitCode] = program.exitCode.provideLayer(env) + override def run(args: List[String]): UIO[ExitCode] = program.exitCode.provideLayer(env) } ``` diff --git a/docs/website/docs/v1.x/examples/advanced-examples/middleware_basic_auth.md b/docs/website/docs/v1.x/examples/advanced-examples/middleware_basic_auth.md index df18efb16e..c3b6a9b787 100644 --- a/docs/website/docs/v1.x/examples/advanced-examples/middleware_basic_auth.md +++ b/docs/website/docs/v1.x/examples/advanced-examples/middleware_basic_auth.md @@ -8,7 +8,7 @@ import zhttp.http._ import zhttp.service.Server import zio.{App, ExitCode, URIO} -object BasicAuth extends App { +object BasicAuth extends ZIOAppDefault { // Http app that requires a JWT claim val user: UHttpApp = Http.collect[Request] { case Method.GET -> !! / "user" / name / "greet" => @@ -19,8 +19,8 @@ object BasicAuth extends App { val app: UHttpApp = user @@ basicAuth("admin", "admin") // Run it like any simple app - override def run(args: List[String]): UIO[ExitCode] = - Server.start(8090, app).exitCode + override val run = + Server.start(8090, app) } ``` \ No newline at end of file diff --git a/docs/website/docs/v1.x/examples/advanced-examples/middleware_cors.md b/docs/website/docs/v1.x/examples/advanced-examples/middleware_cors.md index 5b6cce6fa8..5ad5057f91 100644 --- a/docs/website/docs/v1.x/examples/advanced-examples/middleware_cors.md +++ b/docs/website/docs/v1.x/examples/advanced-examples/middleware_cors.md @@ -5,7 +5,7 @@ import zhttp.http._ import zhttp.service.Server import zio._ -object HelloWorldWithCORS extends App { +object HelloWorldWithCORS extends ZIOAppDefault { // Create CORS configuration val config: CORSConfig = CORSConfig(allowedOrigins = _ == "dev", allowedMethods = Some(Set(Method.PUT, Method.DELETE))) @@ -18,7 +18,7 @@ object HelloWorldWithCORS extends App { } @@ cors(config) // Run it like any simple app - override def run(args: List[String]): UIO[ExitCode] = - Server.start(8090, app.silent).exitCode + override val run = + Server.start(8090, app.silent) } ``` \ No newline at end of file diff --git a/docs/website/docs/v1.x/examples/advanced-examples/middleware_csrf.md b/docs/website/docs/v1.x/examples/advanced-examples/middleware_csrf.md index d9bc79ead9..35244c4579 100644 --- a/docs/website/docs/v1.x/examples/advanced-examples/middleware_csrf.md +++ b/docs/website/docs/v1.x/examples/advanced-examples/middleware_csrf.md @@ -18,8 +18,8 @@ object CSRF extends App { } @@ csrfGenerate() // set x-csrf token cookie val app = publicApp ++ privateApp - override def run(args: List[String]): UIO[ExitCode] = - Server.start(8090, app).exitCode + override val run = + Server.start(8090, app) } ``` \ No newline at end of file diff --git a/docs/website/docs/v1.x/examples/advanced-examples/stream-file.md b/docs/website/docs/v1.x/examples/advanced-examples/stream-file.md index e96a387637..03cefe5039 100644 --- a/docs/website/docs/v1.x/examples/advanced-examples/stream-file.md +++ b/docs/website/docs/v1.x/examples/advanced-examples/stream-file.md @@ -8,7 +8,7 @@ import zio._ import java.io.File import java.nio.file.Paths -object FileStreaming extends App { +object FileStreaming extends ZIOAppDefault { // Create HTTP route val app = Http.collectHttp[Request] { @@ -26,8 +26,8 @@ object FileStreaming extends App { } // Run it like any simple app - override def run(args: List[String]): UIO[ExitCode] = - Server.start(8090, app.silent).exitCode + override val run = + Server.start(8090, app.silent) } ``` \ No newline at end of file diff --git a/docs/website/docs/v1.x/examples/advanced-examples/stream-response.md b/docs/website/docs/v1.x/examples/advanced-examples/stream-response.md index 5ef4516d83..2e45142173 100644 --- a/docs/website/docs/v1.x/examples/advanced-examples/stream-response.md +++ b/docs/website/docs/v1.x/examples/advanced-examples/stream-response.md @@ -9,12 +9,10 @@ import zio._ /** * Example to encode content using a ZStream */ -object StreamingResponse extends App { - override def run(args: List[String]): UIO[ExitCode] = { - +object StreamingResponse extends ZIOAppDefault { + override val run = // Starting the server (for more advanced startup configuration checkout `HelloWorldAdvanced`) - Server.start(8090, app.silent).exitCode - } + Server.start(8090, app.silent) // Create a message as a Chunk[Byte] val message = Chunk.fromArray("Hello world !\r\n".getBytes(HTTP_CHARSET)) diff --git a/docs/website/docs/v1.x/getting-started.md b/docs/website/docs/v1.x/getting-started.md index 061011de99..3188787f6d 100644 --- a/docs/website/docs/v1.x/getting-started.md +++ b/docs/website/docs/v1.x/getting-started.md @@ -25,7 +25,7 @@ An app can be made using any of the available constructors on `zhttp.Http`. ### Routing - For handling routes, Http Domain has a `collect` method that, accepts different requests and produces responses. Pattern matching on the route is supported by the framework + For handling routes, Http Domain has a `collect` method that, accepts different requests and produces responses. Pattern matching on the route is supported by the framework. The example below shows how to create routes: ```scala @@ -108,7 +108,7 @@ import zhttp.http._ object Spec extends ZIOSpecDefault { def spec = suite("http")( - testM("should be ok") { + test("should be ok") { val app = Http.ok val req = Request() assertM(app(req))(equalTo(Response.ok)) @@ -156,11 +156,11 @@ import zhttp.http._ import zhttp.service.Server import zio._ -object HelloWorld extends App { +object HelloWorld extends ZIOAppDefault { val app = Http.ok - override def run(args: List[String]): URIO[Any, ExitCode] = - Server.start(8090, app).exitCode + override val run = + Server.start(8090, app) } ``` diff --git a/docs/website/yarn.lock b/docs/website/yarn.lock index 493e7e51b6..b190a4a8ba 100644 --- a/docs/website/yarn.lock +++ b/docs/website/yarn.lock @@ -5731,9 +5731,9 @@ minimatch@3.0.4, minimatch@^3.0.4: brace-expansion "^1.1.7" minimist@^1.2.0, minimist@^1.2.5: - version "1.2.5" - resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602" - integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw== + version "1.2.6" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44" + integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q== mixin-deep@^1.2.0: version "1.3.2" diff --git a/example/src/main/scala/example/AuthenticationClient.scala b/example/src/main/scala/example/AuthenticationClient.scala index 42e01bd733..113aa5ed40 100644 --- a/example/src/main/scala/example/AuthenticationClient.scala +++ b/example/src/main/scala/example/AuthenticationClient.scala @@ -2,7 +2,7 @@ package example import zhttp.http.Headers import zhttp.service.{ChannelFactory, Client, EventLoopGroup} -import zio.{ExitCode, UIO, ZIOAppDefault} +import zio._ object AuthenticationClient extends ZIOAppDefault { @@ -20,9 +20,9 @@ object AuthenticationClient extends ZIOAppDefault { // Once the jwt token is procured, adding it as a Barer token in Authorization header while accessing a protected route. response <- Client.request(s"${url}/user/userName/greet", headers = Headers.bearerAuthorizationHeader(token)) body <- response.bodyAsString - _ <- zio.Console.printLine(body) + _ <- Console.printLine(body) } yield () - val run: UIO[ExitCode] = program.exitCode.provideLayer(env) + override val run = program.provideLayer(env) } diff --git a/example/src/main/scala/example/AuthenticationServer.scala b/example/src/main/scala/example/AuthenticationServer.scala index e70ea343e3..28f42ee631 100644 --- a/example/src/main/scala/example/AuthenticationServer.scala +++ b/example/src/main/scala/example/AuthenticationServer.scala @@ -4,7 +4,7 @@ import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim} import zhttp.http.Middleware.bearerAuth import zhttp.http._ import zhttp.service.Server -import zio.{ExitCode, UIO, ZIOAppDefault} +import zio.{Clock => _, _} import java.time.Clock @@ -52,6 +52,6 @@ object AuthenticationServer extends ZIOAppDefault { val app: UHttpApp = login ++ user // Run it like any simple app - val run: UIO[ExitCode] = - Server.start(8090, app).exitCode + override val run = + Server.start(8090, app) } diff --git a/example/src/main/scala/example/StaticServer.scala b/example/src/main/scala/example/StaticServer.scala index 8d37c08b75..e901f1a105 100644 --- a/example/src/main/scala/example/StaticServer.scala +++ b/example/src/main/scala/example/StaticServer.scala @@ -10,7 +10,7 @@ object StaticServer extends ZIOAppDefault { // A simple app to serve static resource files from a local directory. val app = Http.collectHttp[Request] { case Method.GET -> "static" /: path => for { - file <- Http.getResourceAsFile(path.encode) + file <- Http.getResourceAsFile(path.encode.tail) http <- // Rendering a custom UI to list all the files in the directory if (file.isDirectory) { diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 63c999da96..17472e2939 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -1,7 +1,7 @@ import sbt._ object Dependencies { - val JwtCoreVersion = "9.0.4" + val JwtCoreVersion = "9.0.5" val NettyVersion = "4.1.75.Final" val NettyIncubatorVersion = "0.0.13.Final" val ScalaCompactCollectionVersion = "2.7.0" diff --git a/zio-http/src/main/scala/zhttp/http/HttpData.scala b/zio-http/src/main/scala/zhttp/http/HttpData.scala index ba945b18a3..66e3c3255b 100644 --- a/zio-http/src/main/scala/zhttp/http/HttpData.scala +++ b/zio-http/src/main/scala/zhttp/http/HttpData.scala @@ -200,7 +200,7 @@ object HttpData { private[zhttp] final case class BinaryChunk(data: Chunk[Byte]) extends Complete { - private def encode = Unpooled.wrappedBuffer(data.toArray) + private def encode: ByteBuf = Unpooled.wrappedBuffer(data.toArray) /** * Encodes the HttpData into a ByteBuf. diff --git a/zio-http/src/main/scala/zhttp/http/middleware/Auth.scala b/zio-http/src/main/scala/zhttp/http/middleware/Auth.scala index ff3d889da6..f9237b4728 100644 --- a/zio-http/src/main/scala/zhttp/http/middleware/Auth.scala +++ b/zio-http/src/main/scala/zhttp/http/middleware/Auth.scala @@ -4,7 +4,7 @@ import io.netty.handler.codec.http.HttpHeaderNames import zhttp.http.Headers.{BasicSchemeName, BearerSchemeName} import zhttp.http._ import zhttp.http.middleware.Auth.Credentials -import zio.ZIO +import zio._ private[zhttp] trait Auth { diff --git a/zio-http/src/main/scala/zhttp/socket/Socket.scala b/zio-http/src/main/scala/zhttp/socket/Socket.scala index 8c7396a9cb..f442b6d68a 100644 --- a/zio-http/src/main/scala/zhttp/socket/Socket.scala +++ b/zio-http/src/main/scala/zhttp/socket/Socket.scala @@ -85,7 +85,7 @@ object Socket { */ def empty: Socket[Any, Nothing, Any, Nothing] = Socket.Empty - def end: ZStream[Any, Nothing, Nothing] = ZStream.failCause(Cause.empty) + def end: Socket[Any, Nothing, Any, Nothing] = Socket.End def fromFunction[A]: PartialFromFunction[A] = new PartialFromFunction[A](()) diff --git a/zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala b/zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala index e4c83f127f..8a4bdbc3d6 100644 --- a/zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/ContentTypeSpec.scala @@ -3,10 +3,10 @@ package zhttp.http import zhttp.internal.{DynamicServer, HttpRunnableSpec} import zhttp.service.server.ServerChannelFactory import zhttp.service.{ChannelFactory, EventLoopGroup} +import zio._ import zio.test.Assertion.{equalTo, isNone, isSome} import zio.test.TestAspect.timeout import zio.test.assertM -import zio.{Scope, durationInt} object ContentTypeSpec extends HttpRunnableSpec { diff --git a/zio-http/src/test/scala/zhttp/service/ServerSpec.scala b/zio-http/src/test/scala/zhttp/service/ServerSpec.scala index 20e05b1ba6..2d9d41599f 100644 --- a/zio-http/src/test/scala/zhttp/service/ServerSpec.scala +++ b/zio-http/src/test/scala/zhttp/service/ServerSpec.scala @@ -83,6 +83,21 @@ object ServerSpec extends HttpRunnableSpec { assertM(res)(isSome(anything)) } } + + suite("die") { + val app = Http.die(new Error("SERVER_ERROR")) + test("status is 500") { + val res = app.deploy.status.run() + assertM(res)(equalTo(Status.InternalServerError)) + } + + test("content is set") { + val res = app.deploy.bodyAsString.run() + assertM(res)(containsString("SERVER_ERROR")) + } + + test("header is set") { + val res = app.deploy.headerValue(HeaderNames.contentLength).run() + assertM(res)(isSome(anything)) + } + } + suite("echo content") { val app = Http.collectZIO[Request] { case req => req.bodyAsString.map(text => Response.text(text))