-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: merge Request for Client and Server
- Loading branch information
1 parent
dc0d80f
commit 55089b8
Showing
8 changed files
with
157 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 32 additions & 31 deletions
63
zio-http/src/main/scala/zhttp/service/EncodeClientParams.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,42 @@ | ||
package zhttp.service | ||
|
||
import io.netty.buffer.Unpooled | ||
import io.netty.handler.codec.http.{DefaultFullHttpRequest, FullHttpRequest, HttpHeaderNames, HttpVersion} | ||
import zhttp.http.HTTP_CHARSET | ||
import zhttp.http.Request | ||
import zio.Task | ||
trait EncodeClientParams { | ||
|
||
/** | ||
* Converts client params to JFullHttpRequest | ||
*/ | ||
def encodeClientParams(jVersion: HttpVersion, req: Client.ClientRequest): FullHttpRequest = { | ||
val method = req.method.asHttpMethod | ||
val url = req.url | ||
|
||
// As per the spec, the path should contain only the relative path. | ||
// Host and port information should be in the headers. | ||
val path = url.relative.asString | ||
|
||
val content = req.getBodyAsString match { | ||
case Some(text) => Unpooled.copiedBuffer(text, HTTP_CHARSET) | ||
case None => Unpooled.EMPTY_BUFFER | ||
} | ||
|
||
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()) | ||
} | ||
// TODO: we should also add a default user-agent req header as some APIs might reject requests without it. | ||
val jReq = new DefaultFullHttpRequest(jVersion, method, path, content) | ||
jReq.headers().set(headers) | ||
|
||
jReq | ||
def encodeClientParams(jVersion: HttpVersion, req: Request): Task[FullHttpRequest] = req.getBodyAsByteBuf.map { | ||
content => | ||
val method = req.method.asHttpMethod | ||
val url = req.url | ||
|
||
// As per the spec, the path should contain only the relative path. | ||
// Host and port information should be in the headers. | ||
val path = url.relative.asString | ||
|
||
// val content = req.getBodyAsString match { | ||
// case Some(text) => Unpooled.copiedBuffer(text, HTTP_CHARSET) | ||
// case None => Unpooled.EMPTY_BUFFER | ||
// } | ||
|
||
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()) | ||
} | ||
// TODO: we should also add a default user-agent req header as some APIs might reject requests without it. | ||
val jReq = new DefaultFullHttpRequest(jVersion, method, path, content) | ||
jReq.headers().set(headers) | ||
|
||
jReq | ||
} | ||
} |
83 changes: 0 additions & 83 deletions
83
zio-http/src/test/scala/zhttp/http/EncodeClientRequestSpec.scala
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
zio-http/src/test/scala/zhttp/http/EncodeRequestSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package zhttp.http | ||
|
||
import io.netty.handler.codec.http.{HttpHeaderNames, HttpVersion} | ||
import zhttp.internal.HttpGen | ||
import zhttp.service.EncodeClientParams | ||
import zio.random.Random | ||
import zio.test.Assertion._ | ||
import zio.test._ | ||
|
||
object EncodeRequestSpec extends DefaultRunnableSpec with EncodeClientParams { | ||
|
||
val anyClientParam: Gen[Random with Sized, Request] = HttpGen.clientRequest( | ||
HttpGen.httpData( | ||
Gen.listOf(Gen.alphaNumericString), | ||
), | ||
) | ||
|
||
val clientParamWithAbsoluteUrl = HttpGen.clientRequest( | ||
dataGen = HttpGen.httpData( | ||
Gen.listOf(Gen.alphaNumericString), | ||
), | ||
urlGen = HttpGen.genAbsoluteURL, | ||
) | ||
|
||
def clientParamWithFiniteData(size: Int): Gen[Random with Sized, Request] = HttpGen.clientRequest( | ||
for { | ||
content <- Gen.alphaNumericStringBounded(size, size) | ||
data <- Gen.fromIterable(List(HttpData.fromString(content))) | ||
} yield data, | ||
) | ||
|
||
def spec = suite("EncodeClientParams") { | ||
testM("method") { | ||
checkM(anyClientParam) { params => | ||
val method = encodeClientParams(HttpVersion.HTTP_1_1, params).map(_.method()) | ||
assertM(method)(equalTo(params.method.asHttpMethod)) | ||
} | ||
} + | ||
testM("method on HttpData.File") { | ||
checkM(HttpGen.clientParamsForFileHttpData()) { params => | ||
val method = encodeClientParams(HttpVersion.HTTP_1_1, params).map(_.method()) | ||
assertM(method)(equalTo(params.method.asHttpMethod)) | ||
} | ||
} + | ||
suite("uri") { | ||
testM("uri") { | ||
checkM(anyClientParam) { params => | ||
val uri = encodeClientParams(HttpVersion.HTTP_1_1, params).map(_.uri()) | ||
assertM(uri)(equalTo(params.url.relative.asString)) | ||
} | ||
} + | ||
testM("uri on HttpData.File") { | ||
checkM(HttpGen.clientParamsForFileHttpData()) { params => | ||
val uri = encodeClientParams(HttpVersion.HTTP_1_1, params).map(_.uri()) | ||
assertM(uri)(equalTo(params.url.relative.asString)) | ||
} | ||
} | ||
} + | ||
testM("content-length") { | ||
checkM(clientParamWithFiniteData(5)) { params => | ||
val len = encodeClientParams(HttpVersion.HTTP_1_1, params).map( | ||
_.headers().getInt(HttpHeaderNames.CONTENT_LENGTH).toLong, | ||
) | ||
assertM(len)(equalTo(5L)) | ||
} | ||
} + | ||
testM("host header") { | ||
checkM(anyClientParam) { params => | ||
val hostHeader = HttpHeaderNames.HOST | ||
val headers = encodeClientParams(HttpVersion.HTTP_1_1, params).map(h => Option(h.headers().get(hostHeader))) | ||
assertM(headers)(equalTo(params.url.host)) | ||
} | ||
} + | ||
testM("host header when absolute url") { | ||
checkM(clientParamWithAbsoluteUrl) { params => | ||
val hostHeader = HttpHeaderNames.HOST | ||
for { | ||
reqHeaders <- encodeClientParams(HttpVersion.HTTP_1_1, params).map(_.headers()) | ||
} yield assert(reqHeaders.getAll(hostHeader).size)(equalTo(1)) && assert(Option(reqHeaders.get(hostHeader)))( | ||
equalTo(params.url.host), | ||
) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.