Skip to content

Commit

Permalink
Merge pull request #96 from hmrc/BDOG-780c
Browse files Browse the repository at this point in the history
Bdog 780c
  • Loading branch information
colin-lamed authored May 6, 2020
2 parents ebcedcd + 8aa39bf commit 6361334
Show file tree
Hide file tree
Showing 19 changed files with 362 additions and 535 deletions.
21 changes: 9 additions & 12 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ lazy val library = (project in file("."))
)
.disablePlugins(sbt.plugins.JUnitXmlReportPlugin)

lazy val httpVerbsCommon = Project("http-verbs-common", file("http-verbs-common"))
.disablePlugins(SbtGitVersioning)

lazy val httpVerbsPlay25 = Project("http-verbs-play-25", file("http-verbs-play-25"))
.enablePlugins(SbtAutoBuildPlugin, SbtArtifactory)
.settings(
commonSettings,
unmanagedSourceDirectories in Compile += (httpVerbsCommon / Compile / scalaSource).value,
unmanagedSourceDirectories in Test += (httpVerbsCommon / Test / scalaSource).value,
Compile / unmanagedSourceDirectories += baseDirectory.value / "../http-verbs-common/src/main/scala",
Test / unmanagedSourceDirectories += baseDirectory.value / "../http-verbs-common/src/test/scala",
crossScalaVersions := Seq(scala2_11),
libraryDependencies ++= AppDependencies.compileCommon ++ AppDependencies.compilePlay25 ++ AppDependencies.testCommon ++ AppDependencies.testPlay25,
Test / fork := true // akka is not unloaded properly, which can affect other tests
Expand All @@ -55,8 +52,8 @@ lazy val httpVerbsPlay26 = Project("http-verbs-play-26", file("http-verbs-play-2
commonSettings,
crossScalaVersions := Seq(scala2_11, scala2_12),
libraryDependencies ++= AppDependencies.compileCommon ++ AppDependencies.compilePlay26 ++ AppDependencies.testCommon ++ AppDependencies.testPlay26,
unmanagedSourceDirectories in Compile += (httpVerbsCommon / Compile / scalaSource).value,
unmanagedSourceDirectories in Test += (httpVerbsCommon / Test / scalaSource).value,
Compile / unmanagedSourceDirectories += baseDirectory.value / "../http-verbs-common/src/main/scala",
Test / unmanagedSourceDirectories += baseDirectory.value / "../http-verbs-common/src/test/scala",
Test / fork := true // akka is not unloaded properly, which can affect other tests
)

Expand All @@ -66,9 +63,9 @@ lazy val httpVerbsPlay27 = Project("http-verbs-play-27", file("http-verbs-play-2
commonSettings,
crossScalaVersions := Seq(scala2_11, scala2_12),
libraryDependencies ++= AppDependencies.compileCommon ++ AppDependencies.compilePlay27 ++ AppDependencies.testCommon ++ AppDependencies.testPlay27,
unmanagedSourceDirectories in Compile += (httpVerbsCommon / Compile / scalaSource).value,
unmanagedSourceDirectories in Test += (httpVerbsCommon / Test / scalaSource).value,
scalaSource in Compile := (httpVerbsPlay26 / Compile / scalaSource).value,
scalaSource in Test := (httpVerbsPlay26 / Test / scalaSource).value,
Test / fork := true // akka is not unloaded properly, which can affect other tests
Compile / unmanagedSourceDirectories += baseDirectory.value / "../http-verbs-common/src/main/scala",
Test / unmanagedSourceDirectories += baseDirectory.value / "../http-verbs-common/src/test/scala",
Compile / scalaSource := (httpVerbsPlay26 / Compile / scalaSource).value,
Test / scalaSource := (httpVerbsPlay26 / Test / scalaSource).value,
Test / fork := true // akka is not unloaded properly, which can affect other tests
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

package uk.gov.hmrc.play.http.ws

import play.api.libs.json.JsValue
import play.api.libs.ws.WSResponse
import uk.gov.hmrc.http.HttpResponse

class WSHttpResponse(wsResponse: WSResponse) extends HttpResponse {
override def allHeaders: Map[String, Seq[String]] = wsResponse.allHeaders

override def status = wsResponse.status
override def status: Int = wsResponse.status

override def json = wsResponse.json
override def json: JsValue = wsResponse.json

override def body = wsResponse.body
override def body: String = wsResponse.body
}

trait WSHttp extends WSGet with WSPut with WSPost with WSDelete with WSPatch
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2020 HM Revenue & Customs
*
* 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 uk.gov.hmrc.play.http.ws

import play.api.libs.ws.{WSClient, WSRequest => PlayWSRequest}
import uk.gov.hmrc.http.{HeaderCarrier, Request}

trait WSRequestBuilder extends Request {

def wsClient: WSClient

def buildRequest[A](url: String, headers: Seq[(String, String)])(implicit hc: HeaderCarrier): PlayWSRequest
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
* limitations under the License.
*/

package uk.gov.hmrc.play.http.ws
package uk.gov.hmrc.play.http.ws.default

import uk.gov.hmrc.http._
import uk.gov.hmrc.http.{CoreDelete, DeleteHttpTransport, HeaderCarrier, HttpResponse}
import uk.gov.hmrc.play.http.ws.{WSExecute, WSHttpResponse, WSRequestBuilder}

import scala.concurrent.{ExecutionContext, Future}

trait WSDelete extends CoreDelete with DeleteHttpTransport with WSRequest with WSExecute {
trait WSDelete extends CoreDelete with DeleteHttpTransport with WSRequestBuilder with WSExecute {

override def doDelete(
url: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
* limitations under the License.
*/

package uk.gov.hmrc.play.http.ws
package uk.gov.hmrc.play.http.ws.default

import uk.gov.hmrc.http._
import uk.gov.hmrc.http.{CoreGet, GetHttpTransport, HeaderCarrier, HttpResponse}
import uk.gov.hmrc.play.http.ws.{WSExecute, WSHttpResponse, WSRequestBuilder}

import scala.concurrent.{ExecutionContext, Future}

trait WSGet extends CoreGet with GetHttpTransport with WSRequest with WSExecute {
trait WSGet extends CoreGet with GetHttpTransport with WSRequestBuilder with WSExecute {

override def doGet(
url: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* limitations under the License.
*/

package uk.gov.hmrc.play.http.ws
package uk.gov.hmrc.play.http.ws.default

import play.api.libs.json.{Json, Writes}
import uk.gov.hmrc.http._
import uk.gov.hmrc.http.{CorePatch, HeaderCarrier, HttpResponse, PatchHttpTransport}
import uk.gov.hmrc.play.http.ws.{WSExecute, WSHttpResponse, WSRequestBuilder}

import scala.concurrent.{ExecutionContext, Future}

trait WSPatch extends CorePatch with PatchHttpTransport with WSRequest with WSExecute {
trait WSPatch extends CorePatch with PatchHttpTransport with WSRequestBuilder with WSExecute {

override def doPatch[A](
url: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,60 @@
* limitations under the License.
*/

package uk.gov.hmrc.play.http.ws
package uk.gov.hmrc.play.http.ws.default

import play.api.libs.json.{Json, Writes}
import play.api.mvc.Results
import play.api.libs.ws.WSRequest
import uk.gov.hmrc.http.{CorePost, HeaderCarrier, HttpResponse, PostHttpTransport}
import uk.gov.hmrc.play.http.ws.{WSExecute, WSHttpResponse, WSRequestBuilder}

import scala.concurrent.{ExecutionContext, Future}

trait WSPost extends CorePost with PostHttpTransport with WSRequestBuilder with WSExecute {

trait WSPost extends CorePost with PostHttpTransport with WSRequest with WSExecute {
def withEmptyBody(request: WSRequest): WSRequest

override def doPost[A](
url: String,
body: A,
headers: Seq[(String, String)])(
implicit rds: Writes[A],
hc: HeaderCarrier,
ec: ExecutionContext): Future[HttpResponse] =
headers: Seq[(String, String)]
)(
implicit rds: Writes[A],
hc: HeaderCarrier,
ec: ExecutionContext
): Future[HttpResponse] =
execute(buildRequest(url, headers).withBody(Json.toJson(body)), "POST")
.map(new WSHttpResponse(_))

override def doFormPost(
url: String,
body: Map[String, Seq[String]],
headers: Seq[(String, String)])(
implicit hc: HeaderCarrier,
ec: ExecutionContext): Future[HttpResponse] =
headers: Seq[(String, String)]
)(
implicit hc: HeaderCarrier,
ec: ExecutionContext
): Future[HttpResponse] =
execute(buildRequest(url, headers).withBody(body), "POST")
.map(new WSHttpResponse(_))

override def doPostString(
url: String,
body: String,
headers: Seq[(String, String)])(
implicit hc: HeaderCarrier,
ec: ExecutionContext): Future[HttpResponse] =
headers: Seq[(String, String)]
)(
implicit hc: HeaderCarrier,
ec: ExecutionContext
): Future[HttpResponse] =
execute(buildRequest(url, headers).withBody(body), "POST")
.map(new WSHttpResponse(_))

override def doEmptyPost[A](
url: String,
headers: Seq[(String, String)])(
implicit hc: HeaderCarrier,
ec: ExecutionContext): Future[HttpResponse] = {
import play.api.http.Writeable._
execute(buildRequest(url, headers).withBody(Results.EmptyContent()), "POST")
headers: Seq[(String, String)]
)(
implicit hc: HeaderCarrier,
ec: ExecutionContext
): Future[HttpResponse] =
execute(withEmptyBody(buildRequest(url, headers)), "POST")
.map(new WSHttpResponse(_))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* limitations under the License.
*/

package uk.gov.hmrc.play.http.ws
package uk.gov.hmrc.play.http.ws.default

import play.api.libs.json.{Json, Writes}
import uk.gov.hmrc.http.{CorePut, HeaderCarrier, HttpResponse, PutHttpTransport}
import uk.gov.hmrc.play.http.ws.{WSExecute, WSHttpResponse, WSRequestBuilder}

import scala.concurrent.{ExecutionContext, Future}

trait WSPut extends CorePut with PutHttpTransport with WSRequest with WSExecute {
trait WSPut extends CorePut with PutHttpTransport with WSRequestBuilder with WSExecute{

override def doPut[A](
url: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,11 @@ class RetriesSpec extends AnyWordSpecLike with Matchers with MockitoSugar with S
}

trait TestHttpVerb extends HttpVerb with Retries with HttpHooks with SucceedNthCall {
System.setProperty("akka.jvm-shutdown-hooks", "off")
protected def configuration: Option[Config] =
Some(Configuration("http-verbs.retries.ssl-engine-closed-already.enabled" -> true).underlying)
Some(Configuration(
"http-verbs.retries.ssl-engine-closed-already.enabled" -> true
).underlying)
override val hooks: Seq[HttpHook] = Nil
override private[http] lazy val intervals: Seq[FiniteDuration] = List.fill(3)(1.millis)
override def actorSystem: ActorSystem = ActorSystem("test-actor-system")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package uk.gov.hmrc.play.http.ws

import play.api.libs.ws
import play.api.libs.ws.{DefaultWSProxyServer, WSClient, WSProxyServer}
import play.api.libs.ws.{DefaultWSProxyServer, WSClient, WSProxyServer, WSRequest => PlayWSRequest }
import play.api.{Configuration, Play}
import uk.gov.hmrc.http.{HeaderCarrier, Request}
import uk.gov.hmrc.http.HeaderCarrier

trait WSRequest extends Request {
trait WSRequest extends WSRequestBuilder {

import play.api.libs.ws.WS

Expand All @@ -30,7 +29,7 @@ trait WSRequest extends Request {
WS.client
}

def buildRequest[A](url: String, headers: Seq[(String, String)] = Seq.empty)(implicit hc: HeaderCarrier): ws.WSRequest =
def buildRequest[A](url: String, headers: Seq[(String, String)] = Seq.empty)(implicit hc: HeaderCarrier): PlayWSRequest =
wsClient.url(url)
.withHeaders(applicableHeaders(url)(hc): _*)
.withHeaders(headers: _*)
Expand All @@ -41,7 +40,7 @@ trait WSProxy extends WSRequest {

def wsProxyServer: Option[WSProxyServer]

override def buildRequest[A](url: String, headers: Seq[(String, String)])(implicit hc: HeaderCarrier): ws.WSRequest =
override def buildRequest[A](url: String, headers: Seq[(String, String)])(implicit hc: HeaderCarrier): PlayWSRequest =
wsProxyServer match {
case Some(proxy) => super.buildRequest(url, headers).withProxyServer(proxy)
case None => super.buildRequest(url, headers)
Expand Down Expand Up @@ -75,5 +74,4 @@ object WSProxyConfiguration {

case class ProxyConfigurationException(key: String)
extends RuntimeException(s"Missing proxy configuration - key '$key' not found")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2020 HM Revenue & Customs
*
* 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 uk.gov.hmrc.play.http.ws

import play.api.libs.ws.{WSRequest => PlayWSRequest}
import play.api.mvc.Results

trait WSDelete extends default.WSDelete with WSRequest
trait WSGet extends default.WSGet with WSRequest
trait WSPatch extends default.WSPatch with WSRequest
trait WSPut extends default.WSPut with WSRequest
trait WSPost extends default.WSPost with WSRequest {
override def withEmptyBody(request: PlayWSRequest): PlayWSRequest =
request.withBody(Results.EmptyContent())
}

trait WSHttp extends WSGet with WSPut with WSPost with WSDelete with WSPatch
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import com.github.tomakehurst.wiremock.client.WireMock._
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig
import com.typesafe.config.Config
import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.must.Matchers
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{JsValue, Json}
import play.api.libs.ws.WSClient
Expand Down
Loading

0 comments on commit 6361334

Please sign in to comment.