Skip to content

Commit 2b169d5

Browse files
desiderantesadamwclaude
authored
Add RFC 10008 (The HTTP QUERY Method) support (#465)
This adds basic support so it can be used to build proper RFC 10008 support in consumers. --------- Co-authored-by: Adam Warski <adam@warski.org> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent fd28b69 commit 2b169d5

5 files changed

Lines changed: 18 additions & 2 deletions

File tree

core/src/main/scala/sttp/model/Header.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ object Header {
8080
def accept(mediaType: MediaType, additionalMediaTypes: MediaType*): Header = accept(
8181
s"${(mediaType :: additionalMediaTypes.toList).map(_.noCharset).mkString(", ")}"
8282
)
83+
def acceptQuery(mediaType: MediaType, additionalMediaTypes: MediaType*): Header = acceptQuery(
84+
s"${(mediaType :: additionalMediaTypes.toList).map(_.toString).mkString(", ")}"
85+
)
8386
def accept(mediaRanges: String): Header = Header(HeaderNames.Accept, mediaRanges)
87+
def acceptQuery(mediaRanges: String): Header = Header(HeaderNames.AcceptQuery, mediaRanges)
8488
def acceptCharset(charsetRanges: String): Header = Header(HeaderNames.AcceptCharset, charsetRanges)
8589
def acceptEncoding(encodingRanges: String): Header = Header(HeaderNames.AcceptEncoding, encodingRanges)
8690
def accessControlAllowCredentials(allow: Boolean): Header =

core/src/main/scala/sttp/model/HeaderNames.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ trait HeaderNames {
77
val AcceptEncoding = "Accept-Encoding"
88
val AcceptLanguage = "Accept-Language"
99
val AcceptRanges = "Accept-Ranges"
10+
// a def, not a val, as adding a val to a trait breaks binary compatibility for classes implementing it
11+
def AcceptQuery: String = "Accept-Query"
1012
val AccessControlAllowCredentials = "Access-Control-Allow-Credentials"
1113
val AccessControlAllowHeaders = "Access-Control-Allow-Headers"
1214
val AccessControlAllowMethods = "Access-Control-Allow-Methods"

core/src/main/scala/sttp/model/Method.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ object Method extends Methods {
3232
def isSafe(m: Method): Boolean = safe.contains(m)
3333

3434
private val idempotent: Set[Method] =
35-
Set(Method.HEAD, Method.TRACE, Method.GET, Method.PUT, Method.OPTIONS, Method.DELETE)
36-
private val safe: Set[Method] = Set(Method.HEAD, Method.GET, Method.OPTIONS)
35+
Set(Method.HEAD, Method.TRACE, Method.GET, Method.PUT, Method.OPTIONS, Method.DELETE, Method.QUERY)
36+
private val safe: Set[Method] = Set(Method.HEAD, Method.GET, Method.OPTIONS, Method.QUERY)
3737
}
3838

3939
trait Methods {
@@ -42,6 +42,8 @@ trait Methods {
4242
val POST: Method = Method("POST")
4343
val PUT: Method = Method("PUT")
4444
val DELETE: Method = Method("DELETE")
45+
// a def, not a val, as adding a val to a trait breaks binary compatibility for classes implementing it
46+
def QUERY: Method = Method("QUERY")
4547
val OPTIONS: Method = Method("OPTIONS")
4648
val PATCH: Method = Method("PATCH")
4749
val CONNECT: Method = Method("CONNECT")

core/src/test/scala/sttp/model/MethodTest.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ class MethodTest extends AnyFlatSpec with Matchers {
1414
Method.unsafeApply("patch") shouldBe Method.unsafeApply("PATCH")
1515
Method.unsafeApply("connect") shouldBe Method.unsafeApply("CONNECT")
1616
Method.unsafeApply("trace") shouldBe Method.unsafeApply("TRACE")
17+
Method.unsafeApply("query") shouldBe Method.unsafeApply("QUERY")
1718
}
1819
}

core/src/test/scalajvm/sttp/model/HeaderTests.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ class HeaderTests extends AnyFlatSpec with Matchers {
5252
.toString shouldBe "Cache-Control: no-transform, public, s-maxage=10"
5353
}
5454

55+
it should "properly create an accept-query header" in {
56+
Header
57+
.acceptQuery(MediaType.ApplicationJson, MediaType.ApplicationXml)
58+
.toString shouldBe "Accept-Query: application/json, application/xml"
59+
Header.acceptQuery("application/jsonpath").toString shouldBe "Accept-Query: application/jsonpath"
60+
}
61+
5562
"Instant" should "be formatted according to rfc1123-date1 with a leading zero for single-digit dates" in {
5663
Header.toHttpDateString(rfc1123DatetimeToBeChecked) shouldBe rfc1123DatetimeFormatted
5764
}

0 commit comments

Comments
 (0)