Skip to content

Commit

Permalink
Use upper case for constant
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Oct 16, 2023
1 parent 6ef062f commit c542c42
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
30 changes: 15 additions & 15 deletions core/src/main/scala/sttp/model/ContentTypeRange.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package sttp.model

import sttp.model.ContentTypeRange.{Wildcard, emptyParameters}
import sttp.model.ContentTypeRange.{Wildcard, EmptyParameters}

case class ContentTypeRange(mainType: String, subType: String, charset: String, otherParameters: Map[String, String]) {
// required for binary compatibility
def this(mainType: String, subType: String, charset: String) = this(mainType, subType, charset, emptyParameters)
def this(mainType: String, subType: String, charset: String) = this(mainType, subType, charset, EmptyParameters)

def copy(
mainType: String = this.mainType,
Expand Down Expand Up @@ -46,22 +46,22 @@ case class ContentTypeRange(mainType: String, subType: String, charset: String,
object ContentTypeRange {
// required for binary compatibility
def apply(mainType: String, subType: String, charset: String): ContentTypeRange =
new ContentTypeRange(mainType, subType, charset, emptyParameters)
new ContentTypeRange(mainType, subType, charset, EmptyParameters)

val Wildcard = "*"
val emptyParameters: Map[String, String] = Map.empty
val EmptyParameters: Map[String, String] = Map.empty

val AnyRange: ContentTypeRange = ContentTypeRange(Wildcard, Wildcard, Wildcard, emptyParameters)
val AnyApplication: ContentTypeRange = ContentTypeRange("application", Wildcard, Wildcard, emptyParameters)
val AnyAudio: ContentTypeRange = ContentTypeRange("audio", Wildcard, Wildcard, emptyParameters)
val AnyImage: ContentTypeRange = ContentTypeRange("image", Wildcard, Wildcard, emptyParameters)
val AnyMessage: ContentTypeRange = ContentTypeRange("message", Wildcard, Wildcard, emptyParameters)
val AnyMultipart: ContentTypeRange = ContentTypeRange("multipart", Wildcard, Wildcard, emptyParameters)
val AnyText: ContentTypeRange = ContentTypeRange("text", Wildcard, Wildcard, emptyParameters)
val AnyVideo: ContentTypeRange = ContentTypeRange("video", Wildcard, Wildcard, emptyParameters)
val AnyFont: ContentTypeRange = ContentTypeRange("font", Wildcard, Wildcard, emptyParameters)
val AnyExample: ContentTypeRange = ContentTypeRange("example", Wildcard, Wildcard, emptyParameters)
val AnyModel: ContentTypeRange = ContentTypeRange("model", Wildcard, Wildcard, emptyParameters)
val AnyRange: ContentTypeRange = ContentTypeRange(Wildcard, Wildcard, Wildcard, EmptyParameters)
val AnyApplication: ContentTypeRange = ContentTypeRange("application", Wildcard, Wildcard, EmptyParameters)
val AnyAudio: ContentTypeRange = ContentTypeRange("audio", Wildcard, Wildcard, EmptyParameters)
val AnyImage: ContentTypeRange = ContentTypeRange("image", Wildcard, Wildcard, EmptyParameters)
val AnyMessage: ContentTypeRange = ContentTypeRange("message", Wildcard, Wildcard, EmptyParameters)
val AnyMultipart: ContentTypeRange = ContentTypeRange("multipart", Wildcard, Wildcard, EmptyParameters)
val AnyText: ContentTypeRange = ContentTypeRange("text", Wildcard, Wildcard, EmptyParameters)
val AnyVideo: ContentTypeRange = ContentTypeRange("video", Wildcard, Wildcard, EmptyParameters)
val AnyFont: ContentTypeRange = ContentTypeRange("font", Wildcard, Wildcard, EmptyParameters)
val AnyExample: ContentTypeRange = ContentTypeRange("example", Wildcard, Wildcard, EmptyParameters)
val AnyModel: ContentTypeRange = ContentTypeRange("model", Wildcard, Wildcard, EmptyParameters)

def exact(mt: MediaType): ContentTypeRange =
ContentTypeRange(mt.mainType, mt.subType, mt.charset.getOrElse(Wildcard), mt.otherParameters)
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/sttp/model/headers/Accepts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ object Accepts {
): Seq[ContentTypeRange] = {
(mediaTypes, charsets) match {
case (Nil, Nil) => AnyRange :: Nil
case (Nil, (ch, _) :: Nil) => ContentTypeRange(Wildcard, Wildcard, ch, emptyParameters) :: Nil
case (Nil, (ch, _) :: Nil) => ContentTypeRange(Wildcard, Wildcard, ch, EmptyParameters) :: Nil
case ((mt, _) :: Nil, Nil) => ContentTypeRange(mt.mainType, mt.subType, Wildcard, mt.otherParameters) :: Nil
case (Nil, chs) =>
chs.sortBy({ case (_, q) => -q }).map { case (ch, _) =>
ContentTypeRange(Wildcard, Wildcard, ch, emptyParameters)
ContentTypeRange(Wildcard, Wildcard, ch, EmptyParameters)
}
case (mts, Nil) =>
mts.sortBy({ case (_, q) => -q }).map { case (mt, _) =>
Expand Down
52 changes: 26 additions & 26 deletions core/src/test/scala/sttp/model/MediaTypeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sttp.model
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.prop.TableDrivenPropertyChecks
import sttp.model.ContentTypeRange.{AnyRange, emptyParameters}
import sttp.model.ContentTypeRange.{AnyRange, EmptyParameters}

import scala.collection.immutable.Seq

Expand Down Expand Up @@ -62,24 +62,24 @@ class MediaTypeTests extends AnyFlatSpec with Matchers with TableDrivenPropertyC
private val matchCases = Table(
("media type", "content type range", "matches"),
(MediaType.ApplicationJson, AnyRange, true),
(MediaType("*", "html"), ContentTypeRange("*", "json", "*", emptyParameters), true),
(MediaType("text", "*"), ContentTypeRange("text", "*", "*", emptyParameters), true),
(MediaType.ApplicationJson, ContentTypeRange("application", "*", "*", emptyParameters), true),
(MediaType.ApplicationJson, ContentTypeRange("application", "json", "*", emptyParameters), true),
(MediaType("*", "html"), ContentTypeRange("*", "json", "*", EmptyParameters), true),
(MediaType("text", "*"), ContentTypeRange("text", "*", "*", EmptyParameters), true),
(MediaType.ApplicationJson, ContentTypeRange("application", "*", "*", EmptyParameters), true),
(MediaType.ApplicationJson, ContentTypeRange("application", "json", "*", EmptyParameters), true),
(MediaType.ApplicationJson, ContentTypeRange("application", "json", "*", Map("a" -> "1")), true),
(MediaType.ApplicationJson.copy(otherParameters = Map("a" -> "truE")), ContentTypeRange("application", "json", "*", Map("A" -> "TrUe")), true),
(MediaType.ApplicationJson.copy(otherParameters = Map("a" -> "1")), ContentTypeRange("application", "json", "*", Map("A" -> "1", "b" -> "2")), true),
(MediaType.ApplicationJson.copy(otherParameters = Map("a" -> "1", "b" -> "2")), ContentTypeRange("application", "json", "*", Map("A" -> "1")), false),
(MediaType.ApplicationJson.copy(otherParameters = Map("a" -> "1")), ContentTypeRange("application", "json", "*", Map("b" -> "2")), false),
//
(MediaType.ApplicationJson.charset("utf-8"), ContentTypeRange("*", "*", "utf-16", emptyParameters), false),
(MediaType("*", "html").charset("utf-8"), ContentTypeRange("*", "json", "utf-16", emptyParameters), false),
(MediaType("text", "*").charset("utf-8"), ContentTypeRange("text", "*", "utf-16", emptyParameters), false),
(MediaType.ApplicationJson.charset("utf-8"), ContentTypeRange("application", "*", "utf-16", emptyParameters), false),
(MediaType.ApplicationJson.charset("utf-8"), ContentTypeRange("application", "json", "utf-16", emptyParameters), false),
(MediaType.ApplicationJson.charset("utf-8"), ContentTypeRange("*", "*", "utf-16", EmptyParameters), false),
(MediaType("*", "html").charset("utf-8"), ContentTypeRange("*", "json", "utf-16", EmptyParameters), false),
(MediaType("text", "*").charset("utf-8"), ContentTypeRange("text", "*", "utf-16", EmptyParameters), false),
(MediaType.ApplicationJson.charset("utf-8"), ContentTypeRange("application", "*", "utf-16", EmptyParameters), false),
(MediaType.ApplicationJson.charset("utf-8"), ContentTypeRange("application", "json", "utf-16", EmptyParameters), false),
//
(MediaType.ApplicationJson.charset("utf-8"), ContentTypeRange("application", "json", "*", emptyParameters), true),
(MediaType.ApplicationOctetStream, ContentTypeRange("*", "*", "utf-8", emptyParameters), true)
(MediaType.ApplicationJson.charset("utf-8"), ContentTypeRange("application", "json", "*", EmptyParameters), true),
(MediaType.ApplicationOctetStream, ContentTypeRange("*", "*", "utf-8", EmptyParameters), true)
)

forAll(matchCases) { (mt, range, matches) =>
Expand All @@ -91,48 +91,48 @@ class MediaTypeTests extends AnyFlatSpec with Matchers with TableDrivenPropertyC
private val bestMatchCases = Table(
("ranges", "best match"),
(Seq(AnyRange), Some(MediaType.ApplicationJson.charset("utf-8"))),
(Seq(ContentTypeRange("application", "json", "*", emptyParameters)), Some(MediaType.ApplicationJson.charset("utf-8"))),
(Seq(ContentTypeRange("application", "xml", "*", emptyParameters)), Some(MediaType.ApplicationXml.charset("utf-8"))),
(Seq(ContentTypeRange("application", "json", "*", EmptyParameters)), Some(MediaType.ApplicationJson.charset("utf-8"))),
(Seq(ContentTypeRange("application", "xml", "*", EmptyParameters)), Some(MediaType.ApplicationXml.charset("utf-8"))),
(
Seq(
ContentTypeRange("application", "xml", "*", emptyParameters),
ContentTypeRange("application", "json", "*", emptyParameters)
ContentTypeRange("application", "xml", "*", EmptyParameters),
ContentTypeRange("application", "json", "*", EmptyParameters)
),
Some(MediaType.ApplicationXml.charset("utf-8"))
),
(
Seq(
ContentTypeRange("application", "json", "*", emptyParameters),
ContentTypeRange("application", "xml", "*", emptyParameters)
ContentTypeRange("application", "json", "*", EmptyParameters),
ContentTypeRange("application", "xml", "*", EmptyParameters)
),
Some(MediaType.ApplicationJson.charset("utf-8"))
),
(
Seq(
ContentTypeRange("application", "xml", "*", emptyParameters),
ContentTypeRange("application", "json", "*", emptyParameters),
ContentTypeRange("text", "html", "*", emptyParameters)
ContentTypeRange("application", "xml", "*", EmptyParameters),
ContentTypeRange("application", "json", "*", EmptyParameters),
ContentTypeRange("text", "html", "*", EmptyParameters)
),
Some(MediaType.ApplicationXml.charset("utf-8"))
),
(
Seq(ContentTypeRange("text", "*", "*", emptyParameters), ContentTypeRange("application", "*", "*", emptyParameters)),
Seq(ContentTypeRange("text", "*", "*", EmptyParameters), ContentTypeRange("application", "*", "*", EmptyParameters)),
Some(MediaType.TextHtml.charset("utf-8"))
),
(
Seq(ContentTypeRange("*", "*", "iso-8859-1", emptyParameters)),
Seq(ContentTypeRange("*", "*", "iso-8859-1", EmptyParameters)),
Some(MediaType.TextHtml.charset("iso-8859-1"))
),
(
Seq(ContentTypeRange("text", "html", "iso-8859-1", emptyParameters), ContentTypeRange("text", "html", "utf-8", emptyParameters)),
Seq(ContentTypeRange("text", "html", "iso-8859-1", EmptyParameters), ContentTypeRange("text", "html", "utf-8", EmptyParameters)),
Some(MediaType.TextHtml.charset("iso-8859-1"))
),
(
Seq(ContentTypeRange("text", "csv", "*", emptyParameters)),
Seq(ContentTypeRange("text", "csv", "*", EmptyParameters)),
None
),
(
Seq(ContentTypeRange("text", "html", "utf-16", emptyParameters)),
Seq(ContentTypeRange("text", "html", "utf-16", EmptyParameters)),
None
)
)
Expand Down
44 changes: 22 additions & 22 deletions core/src/test/scalajvm/sttp/model/headers/AcceptsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sttp.model.headers
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.prop.TableDrivenPropertyChecks
import sttp.model.ContentTypeRange.{AnyRange, emptyParameters}
import sttp.model.ContentTypeRange.{AnyRange, EmptyParameters}
import sttp.model.{ContentTypeRange, Header, HeaderNames}

import scala.collection.immutable.Seq
Expand All @@ -12,47 +12,47 @@ class AcceptsTest extends AnyFlatSpec with Matchers with TableDrivenPropertyChec

private val acceptsCases = Table(
("Accept", "Accept-Charset", "result"),
("application/json", "*", Seq(ContentTypeRange("application", "json", "*", emptyParameters))),
("application/json", "*", Seq(ContentTypeRange("application", "json", "*", EmptyParameters))),
("application/json;q=.8", "*", Seq(ContentTypeRange("application", "json", "*", Map("q" -> ".8")))),
("application/json;q=.88", "*", Seq(ContentTypeRange("application", "json", "*", Map("q" -> ".88")))),
("application/json;q=.888", "*", Seq(ContentTypeRange("application", "json", "*", Map("q" -> ".888")))),
("application/json", "utf-8", Seq(ContentTypeRange("application", "json", "utf-8", emptyParameters))),
("application/json", "utf-8;a=1;b=2;c=3", Seq(ContentTypeRange("application", "json", "utf-8", emptyParameters))),
("application/json", "utf-8", Seq(ContentTypeRange("application", "json", "utf-8", EmptyParameters))),
("application/json", "utf-8;a=1;b=2;c=3", Seq(ContentTypeRange("application", "json", "utf-8", EmptyParameters))),
(
"text/plain",
"utf-8, iso-8559-1",
Seq(ContentTypeRange("text", "plain", "utf-8", emptyParameters), ContentTypeRange("text", "plain", "iso-8559-1", emptyParameters))
Seq(ContentTypeRange("text", "plain", "utf-8", EmptyParameters), ContentTypeRange("text", "plain", "iso-8559-1", EmptyParameters))
),
(
"text/plain",
"utf-8;q=0.99, iso-8559-1",
Seq(ContentTypeRange("text", "plain", "iso-8559-1", emptyParameters), ContentTypeRange("text", "plain", "utf-8", emptyParameters))
Seq(ContentTypeRange("text", "plain", "iso-8559-1", EmptyParameters), ContentTypeRange("text", "plain", "utf-8", EmptyParameters))
),
(
"text/plain",
"utf-8;q=0.55, iso-8559-1;q=0.66, utf-16;q=0.77",
Seq(
ContentTypeRange("text", "plain", "utf-16", emptyParameters),
ContentTypeRange("text", "plain", "iso-8559-1", emptyParameters),
ContentTypeRange("text", "plain", "utf-8", emptyParameters)
ContentTypeRange("text", "plain", "utf-16", EmptyParameters),
ContentTypeRange("text", "plain", "iso-8559-1", EmptyParameters),
ContentTypeRange("text", "plain", "utf-8", EmptyParameters)
)
),
(
"text/csv, text/plain",
"utf-8, utf-16;q=0.5",
Seq(
ContentTypeRange("text", "csv", "utf-8", emptyParameters),
ContentTypeRange("text", "plain", "utf-8", emptyParameters),
ContentTypeRange("text", "csv", "utf-16", emptyParameters),
ContentTypeRange("text", "plain", "utf-16", emptyParameters)
ContentTypeRange("text", "csv", "utf-8", EmptyParameters),
ContentTypeRange("text", "plain", "utf-8", EmptyParameters),
ContentTypeRange("text", "csv", "utf-16", EmptyParameters),
ContentTypeRange("text", "plain", "utf-16", EmptyParameters)
)
),
(
"text/csv, text/plain;q=0.1",
"utf-8, utf-16;q=0.5",
Seq(
ContentTypeRange("text", "csv", "utf-8", emptyParameters),
ContentTypeRange("text", "csv", "utf-16", emptyParameters),
ContentTypeRange("text", "csv", "utf-8", EmptyParameters),
ContentTypeRange("text", "csv", "utf-16", EmptyParameters),
ContentTypeRange("text", "plain", "utf-8", Map("q" -> "0.1")),
ContentTypeRange("text", "plain", "utf-16", Map("q" -> "0.1"))
)
Expand All @@ -61,7 +61,7 @@ class AcceptsTest extends AnyFlatSpec with Matchers with TableDrivenPropertyChec
"text/*, application/json;q=0.9",
"utf-8",
Seq(
ContentTypeRange("text", "*", "utf-8", emptyParameters),
ContentTypeRange("text", "*", "utf-8", EmptyParameters),
ContentTypeRange("application", "json", "utf-8", Map("q" -> "0.9"))
)
),
Expand All @@ -79,9 +79,9 @@ class AcceptsTest extends AnyFlatSpec with Matchers with TableDrivenPropertyChec
"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
"*",
Seq(
ContentTypeRange("text", "html", "*", emptyParameters),
ContentTypeRange("image", "gif", "*", emptyParameters),
ContentTypeRange("image", "jpeg", "*", emptyParameters),
ContentTypeRange("text", "html", "*", EmptyParameters),
ContentTypeRange("image", "gif", "*", EmptyParameters),
ContentTypeRange("image", "jpeg", "*", EmptyParameters),
ContentTypeRange("*", "*", "*", Map("q" -> ".2")),
ContentTypeRange("*", "*", "*", Map("q" -> ".2"))
)
Expand Down Expand Up @@ -109,14 +109,14 @@ class AcceptsTest extends AnyFlatSpec with Matchers with TableDrivenPropertyChec

it should "parse when only Accept-Charset specified" in {
Accepts.unsafeParse(otherHeaders ++ Seq(Header(HeaderNames.AcceptCharset, "utf-16;q=0.5, utf-8"))) shouldBe Seq(
ContentTypeRange("*", "*", "utf-8", emptyParameters),
ContentTypeRange("*", "*", "utf-16", emptyParameters)
ContentTypeRange("*", "*", "utf-8", EmptyParameters),
ContentTypeRange("*", "*", "utf-16", EmptyParameters)
)
}

it should "parse when only Accept specified" in {
Accepts.unsafeParse(otherHeaders ++ Seq(Header(HeaderNames.Accept, "text/csv;q=0.1, text/plain"))) shouldBe Seq(
ContentTypeRange("text", "plain", "*", emptyParameters),
ContentTypeRange("text", "plain", "*", EmptyParameters),
ContentTypeRange("text", "csv", "*", Map("q" -> "0.1"))
)
}
Expand Down

0 comments on commit c542c42

Please sign in to comment.