Skip to content

Commit

Permalink
Merge branch 'main' into deprecate-3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Dec 14, 2024
2 parents 3775973 + e82ca17 commit 2482a0a
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ jobs:
cache: sbt

- uses: coursier/setup-action@v1
with:
apps: sbt

- name: Check formatting
if: matrix.scala == '2.13.14'
Expand All @@ -90,6 +92,8 @@ jobs:
run: sbt '++ ${{ matrix.scala }}' test

- uses: coursier/setup-action@v1
with:
apps: sbt

- name: Check doc generation
if: ${{ github.event_name == 'pull_request' }}
Expand Down Expand Up @@ -238,6 +242,8 @@ jobs:
fetch-depth: 0

- uses: coursier/setup-action@v1
with:
apps: sbt

- name: Setup Java (temurin@21)
if: matrix.java == 'temurin@21'
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
with:
fetch-depth: '0'
- uses: coursier/setup-action@v1
with:
apps: 'sbt'
- name: Setup Scala
uses: actions/[email protected]
with:
Expand All @@ -50,6 +52,8 @@ jobs:
java-version: 17
check-latest: true
- uses: coursier/setup-action@v1
with:
apps: 'sbt'
- name: Setup NodeJs
uses: actions/setup-node@v3
with:
Expand All @@ -70,6 +74,8 @@ jobs:
with:
fetch-depth: '0'
- uses: coursier/setup-action@v1
with:
apps: 'sbt'
- name: Install libuv
run: sudo apt-get update && sudo apt-get install -y libuv1-dev
- name: Setup Scala
Expand Down
15 changes: 11 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ ThisBuild / githubWorkflowPREventTypes := Seq(
PREventType.Edited,
PREventType.Labeled,
)
ThisBuild / githubWorkflowAddedJobs :=

val coursierSetup =
WorkflowStep.Use(
UseRef.Public("coursier", "setup-action", "v1"),
params = Map("apps" -> "sbt"),
)

ThisBuild / githubWorkflowAddedJobs :=
Seq(
WorkflowJob(
id = "update_release_draft",
Expand All @@ -40,7 +47,7 @@ ThisBuild / githubWorkflowAddedJobs :=
name = "Mima Check",
steps = List(
WorkflowStep.Use(UseRef.Public("actions", "checkout", "v4"), Map("fetch-depth" -> "0")),
WorkflowStep.Use(UseRef.Public("coursier", "setup-action", "v1")),
coursierSetup,
) ++ WorkflowStep.SetupJava(List(JavaSpec.temurin("21"))) :+ WorkflowStep.Sbt(List("mimaChecks")),
cond = Option("${{ github.event_name == 'pull_request' }}"),
javas = List(JavaSpec.temurin("21")),
Expand Down Expand Up @@ -78,7 +85,7 @@ ThisBuild / githubWorkflowPublish :=
//scala fix isn't available for scala 3 so ensure we only run the fmt check
//using the latest scala 2.13
ThisBuild / githubWorkflowBuildPreamble := Seq(
WorkflowStep.Use(UseRef.Public("coursier", "setup-action", "v1")),
coursierSetup,
WorkflowStep.Run(
name = Some("Check formatting"),
commands = List(s"sbt ++${Scala213} fmtCheck"),
Expand All @@ -91,7 +98,7 @@ ThisBuild / githubWorkflowBuildPostamble :=
"checkDocGeneration",
"Check doc generation",
List(
WorkflowStep.Use(UseRef.Public("coursier", "setup-action", "v1")),
coursierSetup,
WorkflowStep.Run(
commands = List(s"sbt ++${Scala213} doc"),
name = Some("Check doc generation"),
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Dependencies {
val NettyVersion = "4.1.112.Final"
val NettyIncubatorVersion = "0.0.25.Final"
val ScalaCompactCollectionVersion = "2.12.0"
val ZioVersion = "2.1.9"
val ZioVersion = "2.1.11"
val ZioCliVersion = "0.5.0"
val ZioJsonVersion = "0.7.1"
val ZioParserVersion = "0.1.10"
Expand Down
10 changes: 10 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/ClientSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ object ClientSpec extends RoutesRunnableSpec {
app.deploy(Request(headers = Headers(Header.Authorization.Unparsed("", "my-token")))).flatMap(_.body.asString)
assertZIO(responseContent)(equalTo("my-token"))
} @@ timeout(5.seconds),
test("URL and path manipulation on client level") {
for {
baseURL <- DynamicServer.httpURL
_ <-
Handler.ok.toRoutes.deployAndRequest { c =>
(c.updatePath(_ / "my-service") @@ ZClientAspect.requestLogging()).batched.get("/hello")
}.runZIO(())
loggedUrl <- ZTestLogger.logOutput.map(_.collectFirst { case m => m.annotations("url") }.mkString)
} yield assertTrue(loggedUrl == baseURL + "/my-service/hello")
},
)

override def spec = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ object SizeLimitsSpec extends ZIOHttpSpec {
val DEFAULT_URL_SIZE = 4096
val DEFAULT_HEADER_SIZE = 8192
val DEFAULT_CONTENT_SIZE = 1024 * 100
val ERROR_MARGIN = 3

/*
Checks that for `A` with size until `maxSize`, server responds with `Status.Ok` and `badStatus` after it.
Expand Down Expand Up @@ -63,7 +64,7 @@ object SizeLimitsSpec extends ZIOHttpSpec {
}
info <-
if (expected == status) loop(size + 1, lstTestSize, inc(size)(content), f, expected)
else if (size >= lstTestSize - 2) // adding margin for differences in scala 2 and scala 3
else if (size >= lstTestSize - ERROR_MARGIN) // adding margin for differences in scala 2 and scala 3
ZIO.succeed(((size, expected), Some(content)))
else ZIO.succeed(((size, status), None))
} yield info
Expand All @@ -81,7 +82,7 @@ object SizeLimitsSpec extends ZIOHttpSpec {
(lstWorkingSize1, lstStatus1) = info1
(lstWorkingSize2, lstStatus2) = info2
} yield assertTrue(
maxSize - lstWorkingSize1 <= 2,
maxSize - lstWorkingSize1 <= ERROR_MARGIN,
maxSize - lstWorkingSize1 >= 0,
lstStatus1 == Status.Ok,
lstWorkingSize2 == lstTestSize,
Expand Down
36 changes: 21 additions & 15 deletions zio-http/shared/src/main/scala/zio/http/Body.scala
Original file line number Diff line number Diff line change
Expand Up @@ -567,21 +567,27 @@ object Body {

override def asStream(implicit trace: Trace): ZStream[Any, Throwable, Byte] =
ZStream.unwrap {
for {
file <- ZIO.attempt(file)
fs <- ZIO.attemptBlocking(new FileInputStream(file))
size <- ZIO.attemptBlocking(Math.min(chunkSize.toLong, file.length()).toInt)
} yield ZStream
.repeatZIOOption[Any, Throwable, Chunk[Byte]] {
for {
buffer <- ZIO.succeed(new Array[Byte](size))
len <- ZIO.attemptBlocking(fs.read(buffer)).mapError(Some(_))
bytes <-
if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len)))
else ZIO.fail(None)
} yield bytes
}
.ensuring(ZIO.attemptBlocking(fs.close()).ignoreLogged)
ZIO.blocking {
for {
r <- ZIO.attempt {
val fs = new FileInputStream(file)
val size = Math.min(chunkSize.toLong, file.length()).toInt

(fs, size)
}
(fs, size) = r
} yield ZStream
.repeatZIOOption[Any, Throwable, Chunk[Byte]] {
for {
buffer <- ZIO.succeed(new Array[Byte](size))
len <- ZIO.attempt(fs.read(buffer)).mapError(Some(_))
bytes <-
if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len)))
else ZIO.fail(None)
} yield bytes
}
.ensuring(ZIO.attempt(fs.close()).ignoreLogged)
}
}.flattenChunks

override def contentType(newContentType: Body.ContentType): Body = copy(contentType = Some(newContentType))
Expand Down
10 changes: 7 additions & 3 deletions zio-http/shared/src/main/scala/zio/http/ZClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out](

def path(path: String): ZClient[Env, ReqEnv, In, Err, Out] = self.path(Path(path))

def path(path: Path): ZClient[Env, ReqEnv, In, Err, Out] =
copy(url = url.copy(path = path))
def path(path: Path): ZClient[Env, ReqEnv, In, Err, Out] = updatePath(_ => path)

def updatePath(f: Path => Path): ZClient[Env, ReqEnv, In, Err, Out] =
copy(url = url.copy(path = f(url.path)))

def patch(suffix: String)(implicit ev: Body <:< In, trace: Trace): ZIO[Env & ReqEnv, Err, Out] =
request(Method.PATCH, suffix)(ev(Body.empty))
Expand Down Expand Up @@ -289,6 +291,8 @@ final case class ZClient[-Env, ReqEnv, -In, +Err, +Out](
def uri(uri: URI): ZClient[Env, ReqEnv, In, Err, Out] = url(URL.fromURI(uri).getOrElse(URL.empty))

def url(url: URL): ZClient[Env, ReqEnv, In, Err, Out] = copy(url = url)

def updateURL(f: URL => URL): ZClient[Env, ReqEnv, In, Err, Out] = copy(url = f(url))
}

object ZClient extends ZClientPlatformSpecific {
Expand Down Expand Up @@ -696,7 +700,7 @@ object ZClient extends ZClientPlatformSpecific {
webSocketUrl <- url.scheme match {
case Some(Scheme.HTTP) | Some(Scheme.WS) | None => ZIO.succeed(url.scheme(Scheme.WS))
case Some(Scheme.WSS) | Some(Scheme.HTTPS) => ZIO.succeed(url.scheme(Scheme.WSS))
case _ => ZIO.fail(throw new IllegalArgumentException("URL's scheme MUST be WS(S) or HTTP(S)"))
case _ => ZIO.fail(new IllegalArgumentException("URL's scheme MUST be WS(S) or HTTP(S)"))
}
scope <- ZIO.scope
res <- requestAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sealed trait HttpContentCodec[A] { self =>
ZIO.fromEither(codec.codec(config).decode(bytes))
}
case None =>
ZIO.fail(throw new IllegalArgumentException(s"No codec found for content type $contentType"))
ZIO.fail(new IllegalArgumentException(s"No codec found for content type $contentType"))
}
}

Expand All @@ -50,7 +50,7 @@ sealed trait HttpContentCodec[A] { self =>
ZIO.fromEither(codec.codec(config).decode(bytes))
}
case None =>
ZIO.fail(throw new IllegalArgumentException(s"No codec found for content type $contentType"))
ZIO.fail(new IllegalArgumentException(s"No codec found for content type $contentType"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,21 @@ private[codec] object EncoderDecoder {
private def decodeBody(config: CodecConfig, body: Body, inputs: Array[Any])(implicit
trace: Trace,
): Task[Unit] = {
val codecs = flattened.content
val isNonMultiPart = inputs.length < 2
if (isNonMultiPart) {
val codecs = flattened.content

if (inputs.length < 2) {
// non multi-part
codecs.headOption.map { codec =>
// noinspection SimplifyUnlessInspection
if (codecs.isEmpty) ZIO.unit
else {
val codec = codecs.head
codec
.decodeFromBody(body, config)
.mapBoth(
{ err => HttpCodecError.MalformedBody(err.getMessage(), Some(err)) },
{ err => HttpCodecError.MalformedBody(err.getMessage, Some(err)) },
result => inputs(0) = result,
)
}.getOrElse(ZIO.unit)
}
} else {
// multi-part
decodeForm(body.asMultipartFormStream, inputs, config) *> check(inputs)
Expand Down

0 comments on commit 2482a0a

Please sign in to comment.