From 7e701fb67bf815083518cdac6b585933a939ba31 Mon Sep 17 00:00:00 2001 From: Nick Choudhry <28593658+NC-1234@users.noreply.github.com> Date: Mon, 3 Aug 2020 14:33:08 +0100 Subject: [PATCH] APIS-4826: Allow local urls are valid --- app/uk/gov/hmrc/apisubscriptionfields/model/Model.scala | 3 ++- test/uk/gov/hmrc/apisubscriptionfields/model/ModelSpec.scala | 4 ++++ .../apisubscriptionfields/model/ValidationRuleTestData.scala | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/uk/gov/hmrc/apisubscriptionfields/model/Model.scala b/app/uk/gov/hmrc/apisubscriptionfields/model/Model.scala index 1e3d7c3..af35b50 100644 --- a/app/uk/gov/hmrc/apisubscriptionfields/model/Model.scala +++ b/app/uk/gov/hmrc/apisubscriptionfields/model/Model.scala @@ -18,6 +18,7 @@ package uk.gov.hmrc.apisubscriptionfields.model import cats.data.{NonEmptyList => NEL} import uk.gov.hmrc.apisubscriptionfields.model.FieldDefinitionType.FieldDefinitionType +import org.apache.commons.validator.routines.UrlValidator import eu.timepit.refined._ import Types._ @@ -37,7 +38,7 @@ case class RegexValidationRule(regex: RegexExpr) extends ValidationRule { case object UrlValidationRule extends ValidationRule { def validateAgainstRule(value: FieldValue): Boolean = { val schemes = Array("http","https") - val urlValidator = new org.apache.commons.validator.routines.UrlValidator(schemes) + val urlValidator = new org.apache.commons.validator.routines.UrlValidator(schemes, UrlValidator.ALLOW_LOCAL_URLS) urlValidator.isValid(value) } } diff --git a/test/uk/gov/hmrc/apisubscriptionfields/model/ModelSpec.scala b/test/uk/gov/hmrc/apisubscriptionfields/model/ModelSpec.scala index d90b3fd..1c6d5a3 100644 --- a/test/uk/gov/hmrc/apisubscriptionfields/model/ModelSpec.scala +++ b/test/uk/gov/hmrc/apisubscriptionfields/model/ModelSpec.scala @@ -47,6 +47,10 @@ class UrlValidationRuleSpec extends FunSpec with ValidationRuleTestData with Mat UrlValidationRule.validate(validUrl) shouldBe true } + describe("pass for localhost") { + UrlValidationRule.validate(localValidUrl) shouldBe true + } + describe("return true when the value is blank") { UrlValidationRule.validate("") shouldBe true } diff --git a/test/uk/gov/hmrc/apisubscriptionfields/model/ValidationRuleTestData.scala b/test/uk/gov/hmrc/apisubscriptionfields/model/ValidationRuleTestData.scala index 395b835..ca36abd 100644 --- a/test/uk/gov/hmrc/apisubscriptionfields/model/ValidationRuleTestData.scala +++ b/test/uk/gov/hmrc/apisubscriptionfields/model/ValidationRuleTestData.scala @@ -29,6 +29,7 @@ trait ValidationRuleTestData { val atLeastTenLongRule: ValidationRule = RegexValidationRule("""^.{10}.*$""") val validUrl = "https://www.example.com/here/and/there" + val localValidUrl = "https://localhost:9000/" val invalidUrls = List("www.example.com", "ftp://example.com/abc", "https://www example.com", "https://www&example.com", "https://www,example.com") }