-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #246 from JackTreble/main
add Http4sTestHttpRoutesSuite & Http4sTestAuthedRoutesSuite
- Loading branch information
Showing
10 changed files
with
399 additions
and
14 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
110 changes: 110 additions & 0 deletions
110
modules/http4s-munit/src/main/scala/munit/Http4sTestAuthedRoutesSuite.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,110 @@ | ||
/* | ||
* Copyright 2020-2022 Alejandro Hernández <https://github.com/alejandrohdezma> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 munit | ||
|
||
import cats.Show | ||
import cats.effect.IO | ||
import cats.effect.Resource | ||
import cats.effect.SyncIO | ||
import org.http4s.AuthedRequest | ||
import org.http4s.AuthedRoutes | ||
import org.http4s.ContextRequest | ||
import org.http4s.Request | ||
import org.http4s.Response | ||
|
||
/** Base class for suites testing per-test `AuthedRoutes`. * Ensure that a `Show` instance for the request's context | ||
* type is in scope. This instance will be used to include the context's information in the test's name. | ||
* | ||
* @example | ||
* {{{ | ||
* import cats.effect.IO | ||
* | ||
* import org.http4s.AuthedRoutes | ||
* | ||
* class MyAuthedRoutesSuite extends munit.Http4sTestAuthedRoutesSuite[String] { | ||
* | ||
* test(routes = AuthedRoutes.of { case GET -> Root / "hello" as user => | ||
* Ok(user + " says Hi") | ||
* }).as("Jose")) { response => | ||
* assertIO(response.as[String], "Jose says Hi") | ||
* } | ||
* | ||
* } | ||
* }}} | ||
*/ | ||
abstract class Http4sTestAuthedRoutesSuite[A: Show] extends Http4sSuite[AuthedRequest[IO, A]] { | ||
|
||
/** @inheritdoc */ | ||
override def http4sMUnitNameCreator( | ||
request: AuthedRequest[IO, A], | ||
followingRequests: List[String], | ||
testOptions: TestOptions, | ||
config: Http4sMUnitConfig | ||
): String = Http4sMUnitDefaults.http4sMUnitNameCreator( | ||
request, | ||
followingRequests, | ||
testOptions, | ||
config, | ||
http4sMUnitNameCreatorReplacements() | ||
) | ||
|
||
implicit class Request2AuthedRequest(request: Request[IO]) { | ||
|
||
/** Converts an `IO[Request[IO]]` into an `IO[AuthedRequest[IO, A]]` by providing the `A` context. */ | ||
def context(context: A): AuthedRequest[IO, A] = AuthedRequest(context, request) | ||
|
||
/** Converts an `IO[Request[IO]]` into an `IO[AuthedRequest[IO, A]]` by providing the `A` context. */ | ||
def ->(a: A): AuthedRequest[IO, A] = context(a) | ||
|
||
} | ||
|
||
def http4sMUnitFunFixture( | ||
routes: AuthedRoutes[A, IO] | ||
): SyncIO[FunFixture[ContextRequest[IO, A] => Resource[IO, Response[IO]]]] = | ||
SyncIO.pure(FunFixture(_ => routes.orNotFound.run(_).to[Resource[IO, *]], _ => ())) | ||
|
||
/** Declares a test for the provided routes and request. | ||
* | ||
* @example | ||
* {{{ | ||
* test(routes = AuthedRoutes.of { case GET -> Root / "users" / number => | ||
* Ok(number) | ||
* }(GET(uri"users" / 42)) { response => | ||
* // test body | ||
* } | ||
* }}} | ||
* @example | ||
* {{{ | ||
* test(routes = AuthedRoutes.of { case req @ POST -> Root / "users" => | ||
* Ok(req.as[String]) | ||
* }(POST(json, uri"users")).alias("Create a new user") { response => | ||
* // test body | ||
* } | ||
* }}} | ||
* @example | ||
* {{{ | ||
* test(routes = AuthedRoutes.of { case GET -> Root / "users" / number => | ||
* Ok(number) | ||
* }(GET(uri"users" / 42)).flaky { response => | ||
* // test body | ||
* } | ||
* }}} | ||
*/ | ||
def test(routes: AuthedRoutes[A, IO])(request: AuthedRequest[IO, A]): Http4sMUnitTestCreator = | ||
Http4sMUnitTestCreator(request, http4sMUnitFunFixture(routes)) | ||
|
||
} |
103 changes: 103 additions & 0 deletions
103
modules/http4s-munit/src/main/scala/munit/Http4sTestHttpRoutesSuite.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,103 @@ | ||
/* | ||
* Copyright 2020-2022 Alejandro Hernández <https://github.com/alejandrohdezma> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 munit | ||
|
||
import cats.effect.IO | ||
import cats.effect.Resource | ||
import cats.effect.SyncIO | ||
|
||
import org.http4s.ContextRequest | ||
import org.http4s.HttpRoutes | ||
import org.http4s.Request | ||
import org.http4s.Response | ||
|
||
/** Base class for suites testing per-test `HttpRoutes`. | ||
* | ||
* @example | ||
* {{{ | ||
* import cats.effect.IO | ||
* | ||
* import org.http4s.HttpRoutes | ||
* | ||
* class MyHttpRoutesSuite extends munit.Http4sTestHttpRoutesSuite { | ||
* | ||
* test(routes = HttpRoutes.of { case GET -> Root / "hello" => | ||
* Ok("Hello!") | ||
* }(GET(uri"hello")) { response => | ||
* assertIO(response.as[String], "Hello!") | ||
* } | ||
* | ||
* } | ||
* }}} | ||
* @author | ||
* Jack Treble | ||
*/ | ||
abstract class Http4sTestHttpRoutesSuite extends Http4sSuite[Request[IO]] { | ||
|
||
/** @inheritdoc */ | ||
override def http4sMUnitNameCreator( | ||
request: Request[IO], | ||
followingRequests: List[String], | ||
testOptions: TestOptions, | ||
config: Http4sMUnitConfig | ||
): String = | ||
Http4sMUnitDefaults.http4sMUnitNameCreator( | ||
ContextRequest((), request), | ||
followingRequests, | ||
testOptions, | ||
config, | ||
http4sMUnitNameCreatorReplacements() | ||
) | ||
|
||
def http4sMUnitFunFixture(routes: HttpRoutes[IO]): SyncIO[FunFixture[Request[IO] => Resource[IO, Response[IO]]]] = | ||
SyncIO.pure(FunFixture(_ => req => routes.orNotFound.run(req).to[Resource[IO, *]], _ => ())) | ||
|
||
/** Declares a test for the provided routes and request. | ||
* | ||
* @example | ||
* {{{ | ||
* test(routes = HttpRoutes.of { case GET -> Root / "users" / number => | ||
* Ok(number) | ||
* }(GET(uri"users" / 42)) { response => | ||
* // test body | ||
* } | ||
* }}} | ||
* @example | ||
* {{{ | ||
* test(routes = HttpRoutes.of { case req @ POST -> Root / "users" => | ||
* Ok(req.as[String]) | ||
* }(POST(json, uri"users")).alias("Create a new user") { response => | ||
* // test body | ||
* } | ||
* }}} | ||
* @example | ||
* {{{ | ||
* test(routes = HttpRoutes.of { case GET -> Root / "users" / number => | ||
* Ok(number) | ||
* }(GET(uri"users" / 42)).flaky { response => | ||
* // test body | ||
* } | ||
* }}} | ||
*/ | ||
def test(routes: HttpRoutes[IO])(request: Request[IO]): Http4sMUnitTestCreator = { | ||
Http4sMUnitTestCreator( | ||
request, | ||
http4sMUnitFunFixture(routes) | ||
) | ||
} | ||
|
||
} |
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
35 changes: 35 additions & 0 deletions
35
modules/http4s-munit/src/test/scala/munit/Http4sTestAuthedRoutesSuiteSuite.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,35 @@ | ||
/* | ||
* Copyright 2020-2022 Alejandro Hernández <https://github.com/alejandrohdezma> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 munit | ||
|
||
import org.http4s.AuthedRoutes | ||
|
||
class Http4sTestAuthedRoutesSuiteSuite extends Http4sTestAuthedRoutesSuite[String] { | ||
|
||
test(routes = AuthedRoutes.of { case GET -> Root / "hello" as user => | ||
Ok(s"$user: Hi") | ||
})(GET(uri"/hello") -> "jose").alias("Test 1") { response => | ||
assertIO(response.as[String], "jose: Hi") | ||
} | ||
|
||
test(routes = AuthedRoutes.of { case GET -> Root / "hello" / name as user => | ||
Ok(s"$user: Hi $name") | ||
})(GET(uri"/hello" / "Jose").context("alex")).alias("Test 2") { response => | ||
assertIO(response.as[String], "alex: Hi Jose") | ||
} | ||
|
||
} |
Oops, something went wrong.