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

Compliancetests fixes improvements #680

Merged
merged 16 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.http4s.Request
import org.http4s.Response
import org.http4s.Status
import org.http4s.Uri
import org.typelevel.ci.CIString
import smithy.test._
import smithy4s.compliancetests.ComplianceTest.ComplianceResult
import smithy4s.http.CodecAPI
Expand All @@ -35,7 +34,7 @@ import smithy4s.Service
import scala.concurrent.duration._
import smithy4s.http.HttpMediaType
import org.http4s.MediaType
import org.http4s.Header
import org.http4s.Headers

private[compliancetests] class ClientHttpComplianceTestCase[
F[_],
Expand Down Expand Up @@ -186,21 +185,20 @@ private[compliancetests] class ClientHttpComplianceTestCase[
.through(utf8Encode)
}
.getOrElse(fs2.Stream.empty)
val headers: Seq[Header.ToRaw] =
testCase.headers.toList
.flatMap(_.toList)
.map { case (key, value) =>
Header.Raw(CIString(key), value)
}
.map(Header.ToRaw.rawToRaw)
.toSeq

// val headers = extractHeaders(testCase.headers).toList.flatten

val headers = List(
extractHeaders(testCase.headers),
Some(
Headers(`Content-Type`(MediaType.unsafeParse(mediaType.value)))
)
).foldMap(_.combineAll)

req.body.compile.drain.as(
Response[F](status)
.withBodyStream(body)
.putHeaders(headers: _*)
.putHeaders(
`Content-Type`(MediaType.unsafeParse(mediaType.value))
)
.withHeaders(headers)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ private[compliancetests] class ServerHttpComplianceTestCase[
): Request[F] = {
val expectedHeaders =
List(
testCase.headers.map(h =>
Headers(h.toList.map(a => a: Header.ToRaw): _*)
),
extractHeaders(testCase.headers),
testCase.bodyMediaType.map(mt =>
Headers(`Content-Type`(MediaType.unsafeParse(mt)))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package smithy4s
package compliancetests

import org.http4s.Uri
import org.http4s.{Header, Headers, Uri}
import cats.implicits._

import java.nio.charset.StandardCharsets
import scala.collection.immutable.{ListMap}
import scala.annotation.tailrec
import scala.collection.immutable.ListMap

package object internals {

Expand Down Expand Up @@ -62,4 +63,33 @@ package object internals {
}
}
}

def extractHeaders(
maybeHeaders: Option[Map[String, String]]
): Option[Headers] =
yisraelU marked this conversation as resolved.
Show resolved Hide resolved
maybeHeaders.map(h =>
Headers(h.toList.flatMap(parseSingleHeader).map(a => a: Header.ToRaw): _*)
)

// regex for comma not between quotes
private val commaNotBetweenQuotes = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"

private def parseSingleHeader(
kv: (String, String)
): List[(String, String)] = {
val key = kv._1
val values = kv._2.split(commaNotBetweenQuotes).toList
@tailrec
def loop(
yisraelU marked this conversation as resolved.
Show resolved Hide resolved
rest: List[String],
acc: List[(String, String)]
): List[(String, String)] = {
rest match {
case ::(head, next) => loop(next, (key, head) :: acc)
case Nil => acc
}
}
loop(values, List.empty)
}

}