Skip to content

Commit

Permalink
Merge branch 'main' of github.com:dream11/zio-http into docs-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
gciuloaica committed Jan 21, 2022
2 parents 24752b7 + a870231 commit 183cf06
Show file tree
Hide file tree
Showing 56 changed files with 8,956 additions and 32 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.12.15, 2.13.8, 3.1.0]
scala: [2.12.15, 2.13.8, 3.1.1]
java: [graal_21.1.0@11, temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -143,12 +143,12 @@ jobs:
tar xf targets.tar
rm targets.tar
- name: Download target directories (3.1.0)
- name: Download target directories (3.1.1)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-3.1.0-${{ matrix.java }}
name: target-${{ matrix.os }}-3.1.1-${{ matrix.java }}

- name: Inflate target directories (3.1.0)
- name: Inflate target directories (3.1.1)
run: |
tar xf targets.tar
rm targets.tar
Expand Down
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 3.3.1
version = 3.3.2
maxColumn = 120

align.preset = more
Expand Down
4 changes: 4 additions & 0 deletions docs/website/docs/v1.x/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "v1.x",
"position": 1
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions docs/website/docs/v2.x/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "v2.x",
"position": 2
}
4 changes: 2 additions & 2 deletions docs/website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const config = {
items: [
{
type: 'doc',
docId: 'index',
docId: 'v1.x/index',
position: 'left',
label: 'Tutorial',
}
Expand All @@ -57,7 +57,7 @@ const config = {
items: [
{
label: 'Documents',
to: '/docs/index',
to: '/docs/v1.x/index',
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion docs/website/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function HomepageHeader() {
<div className={styles.buttons}>
<Link
className="button button--secondary button--lg"
to="/docs/index">
to="/docs/v1.x/index">
Get Started
</Link>
</div>
Expand Down
8,873 changes: 8,873 additions & 0 deletions docs/website/yarn.lock

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions example/src/main/scala/example/SimpleClient.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package example

import zhttp.http.Headers
import zhttp.service.{ChannelFactory, Client, EventLoopGroup}
import zio.{App, ExitCode, URIO, console}

object SimpleClient extends App {
val env = ChannelFactory.auto ++ EventLoopGroup.auto()
val url = "http://sports.api.decathlon.com/groups/water-aerobics"
val headers = Headers.host("sports.api.decathlon.com")
val env = ChannelFactory.auto ++ EventLoopGroup.auto()
val url = "http://sports.api.decathlon.com/groups/water-aerobics"

val program = for {
res <- Client.request(url, headers)
res <- Client.request(url)
data <- res.getBodyAsString
_ <- console.putStrLn { data }
} yield ()
Expand Down
2 changes: 1 addition & 1 deletion project/BuildHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import xerial.sbt.Sonatype.autoImport._
object BuildHelper extends ScalaSettings {
val Scala212 = "2.12.15"
val Scala213 = "2.13.8"
val ScalaDotty = "3.1.0"
val ScalaDotty = "3.1.1"
val ScoverageVersion = "1.9.3"

private val stdOptions = Seq(
Expand Down
16 changes: 12 additions & 4 deletions zio-http/src/main/scala/zhttp/service/EncodeClientParams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ trait EncodeClientParams {
* Converts client params to JFullHttpRequest
*/
def encodeClientParams(jVersion: HttpVersion, req: Client.ClientRequest): FullHttpRequest = {
val method = req.method.asHttpMethod
val uri = req.url.asString
val content = req.getBodyAsString match {
val method = req.method.asHttpMethod
val url = req.url
val uri = url.asString
val content = req.getBodyAsString match {
case Some(text) => Unpooled.copiedBuffer(text, HTTP_CHARSET)
case None => Unpooled.EMPTY_BUFFER
}
val headers = req.getHeaders.encode

val encodedReqHeaders = req.getHeaders.encode

val headers = url.host match {
case Some(value) => encodedReqHeaders.set(HttpHeaderNames.HOST, value)
case None => encodedReqHeaders
}

val writerIndex = content.writerIndex()
if (writerIndex != 0) {
headers.set(HttpHeaderNames.CONTENT_LENGTH, writerIndex.toString())
Expand Down
28 changes: 26 additions & 2 deletions zio-http/src/test/scala/zhttp/http/EncodeClientRequestSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import zio.test._

object EncodeClientRequestSpec extends DefaultRunnableSpec with EncodeClientParams {

val anyClientParam: Gen[Random with Sized, Client.ClientRequest] = HttpGen.clientParams(
val anyClientParam: Gen[Random with Sized, Client.ClientRequest] = HttpGen.clientRequest(
HttpGen.httpData(
Gen.listOf(Gen.alphaNumericString),
),
)

def clientParamWithFiniteData(size: Int): Gen[Random with Sized, Client.ClientRequest] = HttpGen.clientParams(
val clientParamWithAbsoluteUrl = HttpGen.clientRequest(
dataGen = HttpGen.httpData(
Gen.listOf(Gen.alphaNumericString),
),
urlGen = HttpGen.genAbsoluteURL,
)

def clientParamWithFiniteData(size: Int): Gen[Random with Sized, Client.ClientRequest] = HttpGen.clientRequest(
for {
content <- Gen.alphaNumericStringBounded(size, size)
data <- Gen.fromIterable(List(HttpData.fromString(content)))
Expand Down Expand Up @@ -52,6 +59,23 @@ object EncodeClientRequestSpec extends DefaultRunnableSpec with EncodeClientPara
val req = encodeClientParams(HttpVersion.HTTP_1_1, params)
assert(req.headers().getInt(HttpHeaderNames.CONTENT_LENGTH).toLong)(equalTo(5L))
}
} +
testM("host header") {
check(anyClientParam) { params =>
val req = encodeClientParams(HttpVersion.HTTP_1_1, params)
val hostHeader = HttpHeaderNames.HOST
assert(Option(req.headers().get(hostHeader)))(equalTo(params.url.host))
}
} +
testM("host header when absolute url") {
check(clientParamWithAbsoluteUrl) { params =>
val req = encodeClientParams(HttpVersion.HTTP_1_1, params)
val reqHeaders = req.headers()
val hostHeader = HttpHeaderNames.HOST

assert(reqHeaders.getAll(hostHeader).size)(equalTo(1)) &&
assert(Option(reqHeaders.get(hostHeader)))(equalTo(params.url.host))
}
}
}
}
37 changes: 25 additions & 12 deletions zio-http/src/test/scala/zhttp/internal/HttpGen.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zhttp.internal

import io.netty.buffer.Unpooled
import zhttp.http.URL.Location
import zhttp.http._
import zhttp.service.Client.ClientRequest
import zio.random.Random
Expand All @@ -11,11 +12,16 @@ import zio.{Chunk, ZIO}
import java.io.File

object HttpGen {
def clientParams[R](dataGen: Gen[R, HttpData]) =
def clientRequest[R](
dataGen: Gen[R, HttpData],
methodGen: Gen[R, Method] = HttpGen.method,
urlGen: Gen[Random with Sized, URL] = HttpGen.url,
headerGen: Gen[Random with Sized, Header] = HttpGen.header,
) =
for {
method <- HttpGen.method
url <- HttpGen.url
headers <- Gen.listOf(HttpGen.header).map(Headers(_))
method <- methodGen
url <- urlGen
headers <- Gen.listOf(headerGen).map(Headers(_))
data <- dataGen
} yield ClientRequest(method, url, headers, data)

Expand Down Expand Up @@ -61,16 +67,16 @@ object HttpGen {
)
} yield cnt

def location: Gen[Random with Sized, URL.Location] = {
def genRelative = Gen.const(URL.Location.Relative)
def genRelativeLocation: Gen[Any, Location.Relative.type] = Gen.const(URL.Location.Relative)

def genAbsolute = for {
scheme <- Gen.fromIterable(List(Scheme.HTTP, Scheme.HTTPS))
host <- Gen.alphaNumericStringBounded(1, 5)
port <- Gen.int(0, Int.MaxValue)
} yield URL.Location.Absolute(scheme, host, port)
def genAbsoluteLocation: Gen[Random with Sized, Location.Absolute] = for {
scheme <- Gen.fromIterable(List(Scheme.HTTP, Scheme.HTTPS))
host <- Gen.alphaNumericStringBounded(1, 5)
port <- Gen.int(0, Int.MaxValue)
} yield URL.Location.Absolute(scheme, host, port)

Gen.fromIterable(List(genRelative, genAbsolute)).flatten
def location: Gen[Random with Sized, URL.Location] = {
Gen.fromIterable(List(genRelativeLocation, genAbsoluteLocation)).flatten
}

def method: Gen[Any, Method] = Gen.fromIterable(
Expand Down Expand Up @@ -189,4 +195,11 @@ object HttpGen {
kind <- HttpGen.location
queryParams <- Gen.mapOf(Gen.alphaNumericString, Gen.listOf(Gen.alphaNumericString))
} yield URL(path, kind, queryParams)

def genAbsoluteURL = for {
path <- HttpGen.path
kind <- HttpGen.genAbsoluteLocation
queryParams <- Gen.mapOf(Gen.alphaNumericString, Gen.listOf(Gen.alphaNumericString))
} yield URL(path, kind, queryParams)

}

1 comment on commit 183cf06

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance Benchmark:

Concurrency: 256
Requests/sec: 932411.70

Please sign in to comment.