From 7ca799d872d35df1143b89ae9dbaa1ab2dbcaf40 Mon Sep 17 00:00:00 2001 From: pfeme Date: Sat, 15 Jun 2024 09:45:59 +0200 Subject: [PATCH] fix regex and add tests --- .../src/test/scala/zio/http/RegexSpec.scala | 67 +++++++++++++++++++ .../src/main/scala/zio/http/Header.scala | 6 +- 2 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 zio-http/js/src/test/scala/zio/http/RegexSpec.scala diff --git a/zio-http/js/src/test/scala/zio/http/RegexSpec.scala b/zio-http/js/src/test/scala/zio/http/RegexSpec.scala new file mode 100644 index 0000000000..b70f042091 --- /dev/null +++ b/zio-http/js/src/test/scala/zio/http/RegexSpec.scala @@ -0,0 +1,67 @@ +package zio.http + +import zio._ +import zio.test._ + +object RegexSpec extends ZIOSpecDefault { + override def spec: Spec[TestEnvironment with Scope, Any] = + suite("RegexSpec")( + suite("Header")( + test("AcceptLanguage") { + ZIO.succeed(Header.AcceptLanguage).isSuccess.map(s => assertTrue(s)) + }, + test("ContentDisposition") { + ZIO.succeed(Header.ContentDisposition).isSuccess.map(s => assertTrue(s)) + }, + test("ContentMd5") { + ZIO.succeed(Header.ContentMd5).isSuccess.map(s => assertTrue(s)) + }, + test("ContentRange") { + ZIO.succeed(Header.ContentRange).isSuccess.map(s => assertTrue(s)) + }, + test("ContentSecurityPolicy.Source") { + ZIO.succeed(Header.ContentSecurityPolicy.Source).isSuccess.map(s => assertTrue(s)) + }, + test("ContentSecurityPolicy.TrustedTypesValue") { + ZIO.succeed(Header.ContentSecurityPolicy.TrustedTypesValue).isSuccess.map(s => assertTrue(s)) + }, + test("ContentSecurityPolicy") { + ZIO.succeed(Header.ContentSecurityPolicy).isSuccess.map(s => assertTrue(s)) + }, + test("ContentTransferEncoding") { + ZIO.succeed(Header.ContentTransferEncoding).isSuccess.map(s => assertTrue(s)) + }, + test("From") { + ZIO.succeed(Header.From).isSuccess.map(s => assertTrue(s)) + }, + test("Trailer") { + ZIO.succeed(Header.Trailer).isSuccess.map(s => assertTrue(s)) + }, + test("UserAgent") { + ZIO.succeed(Header.UserAgent).isSuccess.map(s => assertTrue(s)) + }, + test("WWWAuthenticate") { + ZIO.succeed(Header.WWWAuthenticate).isSuccess.map(s => assertTrue(s)) + }, + test("WWWAuthenticate") { + ZIO.succeed(Header.WWWAuthenticate).isSuccess.map(s => assertTrue(s)) + }, + ), + suite("HttpContentCodec")( + test("HttpContentCodec") { + ZIO.succeed(codec.HttpContentCodec).isSuccess.map(s => assertTrue(s)) + }, + ), + suite("OpenAPI")( + test("Key") { + ZIO.succeed(endpoint.openapi.OpenAPI.Key).isSuccess.map(s => assertTrue(s)) + }, + test("Path") { + ZIO.succeed(endpoint.openapi.OpenAPI.Path).isSuccess.map(s => assertTrue(s)) + }, + test("LiteralOrExpression") { + ZIO.succeed(endpoint.openapi.OpenAPI.LiteralOrExpression).isSuccess.map(s => assertTrue(s)) + }, + ), + ) +} diff --git a/zio-http/shared/src/main/scala/zio/http/Header.scala b/zio-http/shared/src/main/scala/zio/http/Header.scala index b347cd5438..25025e4d0b 100644 --- a/zio-http/shared/src/main/scala/zio/http/Header.scala +++ b/zio-http/shared/src/main/scala/zio/http/Header.scala @@ -4149,9 +4149,9 @@ object Header { final case class Comment(comment: String) extends UserAgent - private val productRegex = """(?i)([a-z0-9]+)(?:/([a-z0-9.]+))?""".r - private val commentRegex = """(?i)\((.*)$""".r - private val completeRegex = s"""^(?i)([a-z0-9]+)(?:/([a-z0-9.]+))(.*)$$""".r + private val productRegex = "(?i)([a-z0-9]+)(?:/([a-z0-9.]+))?".r + private val commentRegex = """(?i)\((.*)""".r + private val completeRegex = "(?i)([a-z0-9]+)(?:/([a-z0-9.]+))(.*)".r def parse(userAgent: String): Either[String, UserAgent] = { userAgent match {