Skip to content

Commit

Permalink
API-7598: tidying and moving connector tests to use ExternalWireMockS…
Browse files Browse the repository at this point in the history
…upport
  • Loading branch information
worthydolt committed Jun 28, 2024
1 parent fe5ace3 commit 4eca7af
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import uk.gov.hmrc.apiplatforminboundsoap.models.{SendFail, SendResult, SendSucc
class InboundConnector @Inject() (httpClientV2: HttpClientV2, appConfig: AppConfig)(implicit ec: ExecutionContext) extends Logging {

def postMessage(soapRequest: SoapRequest, headers: Seq[(String, String)])(implicit hc: HeaderCarrier): Future[SendResult] = {
// implicit val hc: HeaderCarrier = HeaderCarrier().withExtraHeaders(headers.headers: _*)

postHttpRequest(soapRequest, headers).map {
case Left(UpstreamErrorResponse(_, statusCode, _, _)) =>
logger.warn(s"Sending message failed with status code $statusCode")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.Helpers._
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.http.test.WireMockSupport
import uk.gov.hmrc.http.test.ExternalWireMockSupport

import uk.gov.hmrc.apiplatforminboundsoap.models.{SendFail, SendResult, SendSuccess, SoapRequest}
import uk.gov.hmrc.apiplatforminboundsoap.support.ExternalServiceStub

class InboundConnectorISpec extends AnyWordSpec with Matchers with GuiceOneAppPerSuite with WireMockSupport with ExternalServiceStub {
class InboundConnectorISpec extends AnyWordSpec with Matchers with GuiceOneAppPerSuite with ExternalWireMockSupport with ExternalServiceStub {
override implicit lazy val app: Application = appBuilder.build()
implicit val hc: HeaderCarrier = HeaderCarrier()

Expand All @@ -42,12 +42,12 @@ class InboundConnectorISpec extends AnyWordSpec with Matchers with GuiceOneAppPe
)

trait Setup {
val headers: Seq[(String, String)] = List("key" -> "value")
val headers: Seq[(String, String)] = List("Authorization" -> "Bearer value")
val underTest: InboundConnector = app.injector.instanceOf[InboundConnector]
}

"postMessage" should {
val message = SoapRequest("<Envelope><Body>foobar</Body></Envelope>", wireMockUrl)
val message = SoapRequest("<Envelope><Body>foobar</Body></Envelope>", externalWireMockUrl)

"return successful statuses returned by the internal service" in new Setup {
val expectedStatus: Int = OK
Expand Down Expand Up @@ -87,6 +87,7 @@ class InboundConnectorISpec extends AnyWordSpec with Matchers with GuiceOneAppPe
await(underTest.postMessage(message, headers))

verifyRequestBody(message.soapEnvelope)
verifyHeader(headers.head._1, headers.head._2)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,20 @@ import play.api.libs.json.Json
import play.api.mvc.Headers
import play.api.test.FakeRequest
import play.api.test.Helpers.{defaultAwaitTimeout, status}
import uk.gov.hmrc.http.test.{HttpClientV2Support, WireMockSupport}
import uk.gov.hmrc.http.test.{ExternalWireMockSupport, HttpClientV2Support}

import uk.gov.hmrc.apiplatforminboundsoap.support.ExternalServiceStub

class PassThroughControllerISpec extends AnyWordSpecLike with Matchers with HttpClientV2Support with WireMockSupport with GuiceOneAppPerSuite with ExternalServiceStub {
override implicit lazy val app: Application = appBuilder.build()
implicit val mat: Materializer = app.injector.instanceOf[Materializer]
class PassThroughControllerISpec extends AnyWordSpecLike with Matchers with HttpClientV2Support with ExternalWireMockSupport with GuiceOneAppPerSuite with ExternalServiceStub {

protected def appBuilder: GuiceApplicationBuilder =
new GuiceApplicationBuilder()
.configure(
"metrics.enabled" -> false,
"auditing.enabled" -> false,
"forwardMessageHost" -> wireMockHost,
"forwardMessagePort" -> wireMockPort
)
override implicit lazy val app: Application = new GuiceApplicationBuilder()
.configure(
"metrics.enabled" -> false,
"auditing.enabled" -> false,
"forwardMessageHost" -> externalWireMockHost,
"forwardMessagePort" -> externalWireMockPort
).build()
implicit val mat: Materializer = app.injector.instanceOf[Materializer]

val path = "/ics2/NESReferralBASV2"
val fakeRequest = FakeRequest("POST", path)
Expand All @@ -67,6 +65,7 @@ class PassThroughControllerISpec extends AnyWordSpecLike with Matchers with Http
status(result) shouldBe expectedStatus

verifyRequestBody(payload.toString(), path)
expectedHeaders.headers.foreach(h => verifyHeader(h._1, h._2, path = path))
}

"forward Authorization header" in {
Expand Down Expand Up @@ -104,6 +103,7 @@ class PassThroughControllerISpec extends AnyWordSpecLike with Matchers with Http

val result = underTest.message(pathWithoutLeadingStroke)(fakeRequest.withXmlBody(payload).withHeaders(expectedHeaders))
status(result) shouldBe expectedStatus
expectedHeaders.headers.foreach(h => verifyHeader(h._1, h._2, path = path))
}

"forward an XML message to the right path" in {
Expand Down Expand Up @@ -133,6 +133,7 @@ class PassThroughControllerISpec extends AnyWordSpecLike with Matchers with Http

val result = underTest.message(path)(fakeRequest.withXmlBody(payload).withHeaders(expectedHeaders))
status(result) shouldBe expectedStatus
expectedHeaders.headers.foreach(h => verifyHeader(h._1, h._2, path = path))
}
}

Expand All @@ -157,8 +158,8 @@ class PassThroughControllerISpec extends AnyWordSpecLike with Matchers with Http
status(result) shouldBe expectedStatus
}

"handle an error response from the forward-to service " in {
val expectedStatus = 500
"handle an server error response from the forward-to service" in {
val expectedStatus = Status.INTERNAL_SERVER_ERROR
val expectedHeaders = Headers("Authorization" -> "Bearer blah")
primeStubForFault("Something went wrong", Fault.CONNECTION_RESET_BY_PEER, path)

Expand All @@ -167,5 +168,16 @@ class PassThroughControllerISpec extends AnyWordSpecLike with Matchers with Http

status(result) shouldBe expectedStatus
}

"handle an unsuccessful response from the forward-to service" in {
val expectedStatus = Status.UNAUTHORIZED
val expectedHeaders = Headers("Authorization" -> "Bearer blah")
primeStubForSuccess("Unauthorized", Status.UNAUTHORIZED, path)

val payload: Elem = XML.load(Source.fromResource("ie4r02-v2.xml").bufferedReader())
val result = underTest.message(path)(fakeRequest.withXmlBody(payload).withHeaders(expectedHeaders))

status(result) shouldBe expectedStatus
}
}
}

This file was deleted.

0 comments on commit 4eca7af

Please sign in to comment.