Skip to content

Commit

Permalink
Bug Add host in client from absolute URL (#847)
Browse files Browse the repository at this point in the history
* feat(Client): Add host in client from absolute URL

* feat(Client): Refactor and add spec for host value

* feat(Client): fmt formatting

* feat(Client): fmt formatting

* feat(Client): use setHeaders from netty

* feat(Client): Add spec for host

* feat(Client): Add spec for host

* feat(Client): Rename clientParams to clientRequest
  • Loading branch information
D11Kaushik authored Jan 21, 2022
1 parent ecc3d8a commit b9784ac
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
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
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)

}

0 comments on commit b9784ac

Please sign in to comment.