Skip to content

Commit

Permalink
Merge pull request #1233 from disneystreaming/add-http-protocol
Browse files Browse the repository at this point in the history
Keep protocol in smithy4s->http4s URI conversion
  • Loading branch information
Baccata authored Oct 4, 2023
2 parents 7b21e56 + 7877a71 commit af517d7
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 3 deletions.
14 changes: 11 additions & 3 deletions modules/http4s-kernel/src/smithy4s/http4s/kernel/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ package object kernel {

def toSmithy4sHttpUri(uri: Uri, pathParams: Option[PathParams] = None): Smithy4sHttpUri = {
val uriScheme = uri.scheme match {
case Some(Uri.Scheme.http) => Smithy4sHttpUriScheme.Http
case _ => Smithy4sHttpUriScheme.Https
case Some(Uri.Scheme.https) => Smithy4sHttpUriScheme.Https
case _ => Smithy4sHttpUriScheme.Http
}

Smithy4sHttpUri(
uriScheme,
uri.host.map(_.renderString).getOrElse("localhost"),
Expand Down Expand Up @@ -97,9 +98,16 @@ package object kernel {

def fromSmithy4sHttpUri(uri: Smithy4sHttpUri): Uri = {
val path = Uri.Path.Root.addSegments(uri.path.map(Uri.Path.Segment(_)).toVector)

Uri(
path = path,
authority = Some(Uri.Authority(host = Uri.RegName(uri.host), port = uri.port))
authority = Some(Uri.Authority(host = Uri.RegName(uri.host), port = uri.port)),
scheme = Some {
uri.scheme match {
case Smithy4sHttpUriScheme.Http => Uri.Scheme.http
case Smithy4sHttpUriScheme.Https => Uri.Scheme.https
}
}
).withMultiValueQueryParams(uri.queryParams)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2021-2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package smithy4s.http4s.kernel

import weaver._
import org.http4s.implicits._
import org.http4s.Uri
import smithy4s.http.HttpUriScheme

object Http4sConversionSpec extends SimpleIOSuite {

http4sToSmithyAndBackUriTest(
uri"/",
uri"http://localhost/"
)

http4sToSmithyAndBackUriTest(
uri"/hello",
uri"http://localhost/hello"
)

http4sToSmithyAndBackUriTest(
uri"http://example.com",
uri"http://example.com/"
)

http4sToSmithyAndBackUriTest(
uri"example.com",
uri"http://example.com/"
)

http4sToSmithyAndBackUriTest(
uri"https://example.com",
uri"https://example.com/"
)

http4sToSmithyAndBackUriTest(
uri"https://example.com/hello",
uri"https://example.com/hello"
)

http4sToSmithyAndBackUriTest(
uri"https://example.com/hello?s=42",
uri"https://example.com/hello?s=42"
)

http4sToSmithyAndBackUriTest(
uri"http://localhost/",
uri"http://localhost/"
)

pureTest("URI: http4s to smithy4s defaults to http") {
assert.same(
smithy4s.http.HttpUriScheme.Http,
toSmithy4sHttpUri(uri"/").scheme
)
}

pureTest("URI: http4s to smithy4s keeps http scheme") {
assert.same(
smithy4s.http.HttpUriScheme.Http,
toSmithy4sHttpUri(uri"http://localhost").scheme
)
}

pureTest("URI: http4s to smithy4s keeps https scheme") {
assert.same(
smithy4s.http.HttpUriScheme.Https,
toSmithy4sHttpUri(uri"https://localhost").scheme
)
}

pureTest("URI: smithy4s to http4s keeps http scheme") {
assert.same(
Some(Uri.Scheme.http),
fromSmithy4sHttpUri(
aSmithy4sUri(
scheme = HttpUriScheme.Http
)
).scheme
)
}

pureTest("URI: smithy4s to http4s keeps http scheme") {
assert.same(
Some(Uri.Scheme.https),
fromSmithy4sHttpUri(
aSmithy4sUri(
scheme = HttpUriScheme.Https
)
).scheme
)
}

private def http4sToSmithyAndBackUriTest(input: Uri, output: Uri) = {
pureTest(s"URI: http4s to smithy4s and back: $input -> $output") {
assert.eql(
uri"http://localhost/",
fromSmithy4sHttpUri(toSmithy4sHttpUri(uri"http://localhost/"))
)
}
}

private def aSmithy4sUri(scheme: HttpUriScheme) =
smithy4s.http.HttpUri(
scheme = scheme,
host = "localhost",
port = None,
path = IndexedSeq.empty,
queryParams = Map.empty,
pathParams = None
)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2021-2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package smithy4s.codegen.mill

import mill._
Expand Down

0 comments on commit af517d7

Please sign in to comment.