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 #2388 from daddykotex/dfrancoeur/2363
fix: do not follow redirect when checking urls
- Loading branch information
Showing
8 changed files
with
163 additions
and
10 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
44 changes: 44 additions & 0 deletions
44
modules/core/src/main/scala/org/scalasteward/core/client/ClientConfiguration.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,44 @@ | ||
/* | ||
* Copyright 2018-2021 Scala Steward contributors | ||
* | ||
* 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 org.scalasteward.core.client | ||
|
||
import org.http4s.client._ | ||
import org.http4s.headers.`User-Agent` | ||
import cats.effect._ | ||
import org.http4s.okhttp.client.OkHttpBuilder | ||
|
||
object ClientConfiguration { | ||
type BuilderMiddleware[F[_]] = OkHttpBuilder[F] => OkHttpBuilder[F] | ||
object BuilderMiddleware { | ||
def default[F[_]]: BuilderMiddleware[F] = bmw => bmw | ||
} | ||
def build[F[_]: Async](bmw: BuilderMiddleware[F], cmw: Middleware[F]): Resource[F, Client[F]] = | ||
OkHttpBuilder | ||
.withDefaultClient[F] | ||
.map(bmw) | ||
.flatMap(_.resource) | ||
.map(cmw) | ||
|
||
def setUserAgent[F[_]: MonadCancelThrow](userAgent: `User-Agent`): Middleware[F] = { client => | ||
Client[F](req => client.run(req.putHeaders(userAgent))) | ||
} | ||
|
||
def disableFollowRedirect[F[_]](builder: OkHttpBuilder[F]): OkHttpBuilder[F] = | ||
builder.withOkHttpClient( | ||
builder.okHttpClient.newBuilder().followRedirects(false).build() | ||
) | ||
} |
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
86 changes: 86 additions & 0 deletions
86
modules/core/src/test/scala/org/scalasteward/core/client/ClientConfigurationTest.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,86 @@ | ||
package org.scalasteward.core.client | ||
|
||
import org.http4s.client._ | ||
import org.http4s.headers.`User-Agent` | ||
import cats.effect._ | ||
import cats.implicits._ | ||
import org.http4s.implicits._ | ||
import org.typelevel.ci._ | ||
import munit.CatsEffectSuite | ||
import org.http4s.HttpRoutes | ||
import org.http4s.headers.Location | ||
|
||
class ClientConfigurationTest extends CatsEffectSuite { | ||
private val userAgentValue = "my-user-agent" | ||
private val dummyUserAgent = | ||
`User-Agent`.parse(userAgentValue).getOrElse(fail("unable to create user agent")) | ||
|
||
private val routes: HttpRoutes[IO] = { | ||
import org.http4s.dsl.io._ | ||
HttpRoutes.of[IO] { | ||
case req @ GET -> Root / "user-agent" => | ||
req.headers.get(ci"user-agent") match { | ||
case Some(value) => | ||
if (value.head.value == userAgentValue) Ok("success") | ||
else | ||
IO.raiseError( | ||
new RuntimeException(s"expected $userAgentValue but got ${value.head.value}") | ||
) | ||
case None => | ||
BadRequest("No user-agent") | ||
} | ||
|
||
case GET -> Root / "redirect" => | ||
Found(Location(uri"/redirected")) | ||
|
||
case GET -> Root / "redirected" => | ||
BadRequest("Got redirected") | ||
} | ||
} | ||
|
||
test("setUserAgent add a specific user agent to requests") { | ||
import org.http4s.client.dsl.io._ | ||
import org.http4s.Method._ | ||
|
||
val initialClient = Client.fromHttpApp[IO](routes.orNotFound) | ||
val setUserAgent = ClientConfiguration.setUserAgent[IO](dummyUserAgent) | ||
val newClient = setUserAgent(initialClient) | ||
|
||
newClient | ||
.run(GET(uri"/user-agent")) | ||
.use(r => r.status.code.pure[IO]) | ||
.assertEquals(200) | ||
|
||
initialClient | ||
.run(GET(uri"/user-agent")) | ||
.use(r => r.status.code.pure[IO]) | ||
.assertEquals(400) | ||
} | ||
|
||
test("disableFollowRedirect does not follow redirect") { | ||
import org.http4s.blaze.server._ | ||
import org.http4s.client.dsl.io._ | ||
import org.http4s.Method._ | ||
|
||
val regularClient = ClientConfiguration.build[IO]( | ||
ClientConfiguration.BuilderMiddleware.default, | ||
ClientConfiguration.setUserAgent(dummyUserAgent) | ||
) | ||
val disabledClient = ClientConfiguration.build[IO]( | ||
ClientConfiguration.disableFollowRedirect, | ||
ClientConfiguration.setUserAgent(dummyUserAgent) | ||
) | ||
val getServer = | ||
BlazeServerBuilder[IO].bindAny("localhost").withHttpApp(routes.orNotFound).resource | ||
|
||
val test = (regularClient, disabledClient, getServer).tupled.use { | ||
case (regClient, disClient, s) => | ||
val fullUri = s.baseUri / "redirect" | ||
( | ||
regClient.run(GET(fullUri)).use(_.status.code.pure[IO]), | ||
disClient.run(GET(fullUri)).use(_.status.code.pure[IO]) | ||
).tupled | ||
} | ||
test.assertEquals((400, 302)) | ||
} | ||
} |
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