-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from blast-hardcheese/text-plain
Support for text/plain, as well as a bugfix for root path matching
- Loading branch information
Showing
11 changed files
with
246 additions
and
15 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
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
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
39 changes: 39 additions & 0 deletions
39
modules/sample/src/main/resources/contentType-textPlain.yaml
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,39 @@ | ||
swagger: "2.0" | ||
info: | ||
title: 'Content-Type: text/plain spec' | ||
version: 1.0.0 | ||
host: localhost:1234 | ||
paths: | ||
/foo: | ||
post: | ||
operationId: doFoo | ||
x-scala-package: foo | ||
consumes: | ||
- text/plain | ||
produces: | ||
- text/plain | ||
parameters: | ||
- in: body | ||
name: body | ||
schema: | ||
type: string | ||
responses: | ||
'201': | ||
description: "Created" | ||
/bar: | ||
post: | ||
operationId: doBar | ||
x-scala-package: foo | ||
consumes: | ||
- text/plain | ||
produces: | ||
- text/plain | ||
parameters: | ||
- in: body | ||
name: body | ||
required: true | ||
schema: | ||
type: string | ||
responses: | ||
'201': | ||
description: "Created" |
48 changes: 48 additions & 0 deletions
48
modules/sample/src/test/scala/generators/AkkaHttp/Client/contentType/textPlain.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,48 @@ | ||
package tests.generators.AkkaHttp.Client.contentType | ||
|
||
import _root_.tests.contentTypes.textPlain.Implicits.IgnoredEntity | ||
import _root_.tests.contentTypes.textPlain.foo.FooClient | ||
import akka.http.scaladsl.model._ | ||
import akka.http.scaladsl.server.Directives._ | ||
import akka.http.scaladsl.server.Route | ||
import akka.http.scaladsl.testkit.ScalatestRouteTest | ||
import cats.instances.future._ | ||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.{EitherValues, FunSuite, Matchers} | ||
import scala.concurrent.Future | ||
import scala.concurrent.duration._ | ||
|
||
import _root_.tests.scalatest.EitherTValues | ||
|
||
class TextPlainTest extends FunSuite with Matchers with EitherValues with EitherTValues with ScalaFutures with ScalatestRouteTest { | ||
override implicit val patienceConfig = PatienceConfig(1000.millis, 1000.millis) | ||
test("Plain text should be emitted for optional parameters") { | ||
val route: Route = (path("foo") & extractRequestEntity & entity(as[String])) { (entity, value) => | ||
complete({ | ||
if (entity.contentType == ContentTypes.`text/plain(UTF-8)` && value == "sample") { | ||
StatusCodes.OK | ||
} else { | ||
StatusCodes.BadRequest | ||
} | ||
}) | ||
} | ||
val client: HttpRequest => Future[HttpResponse] = Route.asyncHandler(route) | ||
val fooClient = FooClient.httpClient(client) | ||
new EitherTValuable(fooClient.doFoo(Some("sample"))).rightValue.futureValue shouldBe IgnoredEntity.empty | ||
} | ||
|
||
test("Plain text should be emitted for required parameters") { | ||
val route: Route = (path("bar") & extractRequestEntity & entity(as[String])) { (entity, value) => | ||
complete({ | ||
if (entity.contentType == ContentTypes.`text/plain(UTF-8)` && value == "sample") { | ||
StatusCodes.OK | ||
} else { | ||
StatusCodes.BadRequest | ||
} | ||
}) | ||
} | ||
val client: HttpRequest => Future[HttpResponse] = Route.asyncHandler(route) | ||
val fooClient = FooClient.httpClient(client) | ||
new EitherTValuable(fooClient.doBar("sample")).rightValue.futureValue shouldBe IgnoredEntity.empty | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
modules/sample/src/test/scala/scalatest/EitherTValues.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,28 @@ | ||
package tests.scalatest | ||
|
||
import cats.Functor | ||
import cats.data.EitherT | ||
import org.scalactic.source | ||
import org.scalatest._ | ||
import org.scalatest.exceptions.{StackDepthException, TestFailedException} | ||
import scala.language.implicitConversions | ||
|
||
trait EitherTValues { | ||
|
||
implicit def convertEitherTToValuable[F[_]: Functor, L, R](eitherT: EitherT[F, L, R]) = new EitherTValuable(eitherT) | ||
|
||
class EitherTValuable[F[_]: Functor, L, R](eitherT: EitherT[F, L, R]) { | ||
def leftValue(implicit pos: source.Position): F[L] = { | ||
eitherT.fold(identity, { _ => | ||
throw new TestFailedException((_: StackDepthException) => Option.empty[String], Option.empty[Throwable], pos) | ||
}) | ||
} | ||
|
||
def rightValue(implicit pos: source.Position): F[R] = { | ||
eitherT.fold({ _ => | ||
throw new TestFailedException((_: StackDepthException) => Option.empty[String], Option.empty[Throwable], pos) | ||
}, identity) | ||
} | ||
} | ||
} | ||
|
76 changes: 76 additions & 0 deletions
76
src/test/scala/generators/AkkaHttp/Client/contentType/textPlain.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,76 @@ | ||
package tests.generators.akkaHttp.client.contentType | ||
|
||
import _root_.io.swagger.parser.SwaggerParser | ||
import cats.instances.all._ | ||
import com.twilio.swagger._ | ||
import com.twilio.guardrail.generators.AkkaHttp | ||
import com.twilio.guardrail.{ClassDefinition, Client, Clients, Context, ClientGenerator, ProtocolGenerator, ProtocolDefinitions, RandomType, CodegenApplication, Target} | ||
import org.scalatest.{FunSuite, Matchers} | ||
import scala.meta._ | ||
|
||
class TextPlainTest extends FunSuite with Matchers { | ||
val swagger = s""" | ||
|swagger: "2.0" | ||
|info: | ||
| title: Whatever | ||
| version: 1.0.0 | ||
|host: localhost:1234 | ||
|schemes: | ||
| - http | ||
|paths: | ||
| /foo: | ||
| put: | ||
| operationId: putFoo | ||
| consumes: | ||
| - text/plain | ||
| parameters: | ||
| - name: body | ||
| in: body | ||
| required: true | ||
| schema: | ||
| type: string | ||
| responses: | ||
| 200: | ||
| description: Success | ||
|""".stripMargin | ||
|
||
test("Properly handle all methods") { | ||
val ( | ||
_, | ||
Clients(Client(tags, className, statements) :: _), | ||
_ | ||
) = runSwaggerSpec(swagger)(Context.empty, AkkaHttp) | ||
val List(cmp, cls) = statements.dropWhile(_.isInstanceOf[Import]) | ||
|
||
val companion = q""" | ||
object Client { | ||
def apply(host: String = "http://localhost:1234")(implicit httpClient: HttpRequest => Future[HttpResponse], ec: ExecutionContext, mat: Materializer): Client = new Client(host = host)(httpClient = httpClient, ec = ec, mat = mat) | ||
def httpClient(httpClient: HttpRequest => Future[HttpResponse], host: String = "http://localhost:1234")(implicit ec: ExecutionContext, mat: Materializer): Client = new Client(host = host)(httpClient = httpClient, ec = ec, mat = mat) | ||
} | ||
""" | ||
val client = q""" | ||
class Client(host: String = "http://localhost:1234")(implicit httpClient: HttpRequest => Future[HttpResponse], ec: ExecutionContext, mat: Materializer) { | ||
val basePath: String = "" | ||
private[this] def wrap[T: FromEntityUnmarshaller](resp: Future[HttpResponse]): EitherT[Future, Either[Throwable, HttpResponse], T] = { | ||
EitherT(resp.flatMap(resp => if (resp.status.isSuccess) { | ||
Unmarshal(resp.entity).to[T].map(Right.apply _) | ||
} else { | ||
FastFuture.successful(Left(Right(resp))) | ||
}).recover({ | ||
case e: Throwable => | ||
Left(Left(e)) | ||
})) | ||
} | ||
def putFoo(body: String, headers: scala.collection.immutable.Seq[HttpHeader] = Nil): EitherT[Future, Either[Throwable, HttpResponse], IgnoredEntity] = { | ||
val allHeaders = headers ++ scala.collection.immutable.Seq[Option[HttpHeader]]().flatten | ||
wrap[IgnoredEntity](Marshal(TextPlain(body)).to[RequestEntity].flatMap { | ||
entity => httpClient(HttpRequest(method = HttpMethods.PUT, uri = host + basePath + "/foo", entity = entity, headers = allHeaders)) | ||
}) | ||
} | ||
} | ||
""" | ||
|
||
cmp.structure should equal(companion.structure) | ||
cls.structure should equal(client.structure) | ||
} | ||
} |
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