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

Backmerge 31 03 2022 #1171

Merged
merged 175 commits into from
Apr 1, 2022
Merged
Changes from 1 commit
Commits
Show all changes
175 commits
Select commit Hold shift + click to select a range
0ffb4d6
Doc: Add outline (#815)
girdharshubham Jan 12, 2022
6410f7e
Update netty-all to 4.1.73.Final (#811)
d11-amitsingh Jan 12, 2022
3a34823
Disable benchmarks comment on fork pull request (#820)
d11-amitsingh Jan 13, 2022
66a1dea
Feature: API to modify headers (#824)
ShrutiVerma97 Jan 13, 2022
143ad28
Feature: Signed Cookie (#751)
ShrutiVerma97 Jan 13, 2022
67650c2
Update sbt-scalafix to 0.9.34 (#805)
d11-amitsingh Jan 13, 2022
64a8f4a
Fix: Echo streaming (#828)
d11-amitsingh Jan 14, 2022
72d71d3
Docs: Update Basic Examples (#814)
ShrutiVerma97 Jan 14, 2022
569bd1d
docs: update advanced examples (#816)
ShrutiVerma97 Jan 14, 2022
fa47791
maintenance: semanticdb revision usage (#832)
girdharshubham Jan 14, 2022
10df2c6
maintenance: workflow scala version (#833)
girdharshubham Jan 14, 2022
9c77c03
Fix: HasHeader bug (#835)
gciuloaica Jan 15, 2022
9ee2048
refactor: fix naming for Http operators (#839)
Jan 15, 2022
3c56a90
Update sbt-bloop to 1.4.12 (#810)
d11-amitsingh Jan 15, 2022
da7c459
Update scala-library to 2.13.8 (#801)
d11-amitsingh Jan 15, 2022
dfd11ac
Add configuration builder methods to zhttp.service.Server (#768)
jgoday Jan 15, 2022
134a9b5
maintenance: html template for internal server error string (#851)
girdharshubham Jan 18, 2022
13c7d2b
Performance: Improve benchmarking code (#731)
Jan 18, 2022
e9abd9f
Refactor: Support middlewares on Http (#773)
Jan 18, 2022
9fc4f40
refactor: rename ClientParams to ClientRequest (#856)
Jan 20, 2022
3b9ea3a
refactor: use declarative encoding for http.middleware (#869)
brendo-m Jan 21, 2022
ecc3d8a
Move docs to v1.x directory (#861)
d11-amitsingh Jan 21, 2022
b9784ac
Bug Add host in client from absolute URL (#847)
D11Kaushik Jan 21, 2022
a36afbc
Doc website fix (#871)
d11-amitsingh Jan 21, 2022
bcbcdd3
Update scala3-library to 3.1.1 (#873)
d11-amitsingh Jan 21, 2022
9e8544c
Doc: Fix getting started link (#874)
d11-amitsingh Jan 21, 2022
a870231
Update scalafmt-core to 3.3.2 (#864)
d11-amitsingh Jan 21, 2022
bdae5cf
Doc: `Headers` documentation (#817)
gciuloaica Jan 21, 2022
786f51a
Fix collapsible Headers Doc (#876)
d11-amitsingh Jan 22, 2022
9fbabe5
Test: Added Integration tests for `HExit.Success` (#852)
zsfVishnu-d11 Jan 24, 2022
0d59e4c
Update scalafmt-core to 3.3.3 (#881)
scala-steward Jan 24, 2022
e4d7424
Update scalafmt-core to 3.3.3 (#884)
d11-amitsingh Jan 24, 2022
0ee6dde
Fix: EncodeClientParams (#868)
ShrutiVerma97 Jan 24, 2022
dc0d80f
Refactor: HttpRunnableSpec clean up (#857)
Jan 25, 2022
6850897
update doc (#897)
Jan 26, 2022
14320ef
Docs: Getting started (#887)
ShrutiVerma97 Jan 26, 2022
fe4424e
refactor: rename asString to encode in URI (#898)
Jan 26, 2022
9d30a44
refactor: rename asString to encode in Scheme (#904)
Jan 26, 2022
470206b
fix: example links (#906)
ShrutiVerma97 Jan 26, 2022
23011a9
Update getting-started.md (#907)
d11-amitsingh Jan 26, 2022
d58857c
refactor: rename asString to encode in Scheme (#905)
Jan 26, 2022
f75030c
Doc: setup (#886)
girdharshubham Jan 26, 2022
5e714e0
Update netty-incubator-transport-native-io_uring to 0.0.12.Final (#908)
scala-steward Jan 27, 2022
b656936
Documentation for Server (#885)
sumawa Jan 27, 2022
594d8cb
Performance: Improve performance of `collectM` (#882)
Jan 27, 2022
b54e935
Doc: Added <> operator in getting started (#910)
ShrutiVerma97 Jan 27, 2022
8e73e9e
feature: Add `collectManaged` to Http (#909)
beem812 Jan 27, 2022
0a3d486
feature: add `echo` operator to `Socket` (#900)
Jan 27, 2022
cfa6c3e
feature: add `Socket.empty` (#901)
Jan 27, 2022
36f8c83
feature: add `toHttp` to Socket.scala (#902)
Jan 27, 2022
fc8b9b5
refactor: merge Request for Client and Server (#894)
Jan 27, 2022
91db176
Revert "refactor: merge Request for Client and Server (#894)" (#915)
Jan 28, 2022
d88c0c4
Feature: add `toHttp` to Response (#903)
Jan 28, 2022
4e2d48c
Update scalafmt-core to 3.4.0 (#920)
d11-amitsingh Feb 1, 2022
7d5a80f
Update sbt to 1.6.2 (#931)
scala-steward Feb 1, 2022
d8b0143
Remove outdated benchmark.md (#940)
ShrutiVerma97 Feb 1, 2022
120450b
refactor: rename `getHeaders` to `headers` in `ClientRequest` (#928)
Feb 1, 2022
aec7382
Refactor: rename `asString` to `encode` in `Path` (#927)
Feb 1, 2022
21cdee6
Disable flow Control (#854)
d11-amitsingh Feb 1, 2022
b192d89
Add builder pattern for URL (#930)
beem812 Feb 2, 2022
c1a306e
Documentation: Http (#888)
dinojohn Feb 2, 2022
456d198
feature: add `Http.apply` (#949)
Feb 3, 2022
95cddc3
Refactor: Drop `toZIO` and `wrapZIO` APIs (#950)
Feb 3, 2022
7873444
Fix: Server KeepAlive true by default and enable client to set Http v…
sumawa Feb 3, 2022
ad3cf22
feat: url add `isAbsolute` and `isRelative` operators (#946)
Feb 3, 2022
3a7e14f
WebSocket Client Support (#933)
tusharmath Feb 4, 2022
ef63c23
Documentation: Request (#926)
ShrutiVerma97 Feb 4, 2022
f396f17
Fix ssl issue due to missing peer host port hint (#952)
d11-amitsingh Feb 5, 2022
e514484
feature: add connect operator on Socket (#955)
tusharmath Feb 5, 2022
6e9c007
Style: Update scala doc wrapping (#959)
tusharmath Feb 5, 2022
b679318
remove get prefix (#958)
tusharmath Feb 5, 2022
136de3e
Update scalafmt-core to 3.4.1 (#960)
scala-steward Feb 6, 2022
2455df1
Update scalafmt-core to 3.4.2 (#962)
scala-steward Feb 6, 2022
e6925b2
Update scalafmt-core to 3.4.2 (#961)
d11-amitsingh Feb 6, 2022
c2ed52e
remove declarative API (#957)
tusharmath Feb 7, 2022
88e2c70
Update sbt-updates to 0.6.2 (#966)
scala-steward Feb 8, 2022
047944f
Doc: Readme (#970)
ShrutiVerma97 Feb 8, 2022
d3fa639
style: rearrange methods in files (#963)
tusharmath Feb 8, 2022
5e4a0cf
Doc: Cookie (#974)
ShrutiVerma97 Feb 8, 2022
b538a1d
refactor: drop `CanBeSilent` type-constraint from Http (#964)
tusharmath Feb 8, 2022
d1d917c
Feature: add `Version` (#965)
tusharmath Feb 8, 2022
ab96fdb
Fix: Fire response instead of error in HExit.Failure() (#980)
dinojohn Feb 8, 2022
9c9fd5e
fix: typo in bug_report.md (#981)
dinojohn Feb 8, 2022
2a73a95
Update netty-all to 4.1.74.Final (#982)
scala-steward Feb 9, 2022
0c0fda2
refactor: remove sealed modifier from Middleware trait (#984)
tusharmath Feb 9, 2022
4b760b4
refactor: rename method on Client (#985)
tusharmath Feb 9, 2022
7fd68ed
fix: request doc (#989)
ShrutiVerma97 Feb 9, 2022
b16b79f
Use a Gen of methods without HEAD for Responses with content (#992)
dinojohn Feb 9, 2022
e94ba3f
doc: setup g8 (#988)
girdharshubham Feb 9, 2022
1571c71
Doc: for WebSocketFrame (#953)
girdharshubham Feb 9, 2022
2bb5fd5
Documentation for HttpData (#987)
dinojohn Feb 10, 2022
88a29c3
refactor: rename `provide` and `provideSome` (#996)
girdharshubham Feb 10, 2022
35e6e32
Fix: `Content-Type` header gets incorrect values. (#899)
ashprakasan Feb 10, 2022
9ada314
Feat: Http from HExit (#986)
dinojohn Feb 10, 2022
259f171
Refactor: Remove Collect and FromFunctionZIO (#1002)
dinojohn Feb 10, 2022
0946576
Fix: Binary WebSocketFrame (#1005)
girdharshubham Feb 10, 2022
8ef998c
feature: add `fromFileZIO` (#1010)
tusharmath Feb 10, 2022
1b2e629
refactor: `fromFile` uses `fromFileZIO`
tusharmath Feb 10, 2022
f22f788
feature: add `Http.fromResource` (#1009)
tusharmath Feb 10, 2022
0ef5c29
Refactor: Content Type Fixes (#1008)
tusharmath Feb 10, 2022
61932ef
doc: fix binary constructor usage (#1014)
girdharshubham Feb 10, 2022
835f9aa
Add data in `Request` trait (#1017)
d11-amitsingh Feb 11, 2022
660d80e
maintenance: run scalafmt once (#1027)
girdharshubham Feb 14, 2022
0f906fd
Build(deps): Bump follow-redirects in /docs/website (#1029)
dependabot[bot] Feb 15, 2022
9f2fa84
Update scalafmt-core to 3.4.3 (#1022)
d11-amitsingh Feb 15, 2022
61e57a0
Remove tuple from ResponseHandler (#1033)
d11-amitsingh Feb 16, 2022
93701cd
restructuring (#1040)
ShrutiVerma97 Feb 17, 2022
70585bd
added more examples (#1038)
ShrutiVerma97 Feb 17, 2022
3f6894e
Doc: Response (#967)
ShrutiVerma97 Feb 17, 2022
77513ce
renaming (#1047)
ShrutiVerma97 Feb 17, 2022
fea9f23
Fix: Releasing request twice (#1046)
dinojohn Feb 17, 2022
b57aa88
optimize path encode method (#1035)
ShrutiVerma97 Feb 18, 2022
33ac08d
Feature: Static Server (#1024)
tusharmath Feb 19, 2022
84d5799
Revert "Feature: Static Server (#1024)" (#1060)
tusharmath Feb 19, 2022
cc22195
Performance: Use `CharSequence` for ServerTime (#1064)
tusharmath Feb 20, 2022
3e6d34c
Feature: add `dropLast` to Path (#1065)
tusharmath Feb 20, 2022
d59c005
feature: Response.html now supports taking in Status also (#1067)
tusharmath Feb 20, 2022
6cdf6c3
feature: add `code` method on Status (#1068)
tusharmath Feb 20, 2022
e0e413b
refactor: fix type info of `Http.response` (#1073)
tusharmath Feb 20, 2022
2aed79e
feature: add `foldCause` to HttpError (#1066)
tusharmath Feb 20, 2022
24a778c
feature: add `withMediaType` operator in HeaderModifier (#1071)
tusharmath Feb 20, 2022
32d29e0
Feature: add `Http.attempt` operator (#1070)
tusharmath Feb 20, 2022
e4e42fb
feature: add `getResource` and `getResourceAsFile` operators on Http …
tusharmath Feb 20, 2022
1564ed1
Update jwt-core to 9.0.4 (#1056)
d11-amitsingh Feb 20, 2022
0f5412f
Update sbt-bloop to 1.4.13 (#1055)
d11-amitsingh Feb 20, 2022
335dcd5
Build(deps): Bump url-parse from 1.5.4 to 1.5.7 in /docs/website (#1062)
dependabot[bot] Feb 20, 2022
f139ded
feature: add `ifModifiedSinceDecoded` (#1075)
tusharmath Feb 20, 2022
33fb57a
Feature: Static Server (#1061)
tusharmath Feb 20, 2022
29c37c6
issue 715 Support custom ChannelInitializer (#932)
rajcspsg Feb 20, 2022
f632f58
doc: socket (#1036)
girdharshubham Feb 21, 2022
042291a
feat: added new constructor in http (#1077)
ShrutiVerma97 Feb 23, 2022
9914695
Feature: Add `when` operator in `Http` (#1078)
ShrutiVerma97 Feb 24, 2022
8cea811
Feature: Effectful Auth Middleware (#1079)
dinojohn Feb 24, 2022
06c29d2
Test: Server Settings Support in `HttpRunnableSpec#serve` (#1053)
girdharshubham Feb 24, 2022
7f9b409
refactor: website docs sidebar pages repositioned (#1049)
girdharshubham Feb 25, 2022
a9ae523
Feat: Server Request Decompression (#1095)
girdharshubham Feb 25, 2022
0096ea9
Feature: Added helper checkers to `Status` class (#1058)
rleibman Feb 25, 2022
4ddc855
Build(deps): Bump prismjs from 1.26.0 to 1.27.0 in /docs/website (#1100)
dependabot[bot] Feb 28, 2022
7cc3555
Build(deps): Bump url-parse from 1.5.7 to 1.5.10 in /docs/website (#1…
dependabot[bot] Feb 28, 2022
ecffdff
added try catch in handler to catch throwable apps (#1099)
dinojohn Feb 28, 2022
69788f3
removed flatten from test (#1109)
ShrutiVerma97 Mar 1, 2022
8e4f6bf
Feature: Request Streaming (#1048)
d11-amitsingh Mar 1, 2022
2bbeb9b
Fix toByteBuf for streamed HttpData (#1118)
ex0ns Mar 5, 2022
0dca97d
example: more secure string compare for login (#1120)
MasseGuillaume Mar 7, 2022
dc33298
Feature: Introduced defect channel for `Http` (#1083)
nartamonov Mar 7, 2022
d603324
Enhancement: Merge server and client Responses (#1111)
d11-amitsingh Mar 7, 2022
48ce70c
rename getBodyAsString => bodyAsString in doc (#1124)
MasseGuillaume Mar 8, 2022
37657eb
removed links from scaladoc (#1127)
dinojohn Mar 8, 2022
c1d98ea
Feature: Add `version` to `Request` (#1094)
d11-amitsingh Mar 9, 2022
94be6b9
Refactor: Merge client and server `Request` (#1125)
d11-amitsingh Mar 9, 2022
5069856
Refactor: Http.Status names to Camel Case (#1129)
tusharmath Mar 10, 2022
0de7b80
Feature: Support Custom statuses code (#1121)
gciuloaica Mar 10, 2022
006e318
Enhancement: Added combine operator (#1106)
ShrutiVerma97 Mar 10, 2022
b3613d2
Refactor: Added lift in PartialCollect (#1105)
ShrutiVerma97 Mar 10, 2022
ec62037
Feature: Bearer auth middleware (#1097)
dinojohn Mar 10, 2022
4cab597
Update netty-all to 4.1.75.Final (#1130)
scala-steward Mar 10, 2022
15e9920
Refactor: `Request` and `Response` to extend `HttpDataExtension` (#1112)
d11-amitsingh Mar 10, 2022
cf299f4
Refactor: Make `Http.getResource` consistent with `ZStream.fromResour…
guizmaii Mar 13, 2022
f18e0ef
Update netty-incubator-transport-native-io_uring to 0.0.13.Final (#1133)
scala-steward Mar 14, 2022
061abe5
Add more useful toString method for Request (#995)
nartamonov Mar 16, 2022
6aa1aef
fix(request): fix scala doc (#1138)
D11Kaushik Mar 16, 2022
2f47081
Performance: Improve HttpData toByteBuf (#1137)
tusharmath Mar 20, 2022
6e5008a
Fix: Handle HttpClient Interruption on connection close (#1039)
gciuloaica Mar 21, 2022
c24c67b
New chapter: efficient development process (#1145)
DaniRey Mar 21, 2022
97c3eaf
do not watch reStop (#1146)
DaniRey Mar 21, 2022
91a0558
Feature: add `Middleware.codecHttp` (#1141)
tusharmath Mar 25, 2022
fe48544
Performance: Use `CharSequence` internally wherever possible (#1142)
tusharmath Mar 26, 2022
55b28fd
Update scala-collection-compat to 2.7.0 (#1154)
d11-amitsingh Mar 26, 2022
57e576b
Update jwt-core to 9.0.5 (#1150)
d11-amitsingh Mar 26, 2022
0a5c0c8
feature: add `narrow` operator to Http (#1161)
tusharmath Mar 27, 2022
2114ccb
feature: add `toHttp` method to `Response` (#1160)
tusharmath Mar 27, 2022
71a5b0a
refactor: DynamicServer is not bound to HttpEnv only (#1159)
tusharmath Mar 28, 2022
568dfb6
fix: Socket.end implementation (#1158)
tusharmath Mar 28, 2022
d978571
Update sbt-scala3-migrate to 0.5.1 (#1163)
d11-amitsingh Mar 30, 2022
1bd42c8
Merge remote-tracking branch 'origin/main' into backmerge-31-03-2022
amitksingh1490 Apr 1, 2022
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
Prev Previous commit
Next Next commit
added try catch in handler to catch throwable apps (#1099)
  • Loading branch information
dinojohn authored Feb 28, 2022
commit ecffdffccb974254d65d9c421e6f65d0bf8cface
56 changes: 34 additions & 22 deletions zio-http/src/main/scala/zhttp/service/Handler.scala
Original file line number Diff line number Diff line change
@@ -23,28 +23,40 @@ private[zhttp] final case class Handler[R](
override def channelRead0(ctx: Ctx, jReq: FullHttpRequest): Unit = {
jReq.touch("server.Handler-channelRead0")
implicit val iCtx: ChannelHandlerContext = ctx
unsafeRun(
jReq,
app,
new Request {
override def method: Method = Method.fromHttpMethod(jReq.method())

override def url: URL = URL.fromString(jReq.uri()).getOrElse(null)

override def headers: Headers = Headers.make(jReq.headers())

override def unsafeEncode: HttpRequest = jReq

override def remoteAddress: Option[InetAddress] = {
ctx.channel().remoteAddress() match {
case m: InetSocketAddress => Some(m.getAddress)
case _ => None
}
}

override def data: HttpData = HttpData.fromByteBuf(jReq.content())
},
)
try
(
unsafeRun(
jReq,
app,
new Request {
override def method: Method = Method.fromHttpMethod(jReq.method())

override def url: URL = URL.fromString(jReq.uri()).getOrElse(null)

override def headers: Headers = Headers.make(jReq.headers())

override def unsafeEncode: HttpRequest = jReq

override def remoteAddress: Option[InetAddress] = {
ctx.channel().remoteAddress() match {
case m: InetSocketAddress => Some(m.getAddress)
case _ => None
}
}

override def data: HttpData = HttpData.fromByteBuf(jReq.content())
},
),
)
catch {
case throwable: Throwable =>
writeResponse(
Response
.fromHttpError(HttpError.InternalServerError(cause = Some(throwable)))
.withConnection(HeaderValues.close),
jReq,
): Unit
}
}

/**
91 changes: 6 additions & 85 deletions zio-http/src/test/scala/zhttp/service/ServerSpec.scala
Original file line number Diff line number Diff line change
@@ -23,19 +23,7 @@ object ServerSpec extends HttpRunnableSpec {
private val env =
EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live

private val staticApp = Http.collectZIO[Request] {
case Method.GET -> !! / "success" => ZIO.succeed(Response.ok)
case Method.GET -> !! / "failure" => ZIO.fail(new RuntimeException("FAILURE"))
case Method.GET -> !! / "get%2Fsuccess" => ZIO.succeed(Response.ok)
}

// Use this route to test anything that doesn't require ZIO related computations.
private val nonZIO = Http.collectHExit[Request] {
case _ -> !! / "HExitSuccess" => HExit.succeed(Response.ok)
case _ -> !! / "HExitFailure" => HExit.fail(new RuntimeException("FAILURE"))
}

private val app = serve(nonZIO ++ staticApp ++ DynamicServer.app, Some(Server.requestDecompression(true)))
private val app = serve(DynamicServer.app, Some(Server.requestDecompression(true)))

def dynamicAppSpec = suite("DynamicAppSpec") {
suite("success") {
@@ -141,40 +129,6 @@ object ServerSpec extends HttpRunnableSpec {
}
}

def nonZIOSpec = suite("NonZIOSpec") {
testM("200 response") {
checkAllM(HttpGen.method) { method =>
val actual = status(method, !! / "HExitSuccess")
assertM(actual)(equalTo(Status.OK))
}
} +
testM("500 response") {
val methodGenWithoutHEAD: Gen[Any, Method] = Gen.fromIterable(
List(
Method.OPTIONS,
Method.GET,
Method.POST,
Method.PUT,
Method.PATCH,
Method.DELETE,
Method.TRACE,
Method.CONNECT,
),
)
checkAllM(methodGenWithoutHEAD) { method =>
val actual = status(method, !! / "HExitFailure")
assertM(actual)(equalTo(Status.INTERNAL_SERVER_ERROR))
}
} +
testM("404 response ") {
checkAllM(HttpGen.method) { method =>
val actual = status(method, !! / "A")
assertM(actual)(equalTo(Status.NOT_FOUND))
}
}

}

def requestSpec = suite("RequestSpec") {
val app: HttpApp[Any, Nothing] = Http.collect[Request] { case req =>
Response.text(req.contentLength.getOrElse(-1).toString)
@@ -287,46 +241,13 @@ object ServerSpec extends HttpRunnableSpec {
}
}

def serverStartSpec = suite("ServerStartSpec") {
testM("desired port") {
val port = 8088
(Server.port(port) ++ Server.app(Http.empty)).make.use { start =>
assertM(ZIO.effect(start.port))(equalTo(port))
}
} +
testM("available port") {
(Server.port(0) ++ Server.app(Http.empty)).make.use { start =>
assertM(ZIO.effect(start.port))(not(equalTo(0)))
}
}
}

override def spec =
suiteM("Server") {
app.as(List(serverStartSpec, staticAppSpec, dynamicAppSpec, responseSpec, requestSpec, nonZIOSpec)).useNow
app
.as(
List(dynamicAppSpec, responseSpec, requestSpec),
)
.useNow
}.provideCustomLayerShared(env) @@ timeout(30 seconds)

def staticAppSpec = suite("StaticAppSpec") {
testM("200 response") {
val actual = status(path = !! / "success")
assertM(actual)(equalTo(Status.OK))
} +
testM("500 response") {
val actual = status(path = !! / "failure")
assertM(actual)(equalTo(Status.INTERNAL_SERVER_ERROR))
} +
testM("404 response") {
val actual = status(path = !! / "random")
assertM(actual)(equalTo(Status.NOT_FOUND))
} +
testM("200 response with encoded path") {
val actual = status(path = !! / "get%2Fsuccess")
assertM(actual)(equalTo(Status.OK))
} +
testM("Multiple 200 response") {
for {
data <- status(path = !! / "success").repeatN(1024)
} yield assertTrue(data == Status.OK)
}
}
}
119 changes: 119 additions & 0 deletions zio-http/src/test/scala/zhttp/service/StaticServerSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package zhttp.service

import zhttp.http._
import zhttp.internal.{DynamicServer, HttpGen, HttpRunnableSpec}
import zhttp.service.server._
import zio.ZIO
import zio.duration.durationInt
import zio.test.Assertion._
import zio.test.TestAspect._
import zio.test._

object StaticServerSpec extends HttpRunnableSpec {

private val env =
EventLoopGroup.nio() ++ ChannelFactory.nio ++ ServerChannelFactory.nio ++ DynamicServer.live

private val staticApp = Http.collectZIO[Request] {
case Method.GET -> !! / "success" => ZIO.succeed(Response.ok)
case Method.GET -> !! / "failure" => ZIO.fail(new RuntimeException("FAILURE"))
case Method.GET -> !! / "get%2Fsuccess" => ZIO.succeed(Response.ok)
}

// Use this route to test anything that doesn't require ZIO related computations.
private val nonZIO = Http.collectHExit[Request] {
case _ -> !! / "HExitSuccess" => HExit.succeed(Response.ok)
case _ -> !! / "HExitFailure" => HExit.fail(new RuntimeException("FAILURE"))
case _ -> !! / "throwable" => throw new Exception("Throw inside Handler")
}

private val app = serve { nonZIO ++ staticApp }

def nonZIOSpec = suite("NonZIOSpec") {
testM("200 response") {
checkAllM(HttpGen.method) { method =>
val actual = status(method, !! / "HExitSuccess")
assertM(actual)(equalTo(Status.OK))
}
} +
testM("500 response") {
val methodGenWithoutHEAD: Gen[Any, Method] = Gen.fromIterable(
List(
Method.OPTIONS,
Method.GET,
Method.POST,
Method.PUT,
Method.PATCH,
Method.DELETE,
Method.TRACE,
Method.CONNECT,
),
)
checkAllM(methodGenWithoutHEAD) { method =>
val actual = status(method, !! / "HExitFailure")
assertM(actual)(equalTo(Status.INTERNAL_SERVER_ERROR))
}
} +
testM("404 response ") {
checkAllM(HttpGen.method) { method =>
val actual = status(method, !! / "A")
assertM(actual)(equalTo(Status.NOT_FOUND))
}
}

}

def serverStartSpec = suite("ServerStartSpec") {
testM("desired port") {
val port = 8088
(Server.port(port) ++ Server.app(Http.empty)).make.use { start =>
assertM(ZIO.effect(start.port))(equalTo(port))
}
} +
testM("available port") {
(Server.port(0) ++ Server.app(Http.empty)).make.use { start =>
assertM(ZIO.effect(start.port))(not(equalTo(0)))
}
}
}

override def spec =
suiteM("Server") {
app
.as(
List(serverStartSpec, staticAppSpec, nonZIOSpec, throwableAppSpec),
)
.useNow
}.provideCustomLayerShared(env) @@ timeout(30 seconds)

def staticAppSpec = suite("StaticAppSpec") {
testM("200 response") {
val actual = status(path = !! / "success")
assertM(actual)(equalTo(Status.OK))
} +
testM("500 response") {
val actual = status(path = !! / "failure")
assertM(actual)(equalTo(Status.INTERNAL_SERVER_ERROR))
} +
testM("404 response") {
val actual = status(path = !! / "random")
assertM(actual)(equalTo(Status.NOT_FOUND))
} +
testM("200 response with encoded path") {
val actual = status(path = !! / "get%2Fsuccess")
assertM(actual)(equalTo(Status.OK))
} +
testM("Multiple 200 response") {
for {
data <- status(path = !! / "success").repeatN(1024)
} yield assertTrue(data == Status.OK)
}
}
def throwableAppSpec = suite("ThrowableAppSpec") {
testM("Throw inside Handler") {
for {
status <- status(Method.GET, !! / "throwable")
} yield assertTrue(status == Status.INTERNAL_SERVER_ERROR)
}
}
}