mirror of
https://github.com/square/okhttp.git
synced 2025-07-31 05:04:26 +03:00
Extension Functions toRequestBody(), toResponseBody()
This commit is contained in:
@ -24,7 +24,7 @@ import okhttp3.MediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Protocol
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.curl.Main.Companion.NAME
|
||||
import okhttp3.internal.format
|
||||
import okhttp3.internal.http.StatusLine
|
||||
@ -187,7 +187,7 @@ class Main : Runnable {
|
||||
request.url(requestUrl)
|
||||
|
||||
data?.let {
|
||||
request.method(requestMethod, RequestBody.create(mediaType(), it))
|
||||
request.method(requestMethod, it.toRequestBody(mediaType()))
|
||||
}
|
||||
|
||||
for (header in headers.orEmpty()) {
|
||||
|
@ -24,7 +24,7 @@ import okhttp3.MediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Protocol
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import okhttp3.internal.platform.Platform
|
||||
import okhttp3.internal.publicsuffix.PublicSuffixDatabase
|
||||
@ -244,7 +244,7 @@ class DnsOverHttps internal constructor(builder: Builder) : Dns {
|
||||
val query = DnsRecordCodec.encodeQuery(hostname, type)
|
||||
|
||||
if (post) {
|
||||
url(url).post(RequestBody.create(DNS_MESSAGE, query))
|
||||
url(url).post(query.toRequestBody(DNS_MESSAGE))
|
||||
} else {
|
||||
val encoded = query.base64Url().replace("=", "")
|
||||
val requestUrl = url.newBuilder().addQueryParameter("dns", encoded).build()
|
||||
|
@ -140,7 +140,7 @@ public final class HttpLoggingInterceptorTest {
|
||||
setLevel(Level.BASIC);
|
||||
|
||||
server.enqueue(new MockResponse());
|
||||
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
|
||||
client.newCall(request().post(RequestBody.create("Hi?", PLAIN)).build()).execute();
|
||||
|
||||
applicationLogs
|
||||
.assertLogEqual("--> POST " + url + " (3-byte body)")
|
||||
@ -225,7 +225,7 @@ public final class HttpLoggingInterceptorTest {
|
||||
setLevel(Level.HEADERS);
|
||||
|
||||
server.enqueue(new MockResponse());
|
||||
Request request = request().post(RequestBody.create(PLAIN, "Hi?")).build();
|
||||
Request request = request().post(RequestBody.create("Hi?", PLAIN)).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
response.body().close();
|
||||
|
||||
@ -258,7 +258,7 @@ public final class HttpLoggingInterceptorTest {
|
||||
setLevel(Level.HEADERS);
|
||||
|
||||
server.enqueue(new MockResponse());
|
||||
Request request = request().post(RequestBody.create(null, "Hi?")).build();
|
||||
Request request = request().post(RequestBody.create("Hi?", null)).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
response.body().close();
|
||||
|
||||
@ -426,7 +426,7 @@ public final class HttpLoggingInterceptorTest {
|
||||
setLevel(Level.BODY);
|
||||
|
||||
server.enqueue(new MockResponse());
|
||||
Request request = request().post(RequestBody.create(PLAIN, "Hi?")).build();
|
||||
Request request = request().post(RequestBody.create("Hi?", PLAIN)).build();
|
||||
Response response = client.newCall(request).execute();
|
||||
response.body().close();
|
||||
|
||||
|
@ -101,7 +101,7 @@ public final class LoggingEventListenerTest {
|
||||
@Test
|
||||
public void post() throws IOException {
|
||||
server.enqueue(new MockResponse());
|
||||
client.newCall(request().post(RequestBody.create(PLAIN, "Hello!")).build()).execute();
|
||||
client.newCall(request().post(RequestBody.create("Hello!", PLAIN)).build()).execute();
|
||||
|
||||
logRecorder
|
||||
.assertLogMatch("callStart: Request\\{method=POST, url=" + url + ", tags=\\{\\}\\}")
|
||||
|
@ -1619,9 +1619,9 @@ class HttpUrl internal constructor(
|
||||
}
|
||||
|
||||
/**
|
||||
* Cuts [this] up into alternating parameter names and values. This divides a query string like
|
||||
* `subject=math&easy&problem=5-2=3` into the list `["subject", "math", "easy", null, "problem",
|
||||
* "5-2=3"]`. Note that values may be null and may contain '=' characters.
|
||||
* Cuts this string up into alternating parameter names and values. This divides a query string
|
||||
* like `subject=math&easy&problem=5-2=3` into the list `["subject", "math", "easy", null,
|
||||
* "problem", "5-2=3"]`. Note that values may be null and may contain '=' characters.
|
||||
*/
|
||||
internal fun String.toQueryNamesAndValues(): MutableList<String?> {
|
||||
val result = mutableListOf<String?>()
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package okhttp3
|
||||
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.internal.toImmutableList
|
||||
import okio.Buffer
|
||||
import okio.BufferedSink
|
||||
@ -203,7 +204,7 @@ class MultipartBody internal constructor(
|
||||
|
||||
@JvmStatic
|
||||
fun createFormData(name: String, value: String): Part =
|
||||
createFormData(name, null, create(null, value))
|
||||
createFormData(name, null, value.toRequestBody())
|
||||
|
||||
@JvmStatic
|
||||
fun createFormData(name: String, filename: String?, body: RequestBody): Part {
|
||||
|
@ -34,7 +34,7 @@ abstract class RequestBody {
|
||||
* or -1 if that count is unknown.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
open fun contentLength(): Long = -1
|
||||
open fun contentLength(): Long = -1L
|
||||
|
||||
/** Writes the content of this request to [sink]. */
|
||||
@Throws(IOException::class)
|
||||
@ -91,11 +91,12 @@ abstract class RequestBody {
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* Returns a new request body that transmits [content]. If [contentType] is non-null
|
||||
* and lacks a charset, this will use UTF-8.
|
||||
* Returns a new request body that transmits this string. If [contentType] is non-null and lacks
|
||||
* a charset, this will use UTF-8.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun create(contentType: MediaType?, content: String): RequestBody {
|
||||
@JvmName("create")
|
||||
fun String.toRequestBody(contentType: MediaType? = null): RequestBody {
|
||||
var charset: Charset = UTF_8
|
||||
var finalContentType: MediaType? = contentType
|
||||
if (contentType != null) {
|
||||
@ -107,56 +108,108 @@ abstract class RequestBody {
|
||||
charset = resolvedCharset
|
||||
}
|
||||
}
|
||||
val bytes = content.toByteArray(charset)
|
||||
return create(finalContentType, bytes)
|
||||
val bytes = toByteArray(charset)
|
||||
return bytes.toRequestBody(finalContentType, 0, bytes.size)
|
||||
}
|
||||
|
||||
/** Returns a new request body that transmits [content]. */
|
||||
/** Returns a new request body that transmits this. */
|
||||
@JvmStatic
|
||||
fun create(
|
||||
contentType: MediaType?,
|
||||
content: ByteString
|
||||
): RequestBody = object : RequestBody() {
|
||||
override fun contentType() = contentType
|
||||
@JvmName("create")
|
||||
fun ByteString.toRequestBody(contentType: MediaType? = null): RequestBody {
|
||||
return object : RequestBody() {
|
||||
override fun contentType() = contentType
|
||||
|
||||
override fun contentLength() = content.size.toLong()
|
||||
override fun contentLength() = size.toLong()
|
||||
|
||||
override fun writeTo(sink: BufferedSink) {
|
||||
sink.write(content)
|
||||
override fun writeTo(sink: BufferedSink) {
|
||||
sink.write(this@toRequestBody)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a new request body that transmits [content]. */
|
||||
/** Returns a new request body that transmits this. */
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun create(
|
||||
contentType: MediaType?,
|
||||
content: ByteArray,
|
||||
@JvmName("create")
|
||||
fun ByteArray.toRequestBody(
|
||||
contentType: MediaType? = null,
|
||||
offset: Int = 0,
|
||||
byteCount: Int = content.size
|
||||
byteCount: Int = size
|
||||
): RequestBody {
|
||||
checkOffsetAndCount(content.size.toLong(), offset.toLong(), byteCount.toLong())
|
||||
checkOffsetAndCount(size.toLong(), offset.toLong(), byteCount.toLong())
|
||||
return object : RequestBody() {
|
||||
override fun contentType() = contentType
|
||||
|
||||
override fun contentLength() = byteCount.toLong()
|
||||
|
||||
override fun writeTo(sink: BufferedSink) {
|
||||
sink.write(content, offset, byteCount)
|
||||
sink.write(this@toRequestBody, offset, byteCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a new request body that transmits the content of [file]. */
|
||||
/** Returns a new request body that transmits the content of this. */
|
||||
@JvmStatic
|
||||
fun create(contentType: MediaType?, file: File): RequestBody = object : RequestBody() {
|
||||
override fun contentType() = contentType
|
||||
@JvmName("create")
|
||||
fun File.toRequestBody(contentType: MediaType? = null): RequestBody {
|
||||
return object : RequestBody() {
|
||||
override fun contentType() = contentType
|
||||
|
||||
override fun contentLength() = file.length()
|
||||
override fun contentLength() = length()
|
||||
|
||||
override fun writeTo(sink: BufferedSink) {
|
||||
file.source().use { source -> sink.writeAll(source) }
|
||||
override fun writeTo(sink: BufferedSink) {
|
||||
source().use { source -> sink.writeAll(source) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Deprecated(
|
||||
message = "Moved to extension function. Put the 'content' argument first to fix Java",
|
||||
replaceWith = ReplaceWith(
|
||||
expression = "content.toRequestBody(contentType)",
|
||||
imports = ["okhttp3.RequestBody.Companion.toRequestBody"]
|
||||
),
|
||||
level = DeprecationLevel.WARNING)
|
||||
fun create(contentType: MediaType?, content: String) = content.toRequestBody(contentType)
|
||||
|
||||
@JvmStatic
|
||||
@Deprecated(
|
||||
message = "Moved to extension function. Put the 'content' argument first to fix Java",
|
||||
replaceWith = ReplaceWith(
|
||||
expression = "content.toRequestBody(contentType)",
|
||||
imports = ["okhttp3.RequestBody.Companion.toRequestBody"]
|
||||
),
|
||||
level = DeprecationLevel.WARNING)
|
||||
fun create(
|
||||
contentType: MediaType?,
|
||||
content: ByteString
|
||||
): RequestBody = content.toRequestBody(contentType)
|
||||
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
@Deprecated(
|
||||
message = "Moved to extension function. Put the 'content' argument first to fix Java",
|
||||
replaceWith = ReplaceWith(
|
||||
expression = "content.toRequestBody(contentType, offset, byteCount)",
|
||||
imports = ["okhttp3.RequestBody.Companion.toRequestBody"]
|
||||
),
|
||||
level = DeprecationLevel.WARNING)
|
||||
fun create(
|
||||
contentType: MediaType?,
|
||||
content: ByteArray,
|
||||
offset: Int = 0,
|
||||
byteCount: Int = content.size
|
||||
) = content.toRequestBody(contentType, offset, byteCount)
|
||||
|
||||
@JvmStatic
|
||||
@Deprecated(
|
||||
message = "Moved to extension function. Put the 'file' argument first to fix Java",
|
||||
replaceWith = ReplaceWith(
|
||||
expression = "file.toRequestBody(contentType)",
|
||||
imports = ["okhttp3.RequestBody.Companion.toRequestBody"]
|
||||
),
|
||||
level = DeprecationLevel.WARNING)
|
||||
fun create(contentType: MediaType?, file: File) = file.toRequestBody(contentType)
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package okhttp3
|
||||
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import okhttp3.internal.connection.Exchange
|
||||
import okhttp3.internal.http.StatusLine.Companion.HTTP_PERM_REDIRECT
|
||||
import okhttp3.internal.http.StatusLine.Companion.HTTP_TEMP_REDIRECT
|
||||
@ -195,7 +196,7 @@ class Response internal constructor(
|
||||
val buffer = Buffer()
|
||||
peeked.request(byteCount)
|
||||
buffer.write(peeked, minOf(byteCount, peeked.buffer.size))
|
||||
return ResponseBody.create(body.contentType(), buffer.size, buffer)
|
||||
return buffer.toResponseBody(body.contentType(), buffer.size)
|
||||
}
|
||||
|
||||
@JvmName("-deprecated_body")
|
||||
|
@ -203,13 +203,13 @@ abstract class ResponseBody : Closeable {
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* Returns a new response body that transmits [content]. If `contentType` is non-null
|
||||
* and lacks a charset, this will use UTF-8.
|
||||
* Returns a new response body that transmits this string. If [contentType] is non-null and
|
||||
* lacks a charset, this will use UTF-8.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun create(contentType: MediaType?, content: String): ResponseBody {
|
||||
@JvmName("create")
|
||||
fun String.toResponseBody(contentType: MediaType? = null): ResponseBody {
|
||||
var charset: Charset = UTF_8
|
||||
var finalContentType: MediaType? = contentType
|
||||
if (contentType != null) {
|
||||
@ -221,36 +221,84 @@ abstract class ResponseBody : Closeable {
|
||||
charset = resolvedCharset
|
||||
}
|
||||
}
|
||||
val buffer = Buffer().writeString(content, charset)
|
||||
return create(finalContentType, buffer.size, buffer)
|
||||
val buffer = Buffer().writeString(this, charset)
|
||||
return buffer.toResponseBody(finalContentType, buffer.size)
|
||||
}
|
||||
|
||||
/** Returns a new response body that transmits [content]. */
|
||||
/** Returns a new response body that transmits this byte array. */
|
||||
@JvmStatic
|
||||
fun create(contentType: MediaType?, content: ByteArray): ResponseBody {
|
||||
val buffer = Buffer().write(content)
|
||||
return create(contentType, content.size.toLong(), buffer)
|
||||
@JvmName("create")
|
||||
fun ByteArray.toResponseBody(contentType: MediaType? = null): ResponseBody {
|
||||
return Buffer()
|
||||
.write(this)
|
||||
.toResponseBody(contentType, size.toLong())
|
||||
}
|
||||
|
||||
/** Returns a new response body that transmits [content]. */
|
||||
/** Returns a new response body that transmits this byte string. */
|
||||
@JvmStatic
|
||||
fun create(contentType: MediaType?, content: ByteString): ResponseBody {
|
||||
val buffer = Buffer().write(content)
|
||||
return create(contentType, content.size.toLong(), buffer)
|
||||
@JvmName("create")
|
||||
fun ByteString.toResponseBody(contentType: MediaType? = null): ResponseBody {
|
||||
return Buffer()
|
||||
.write(this)
|
||||
.toResponseBody(contentType, size.toLong())
|
||||
}
|
||||
|
||||
/** Returns a new response body that transmits [content]. */
|
||||
/** Returns a new response body that transmits this source. */
|
||||
@JvmStatic
|
||||
fun create(
|
||||
contentType: MediaType?,
|
||||
contentLength: Long,
|
||||
content: BufferedSource
|
||||
@JvmName("create")
|
||||
fun BufferedSource.toResponseBody(
|
||||
contentType: MediaType? = null,
|
||||
contentLength: Long = -1L
|
||||
): ResponseBody = object : ResponseBody() {
|
||||
override fun contentType() = contentType
|
||||
|
||||
override fun contentLength() = contentLength
|
||||
|
||||
override fun source() = content
|
||||
override fun source() = this@toResponseBody
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Deprecated(
|
||||
message = "Moved to extension function. Put the 'content' argument first to fix Java",
|
||||
replaceWith = ReplaceWith(
|
||||
expression = "content.toResponseBody(contentType)",
|
||||
imports = ["okhttp3.ResponseBody.Companion.toResponseBody"]
|
||||
),
|
||||
level = DeprecationLevel.WARNING)
|
||||
fun create(contentType: MediaType?, content: String) = content.toResponseBody(contentType)
|
||||
|
||||
@JvmStatic
|
||||
@Deprecated(
|
||||
message = "Moved to extension function. Put the 'content' argument first to fix Java",
|
||||
replaceWith = ReplaceWith(
|
||||
expression = "content.toResponseBody(contentType)",
|
||||
imports = ["okhttp3.ResponseBody.Companion.toResponseBody"]
|
||||
),
|
||||
level = DeprecationLevel.WARNING)
|
||||
fun create(contentType: MediaType?, content: ByteArray) = content.toResponseBody(contentType)
|
||||
|
||||
@JvmStatic
|
||||
@Deprecated(
|
||||
message = "Moved to extension function. Put the 'content' argument first to fix Java",
|
||||
replaceWith = ReplaceWith(
|
||||
expression = "content.toResponseBody(contentType)",
|
||||
imports = ["okhttp3.ResponseBody.Companion.toResponseBody"]
|
||||
),
|
||||
level = DeprecationLevel.WARNING)
|
||||
fun create(contentType: MediaType?, content: ByteString) = content.toResponseBody(contentType)
|
||||
|
||||
@JvmStatic
|
||||
@Deprecated(
|
||||
message = "Moved to extension function. Put the 'content' argument first to fix Java",
|
||||
replaceWith = ReplaceWith(
|
||||
expression = "content.toResponseBody(contentType, contentLength)",
|
||||
imports = ["okhttp3.ResponseBody.Companion.toResponseBody"]
|
||||
),
|
||||
level = DeprecationLevel.WARNING)
|
||||
fun create(
|
||||
contentType: MediaType?,
|
||||
contentLength: Long,
|
||||
content: BufferedSource
|
||||
) = content.toResponseBody(contentType, contentLength)
|
||||
}
|
||||
}
|
||||
|
@ -20,9 +20,9 @@ package okhttp3.internal
|
||||
import okhttp3.EventListener
|
||||
import okhttp3.Headers
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import okhttp3.ResponseBody
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import okhttp3.internal.http2.Header
|
||||
import okio.Buffer
|
||||
import okio.BufferedSink
|
||||
@ -59,9 +59,9 @@ val EMPTY_BYTE_ARRAY = ByteArray(0)
|
||||
val EMPTY_HEADERS = Headers.of()
|
||||
|
||||
@JvmField
|
||||
val EMPTY_RESPONSE = ResponseBody.create(null, EMPTY_BYTE_ARRAY)
|
||||
val EMPTY_RESPONSE = EMPTY_BYTE_ARRAY.toResponseBody()
|
||||
@JvmField
|
||||
val EMPTY_REQUEST = RequestBody.create(null, EMPTY_BYTE_ARRAY)
|
||||
val EMPTY_REQUEST = EMPTY_BYTE_ARRAY.toRequestBody()
|
||||
|
||||
/** Byte order marks. */
|
||||
private val UNICODE_BOMS = Options.of(
|
||||
@ -105,8 +105,8 @@ fun threadFactory(
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing only elements found in [this] and also in [other].
|
||||
* The returned elements are in the same order as in [this].
|
||||
* Returns an array containing only elements found in this array and also in [other]. The returned
|
||||
* elements are in the same order as in this.
|
||||
*/
|
||||
fun Array<String>.intersect(
|
||||
other: Array<String>,
|
||||
@ -125,10 +125,10 @@ fun Array<String>.intersect(
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if there is an element in [this] that is also in [other]. This
|
||||
* method terminates if any intersection is found. The sizes of both arguments are assumed to be
|
||||
* so small, and the likelihood of an intersection so great, that it is not worth the CPU cost of
|
||||
* sorting or the memory cost of hashing.
|
||||
* Returns true if there is an element in this array that is also in [other]. This method terminates
|
||||
* if any intersection is found. The sizes of both arguments are assumed to be so small, and the
|
||||
* likelihood of an intersection so great, that it is not worth the CPU cost of sorting or the
|
||||
* memory cost of hashing.
|
||||
*/
|
||||
fun Array<String>.hasIntersection(
|
||||
other: Array<String>?,
|
||||
@ -171,7 +171,7 @@ fun Array<String>.concat(value: String): Array<String> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments [startIndex] until [this] is not ASCII whitespace. Stops at [endIndex].
|
||||
* Increments [startIndex] until this string is not ASCII whitespace. Stops at [endIndex].
|
||||
*/
|
||||
fun String.indexOfFirstNonAsciiWhitespace(startIndex: Int = 0, endIndex: Int = length): Int {
|
||||
for (i in startIndex until endIndex) {
|
||||
@ -204,8 +204,8 @@ fun String.trimSubstring(startIndex: Int = 0, endIndex: Int = length): String {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the first character in [this] that contains a character in [delimiters].
|
||||
* Returns endIndex if there is no such character.
|
||||
* Returns the index of the first character in this string that contains a character in
|
||||
* [delimiters]. Returns endIndex if there is no such character.
|
||||
*/
|
||||
fun String.delimiterOffset(delimiters: String, startIndex: Int = 0, endIndex: Int = length): Int {
|
||||
for (i in startIndex until endIndex) {
|
||||
@ -215,8 +215,8 @@ fun String.delimiterOffset(delimiters: String, startIndex: Int = 0, endIndex: In
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the first character in [this] that is [delimiter]. Returns
|
||||
* endIndex if there is no such character.
|
||||
* Returns the index of the first character in this string that is [delimiter]. Returns [endIndex]
|
||||
* if there is no such character.
|
||||
*/
|
||||
fun String.delimiterOffset(delimiter: Char, startIndex: Int = 0, endIndex: Int = length): Int {
|
||||
for (i in startIndex until endIndex) {
|
||||
@ -226,9 +226,8 @@ fun String.delimiterOffset(delimiter: Char, startIndex: Int = 0, endIndex: Int =
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the first character in [this] that is either a control character
|
||||
* (like `\u0000` or `\n`) or a non-ASCII character. Returns -1 if [this] has no such
|
||||
* characters.
|
||||
* Returns the index of the first character in this string that is either a control character (like
|
||||
* `\u0000` or `\n`) or a non-ASCII character. Returns -1 if this string has no such characters.
|
||||
*/
|
||||
fun String.indexOfControlOrNonAscii(): Int {
|
||||
for (i in 0 until length) {
|
||||
@ -240,7 +239,7 @@ fun String.indexOfControlOrNonAscii(): Int {
|
||||
return -1
|
||||
}
|
||||
|
||||
/** Returns true if [this] is not a host name and might be an IP address. */
|
||||
/** Returns true if this string is not a host name and might be an IP address. */
|
||||
fun String.canParseAsIpAddress(): Boolean {
|
||||
return VERIFY_AS_IP_ADDRESS.matches(this)
|
||||
}
|
||||
@ -291,7 +290,7 @@ fun Headers.toHeaderList(): List<Header> = (0 until size).map {
|
||||
Header(name(it), value(it))
|
||||
}
|
||||
|
||||
/** Returns true if an HTTP request for [this] and [other] can reuse a connection. */
|
||||
/** Returns true if an HTTP request for this URL and [other] can reuse a connection. */
|
||||
fun HttpUrl.canReuseConnectionFor(other: HttpUrl): Boolean = host == other.host &&
|
||||
port == other.port &&
|
||||
scheme == other.scheme
|
||||
|
@ -779,7 +779,7 @@ public final class CacheTest {
|
||||
|
||||
private RequestBody requestBodyOrNull(String requestMethod) {
|
||||
return (requestMethod.equals("POST") || requestMethod.equals("PUT"))
|
||||
? RequestBody.create(MediaType.get("text/plain"), "foo")
|
||||
? RequestBody.create("foo", MediaType.get("text/plain"))
|
||||
: null;
|
||||
}
|
||||
|
||||
@ -867,7 +867,7 @@ public final class CacheTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.put(RequestBody.create(MediaType.get("text/plain"), "foo"))
|
||||
.put(RequestBody.create("foo", MediaType.get("text/plain")))
|
||||
.build();
|
||||
Response invalidate = client.newCall(request).execute();
|
||||
assertThat(invalidate.body().string()).isEqualTo("");
|
||||
|
@ -220,7 +220,7 @@ public final class CallTest {
|
||||
server.enqueue(new MockResponse());
|
||||
|
||||
try {
|
||||
new Request.Builder().method("GET", RequestBody.create(MediaType.get("text/plain"), "abc"));
|
||||
new Request.Builder().method("GET", RequestBody.create("abc", MediaType.get("text/plain")));
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
@ -314,7 +314,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "def"))
|
||||
.post(RequestBody.create("def", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -344,7 +344,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method("POST", RequestBody.create(null, new byte[0]))
|
||||
.method("POST", RequestBody.create(new byte[0], null))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -403,7 +403,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method("POST", RequestBody.create(null, body))
|
||||
.method("POST", RequestBody.create(body, null))
|
||||
.build();
|
||||
|
||||
String credential = Credentials.basic("jesse", "secret");
|
||||
@ -516,7 +516,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method("DELETE", RequestBody.create(MediaType.get("text/plain"), "def"))
|
||||
.method("DELETE", RequestBody.create("def", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -533,7 +533,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.put(RequestBody.create(MediaType.get("text/plain"), "def"))
|
||||
.put(RequestBody.create("def", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -563,7 +563,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.patch(RequestBody.create(MediaType.get("text/plain"), "def"))
|
||||
.patch(RequestBody.create("def", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -593,7 +593,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method("CUSTOM", RequestBody.create(MediaType.get("text/plain"), "def"))
|
||||
.method("CUSTOM", RequestBody.create("def", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -613,7 +613,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method("POST", RequestBody.create(null, "abc"))
|
||||
.method("POST", RequestBody.create("abc", null))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request).assertCode(200);
|
||||
@ -1448,7 +1448,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "def"))
|
||||
.post(RequestBody.create("def", MediaType.get("text/plain")))
|
||||
.build();
|
||||
client.newCall(request).enqueue(callback);
|
||||
|
||||
@ -1475,7 +1475,7 @@ public final class CallTest {
|
||||
|
||||
Request request2 = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "body!"))
|
||||
.post(RequestBody.create("body!", MediaType.get("text/plain")))
|
||||
.build();
|
||||
Response response2 = client.newCall(request2).execute();
|
||||
assertThat(response2.body().string()).isEqualTo("def");
|
||||
@ -1793,7 +1793,7 @@ public final class CallTest {
|
||||
|
||||
Response response = client.newCall(new Request.Builder()
|
||||
.url(server.url("/page1"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "Request Body"))
|
||||
.post(RequestBody.create("Request Body", MediaType.get("text/plain")))
|
||||
.build()).execute();
|
||||
assertThat(response.body().string()).isEqualTo("Page 2");
|
||||
|
||||
@ -1847,7 +1847,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "Hello"))
|
||||
.post(RequestBody.create("Hello", MediaType.get("text/plain")))
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
@ -2021,7 +2021,7 @@ public final class CallTest {
|
||||
// when
|
||||
Response response = client.newCall(new Request.Builder()
|
||||
.url(server.url("/page1"))
|
||||
.method("PROPFIND", RequestBody.create(MediaType.get("text/plain"), "Request Body"))
|
||||
.method("PROPFIND", RequestBody.create("Request Body", MediaType.get("text/plain")))
|
||||
.build()).execute();
|
||||
|
||||
// then
|
||||
@ -2680,7 +2680,7 @@ public final class CallTest {
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.header("Expect", "100-continue")
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -2696,7 +2696,7 @@ public final class CallTest {
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.header("Expect", "100-continue")
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), ""))
|
||||
.post(RequestBody.create("", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -2720,7 +2720,7 @@ public final class CallTest {
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.header("Expect", "100-continue")
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
Call call = client.newCall(request);
|
||||
@ -2745,7 +2745,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request)
|
||||
@ -2771,7 +2771,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
Call call = client.newCall(request);
|
||||
@ -2798,7 +2798,7 @@ public final class CallTest {
|
||||
executeSynchronously(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.header("Expect", "100-continue")
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build());
|
||||
executeSynchronously(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
@ -2820,7 +2820,7 @@ public final class CallTest {
|
||||
executeSynchronously(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.header("Expect", "100-continue")
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build());
|
||||
executeSynchronously(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
@ -2841,7 +2841,7 @@ public final class CallTest {
|
||||
executeSynchronously(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.header("Expect", "100-continue")
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build());
|
||||
executeSynchronously(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
@ -3369,7 +3369,7 @@ public final class CallTest {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
executeSynchronously(request);
|
||||
@ -3761,7 +3761,7 @@ public final class CallTest {
|
||||
}
|
||||
};
|
||||
return response.newBuilder()
|
||||
.body(ResponseBody.create(null, -1L, Okio.buffer(closeTrackingSource)))
|
||||
.body(ResponseBody.create(Okio.buffer(closeTrackingSource), null, -1L))
|
||||
.build();
|
||||
}
|
||||
})
|
||||
|
@ -194,7 +194,7 @@ public final class ConnectionReuseTest {
|
||||
|
||||
Request requestB = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "b"))
|
||||
.post(RequestBody.create("b", MediaType.get("text/plain")))
|
||||
.build();
|
||||
Response responseB = client.newCall(requestB).execute();
|
||||
assertThat(responseB.body().string()).isEqualTo("b");
|
||||
@ -314,7 +314,7 @@ public final class ConnectionReuseTest {
|
||||
responsesNotClosed.add(response);
|
||||
return response
|
||||
.newBuilder()
|
||||
.body(ResponseBody.create(null, "unrelated response body!"))
|
||||
.body(ResponseBody.create("unrelated response body!", null))
|
||||
.build();
|
||||
})
|
||||
.build();
|
||||
|
@ -1075,7 +1075,7 @@ public final class EventListenerTest {
|
||||
@Test public void requestBodySuccessHttp1OverHttps() throws IOException {
|
||||
enableTlsWithTunnel(false);
|
||||
server.setProtocols(asList(Protocol.HTTP_1_1));
|
||||
requestBodySuccess(RequestBody.create(MediaType.get("text/plain"), "Hello"), equalTo(5L),
|
||||
requestBodySuccess(RequestBody.create("Hello", MediaType.get("text/plain")), equalTo(5L),
|
||||
equalTo(19L));
|
||||
}
|
||||
|
||||
@ -1084,12 +1084,12 @@ public final class EventListenerTest {
|
||||
|
||||
enableTlsWithTunnel(false);
|
||||
server.setProtocols(asList(Protocol.HTTP_2, Protocol.HTTP_1_1));
|
||||
requestBodySuccess(RequestBody.create(MediaType.get("text/plain"), "Hello"), equalTo(5L),
|
||||
requestBodySuccess(RequestBody.create("Hello", MediaType.get("text/plain")), equalTo(5L),
|
||||
equalTo(19L));
|
||||
}
|
||||
|
||||
@Test public void requestBodySuccessHttp() throws IOException {
|
||||
requestBodySuccess(RequestBody.create(MediaType.get("text/plain"), "Hello"), equalTo(5L),
|
||||
requestBodySuccess(RequestBody.create("Hello", MediaType.get("text/plain")), equalTo(5L),
|
||||
equalTo(19L));
|
||||
}
|
||||
|
||||
@ -1109,7 +1109,7 @@ public final class EventListenerTest {
|
||||
}
|
||||
|
||||
@Test public void requestBodySuccessEmpty() throws IOException {
|
||||
requestBodySuccess(RequestBody.create(MediaType.get("text/plain"), ""), equalTo(0L),
|
||||
requestBodySuccess(RequestBody.create("", MediaType.get("text/plain")), equalTo(0L),
|
||||
equalTo(19L));
|
||||
}
|
||||
|
||||
@ -1232,7 +1232,7 @@ public final class EventListenerTest {
|
||||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("OK")
|
||||
.body(ResponseBody.create(null, "a"))
|
||||
.body(ResponseBody.create("a", null))
|
||||
.build())
|
||||
.build();
|
||||
|
||||
@ -1251,7 +1251,7 @@ public final class EventListenerTest {
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.header("Expect", "100-continue")
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.post(RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
Call call = client.newCall(request);
|
||||
|
@ -63,7 +63,7 @@ public final class InterceptorTest {
|
||||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("Intercepted!")
|
||||
.body(ResponseBody.create(MediaType.get("text/plain; charset=utf-8"), "abc"))
|
||||
.body(ResponseBody.create("abc", MediaType.get("text/plain; charset=utf-8")))
|
||||
.build();
|
||||
|
||||
client = client.newBuilder()
|
||||
@ -82,7 +82,7 @@ public final class InterceptorTest {
|
||||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(200)
|
||||
.message("Intercepted!")
|
||||
.body(ResponseBody.create(MediaType.get("text/plain; charset=utf-8"), "abc"))
|
||||
.body(ResponseBody.create("abc", MediaType.get("text/plain; charset=utf-8")))
|
||||
.build();
|
||||
client = client.newBuilder()
|
||||
.addNetworkInterceptor(interceptor)
|
||||
@ -215,7 +215,7 @@ public final class InterceptorTest {
|
||||
Interceptor interceptor = chain -> {
|
||||
Request originalRequest = chain.request();
|
||||
MediaType mediaType = MediaType.get("text/plain");
|
||||
RequestBody body = RequestBody.create(mediaType, "abc");
|
||||
RequestBody body = RequestBody.create("abc", mediaType);
|
||||
return chain.proceed(originalRequest.newBuilder()
|
||||
.method("POST", body)
|
||||
.header("Content-Type", mediaType.toString())
|
||||
@ -260,7 +260,7 @@ public final class InterceptorTest {
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.addHeader("Original-Header", "foo")
|
||||
.method("PUT", RequestBody.create(MediaType.get("text/plain"), "abc"))
|
||||
.method("PUT", RequestBody.create("abc", MediaType.get("text/plain")))
|
||||
.build();
|
||||
|
||||
client.newCall(request).execute();
|
||||
@ -774,7 +774,7 @@ public final class InterceptorTest {
|
||||
byte[] data = new byte[2 * 1024 * 1024]; // 2 MiB.
|
||||
Request request1 = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain"), data))
|
||||
.post(RequestBody.create(data, MediaType.get("text/plain")))
|
||||
.build();
|
||||
Call call = client.newCall(request1);
|
||||
|
||||
@ -845,8 +845,8 @@ public final class InterceptorTest {
|
||||
}
|
||||
|
||||
static ResponseBody uppercase(ResponseBody original) throws IOException {
|
||||
return ResponseBody.create(original.contentType(), original.contentLength(),
|
||||
Okio.buffer(uppercase(original.source())));
|
||||
return ResponseBody.create(Okio.buffer(uppercase(original.source())),
|
||||
original.contentType(), original.contentLength());
|
||||
}
|
||||
|
||||
private static Source uppercase(Source original) {
|
||||
|
@ -43,7 +43,7 @@ public final class MultipartBodyTest {
|
||||
+ "--123--\r\n";
|
||||
|
||||
MultipartBody body = new MultipartBody.Builder("123")
|
||||
.addPart(RequestBody.create(null, "Hello, World!"))
|
||||
.addPart(RequestBody.create("Hello, World!", null))
|
||||
.build();
|
||||
|
||||
assertThat(body.boundary()).isEqualTo("123");
|
||||
@ -75,9 +75,9 @@ public final class MultipartBodyTest {
|
||||
+ "--123--\r\n";
|
||||
|
||||
MultipartBody body = new MultipartBody.Builder("123")
|
||||
.addPart(RequestBody.create(null, "Quick"))
|
||||
.addPart(RequestBody.create(null, "Brown"))
|
||||
.addPart(RequestBody.create(null, "Fox"))
|
||||
.addPart(RequestBody.create("Quick", null))
|
||||
.addPart(RequestBody.create("Brown", null))
|
||||
.addPart(RequestBody.create("Fox", null))
|
||||
.build();
|
||||
|
||||
assertThat(body.boundary()).isEqualTo("123");
|
||||
@ -129,14 +129,14 @@ public final class MultipartBodyTest {
|
||||
.addPart(
|
||||
Headers.of("Content-Disposition", "file; filename=\"file1.txt\""),
|
||||
RequestBody.create(
|
||||
MediaType.get("text/plain"), "... contents of file1.txt ..."))
|
||||
"... contents of file1.txt ...", MediaType.get("text/plain")))
|
||||
.addPart(
|
||||
Headers.of(
|
||||
"Content-Disposition", "file; filename=\"file2.gif\"",
|
||||
"Content-Transfer-Encoding", "binary"),
|
||||
RequestBody.create(
|
||||
MediaType.get("image/gif"),
|
||||
"... contents of file2.gif ...".getBytes(UTF_8)))
|
||||
"... contents of file2.gif ...".getBytes(UTF_8),
|
||||
MediaType.get("image/gif")))
|
||||
.build())
|
||||
.build();
|
||||
|
||||
@ -181,7 +181,7 @@ public final class MultipartBodyTest {
|
||||
MultipartBody body = new MultipartBody.Builder("AaB03x")
|
||||
.setType(MultipartBody.FORM)
|
||||
.addFormDataPart("field with spaces", "filename with spaces.txt",
|
||||
RequestBody.create(MediaType.get("text/plain; charset=utf-8"), "okay"))
|
||||
RequestBody.create("okay", MediaType.get("text/plain; charset=utf-8")))
|
||||
.addFormDataPart("field with \"", "\"")
|
||||
.addFormDataPart("field with %22", "%22")
|
||||
.addFormDataPart("field with \u007e", "Alpha")
|
||||
@ -224,9 +224,9 @@ public final class MultipartBodyTest {
|
||||
+ "--123--\r\n";
|
||||
|
||||
MultipartBody body = new MultipartBody.Builder("123")
|
||||
.addPart(RequestBody.create(null, "Quick"))
|
||||
.addPart(RequestBody.create("Quick", null))
|
||||
.addPart(new StreamingBody("Brown"))
|
||||
.addPart(RequestBody.create(null, "Fox"))
|
||||
.addPart(RequestBody.create("Fox", null))
|
||||
.build();
|
||||
|
||||
assertThat(body.boundary()).isEqualTo("123");
|
||||
@ -244,7 +244,7 @@ public final class MultipartBodyTest {
|
||||
MultipartBody.Builder multipart = new MultipartBody.Builder();
|
||||
try {
|
||||
multipart.addPart(Headers.of("Content-Type", "text/plain"),
|
||||
RequestBody.create(null, "Hello, World!"));
|
||||
RequestBody.create("Hello, World!", null));
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
@ -254,7 +254,7 @@ public final class MultipartBodyTest {
|
||||
MultipartBody.Builder multipart = new MultipartBody.Builder();
|
||||
try {
|
||||
multipart.addPart(Headers.of("Content-Length", "13"),
|
||||
RequestBody.create(null, "Hello, World!"));
|
||||
RequestBody.create("Hello, World!", null));
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
@ -262,7 +262,7 @@ public final class MultipartBodyTest {
|
||||
|
||||
@Test public void partAccessors() throws IOException {
|
||||
MultipartBody body = new MultipartBody.Builder()
|
||||
.addPart(Headers.of("Foo", "Bar"), RequestBody.create(null, "Baz"))
|
||||
.addPart(Headers.of("Foo", "Bar"), RequestBody.create("Baz", null))
|
||||
.build();
|
||||
assertThat(body.parts().size()).isEqualTo(1);
|
||||
|
||||
@ -286,7 +286,7 @@ public final class MultipartBodyTest {
|
||||
MultipartBody body = new MultipartBody.Builder("AaB03x")
|
||||
.setType(MultipartBody.FORM)
|
||||
.addFormDataPart("attachment", "resumé.pdf",
|
||||
RequestBody.create(MediaType.parse("application/pdf"), "Jesse’s Resumé"))
|
||||
RequestBody.create("Jesse’s Resumé", MediaType.parse("application/pdf")))
|
||||
.build();
|
||||
|
||||
Buffer buffer = new Buffer();
|
||||
|
@ -31,7 +31,7 @@ import static org.junit.Assert.fail;
|
||||
public final class RequestTest {
|
||||
@Test public void string() throws Exception {
|
||||
MediaType contentType = MediaType.get("text/plain; charset=utf-8");
|
||||
RequestBody body = RequestBody.create(contentType, "abc".getBytes(UTF_8));
|
||||
RequestBody body = RequestBody.create("abc".getBytes(UTF_8), contentType);
|
||||
assertThat(body.contentType()).isEqualTo(contentType);
|
||||
assertThat(body.contentLength()).isEqualTo(3);
|
||||
assertThat(bodyToHex(body)).isEqualTo("616263");
|
||||
@ -41,7 +41,7 @@ public final class RequestTest {
|
||||
|
||||
@Test public void stringWithDefaultCharsetAdded() throws Exception {
|
||||
MediaType contentType = MediaType.get("text/plain");
|
||||
RequestBody body = RequestBody.create(contentType, "\u0800");
|
||||
RequestBody body = RequestBody.create("\u0800", contentType);
|
||||
assertThat(body.contentType()).isEqualTo(MediaType.get("text/plain; charset=utf-8"));
|
||||
assertThat(body.contentLength()).isEqualTo(3);
|
||||
assertThat(bodyToHex(body)).isEqualTo("e0a080");
|
||||
@ -49,7 +49,7 @@ public final class RequestTest {
|
||||
|
||||
@Test public void stringWithNonDefaultCharsetSpecified() throws Exception {
|
||||
MediaType contentType = MediaType.get("text/plain; charset=utf-16be");
|
||||
RequestBody body = RequestBody.create(contentType, "\u0800");
|
||||
RequestBody body = RequestBody.create("\u0800", contentType);
|
||||
assertThat(body.contentType()).isEqualTo(contentType);
|
||||
assertThat(body.contentLength()).isEqualTo(2);
|
||||
assertThat(bodyToHex(body)).isEqualTo("0800");
|
||||
@ -57,7 +57,7 @@ public final class RequestTest {
|
||||
|
||||
@Test public void byteArray() throws Exception {
|
||||
MediaType contentType = MediaType.get("text/plain");
|
||||
RequestBody body = RequestBody.create(contentType, "abc".getBytes(UTF_8));
|
||||
RequestBody body = RequestBody.create("abc".getBytes(UTF_8), contentType);
|
||||
assertThat(body.contentType()).isEqualTo(contentType);
|
||||
assertThat(body.contentLength()).isEqualTo(3);
|
||||
assertThat(bodyToHex(body)).isEqualTo("616263");
|
||||
@ -67,7 +67,7 @@ public final class RequestTest {
|
||||
|
||||
@Test public void byteArrayRange() throws Exception {
|
||||
MediaType contentType = MediaType.get("text/plain");
|
||||
RequestBody body = RequestBody.create(contentType, ".abcd".getBytes(UTF_8), 1, 3);
|
||||
RequestBody body = RequestBody.create(".abcd".getBytes(UTF_8), contentType, 1, 3);
|
||||
assertThat(body.contentType()).isEqualTo(contentType);
|
||||
assertThat(body.contentLength()).isEqualTo(3);
|
||||
assertThat(bodyToHex(body)).isEqualTo("616263");
|
||||
@ -77,7 +77,7 @@ public final class RequestTest {
|
||||
|
||||
@Test public void byteString() throws Exception {
|
||||
MediaType contentType = MediaType.get("text/plain");
|
||||
RequestBody body = RequestBody.create(contentType, ByteString.encodeUtf8("Hello"));
|
||||
RequestBody body = RequestBody.create(ByteString.encodeUtf8("Hello"), contentType);
|
||||
assertThat(body.contentType()).isEqualTo(contentType);
|
||||
assertThat(body.contentLength()).isEqualTo(5);
|
||||
assertThat(bodyToHex(body)).isEqualTo("48656c6c6f");
|
||||
@ -92,7 +92,7 @@ public final class RequestTest {
|
||||
writer.close();
|
||||
|
||||
MediaType contentType = MediaType.get("text/plain");
|
||||
RequestBody body = RequestBody.create(contentType, file);
|
||||
RequestBody body = RequestBody.create(file, contentType);
|
||||
assertThat(body.contentType()).isEqualTo(contentType);
|
||||
assertThat(body.contentLength()).isEqualTo(3);
|
||||
assertThat(bodyToHex(body)).isEqualTo("616263");
|
||||
@ -103,7 +103,7 @@ public final class RequestTest {
|
||||
/** Common verbs used for apis such as GitHub, AWS, and Google Cloud. */
|
||||
@Test public void crudVerbs() throws IOException {
|
||||
MediaType contentType = MediaType.get("application/json");
|
||||
RequestBody body = RequestBody.create(contentType, "{}");
|
||||
RequestBody body = RequestBody.create("{}", contentType);
|
||||
|
||||
Request get = new Request.Builder().url("http://localhost/api").get().build();
|
||||
assertThat(get.method()).isEqualTo("GET");
|
||||
|
@ -399,7 +399,7 @@ public final class ResponseBodyTest {
|
||||
|
||||
static ResponseBody body(String hex, String charset) {
|
||||
MediaType mediaType = charset == null ? null : MediaType.get("any/thing; charset=" + charset);
|
||||
return ResponseBody.create(mediaType, ByteString.decodeHex(hex));
|
||||
return ResponseBody.create(ByteString.decodeHex(hex), mediaType);
|
||||
}
|
||||
|
||||
static String exhaust(Reader reader) throws IOException {
|
||||
|
@ -85,7 +85,7 @@ public final class ResponseTest {
|
||||
}
|
||||
};
|
||||
|
||||
return ResponseBody.create(null, -1, Okio.buffer(source));
|
||||
return ResponseBody.create(Okio.buffer(source), null, -1);
|
||||
}
|
||||
|
||||
private Response newResponse(ResponseBody responseBody) {
|
||||
|
@ -1597,7 +1597,7 @@ public final class URLConnectionTest {
|
||||
|
||||
Response response = getResponse(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(null, body))
|
||||
.post(RequestBody.create(body, null))
|
||||
.build());
|
||||
assertThat(response.code()).isEqualTo(200);
|
||||
response.body().byteStream().close();
|
||||
@ -1715,7 +1715,7 @@ public final class URLConnectionTest {
|
||||
private void assertMethodPermitsRequestBody(String requestMethod) {
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method(requestMethod, RequestBody.create(null, "abc"))
|
||||
.method(requestMethod, RequestBody.create("abc", null))
|
||||
.build();
|
||||
assertThat(request.method()).isEqualTo(requestMethod);
|
||||
}
|
||||
@ -1724,7 +1724,7 @@ public final class URLConnectionTest {
|
||||
try {
|
||||
new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method(requestMethod, RequestBody.create(null, "abc"))
|
||||
.method(requestMethod, RequestBody.create("abc", null))
|
||||
.build();
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
@ -1853,7 +1853,7 @@ public final class URLConnectionTest {
|
||||
.build();
|
||||
Response response = getResponse(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(null, "ABCD"))
|
||||
.post(RequestBody.create("ABCD", null))
|
||||
.build());
|
||||
assertThat(readAscii(response.body().byteStream(), Integer.MAX_VALUE)).isEqualTo(
|
||||
"Successful auth!");
|
||||
@ -2291,7 +2291,7 @@ public final class URLConnectionTest {
|
||||
|
||||
Response response = getResponse(new Request.Builder()
|
||||
.url(server.url("/page1"))
|
||||
.post(RequestBody.create(MediaType.get("text/plain; charset=utf-8"), "ABCD"))
|
||||
.post(RequestBody.create("ABCD", MediaType.get("text/plain; charset=utf-8")))
|
||||
.header("Transfer-Encoding", "identity")
|
||||
.build());
|
||||
assertThat(readAscii(response.body().byteStream(), Integer.MAX_VALUE)).isEqualTo(
|
||||
@ -2371,7 +2371,7 @@ public final class URLConnectionTest {
|
||||
Request.Builder requestBuilder = new Request.Builder()
|
||||
.url(server.url("/page1"));
|
||||
if (method.equals("POST")) {
|
||||
requestBuilder.post(RequestBody.create(null, "ABCD"));
|
||||
requestBuilder.post(RequestBody.create("ABCD", null));
|
||||
} else {
|
||||
requestBuilder.method(method, null);
|
||||
}
|
||||
@ -2488,7 +2488,7 @@ public final class URLConnectionTest {
|
||||
|
||||
Response response = getResponse(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(null, "Hello"))
|
||||
.post(RequestBody.create("Hello", null))
|
||||
.build());
|
||||
|
||||
assertThat(response.code()).isEqualTo(200);
|
||||
@ -2820,7 +2820,7 @@ public final class URLConnectionTest {
|
||||
try {
|
||||
new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method("GET", RequestBody.create(null, "abc"))
|
||||
.method("GET", RequestBody.create("abc", null))
|
||||
.build();
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
@ -2832,7 +2832,7 @@ public final class URLConnectionTest {
|
||||
.setBody("A"));
|
||||
Response response = getResponse(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(null, "ABC"))
|
||||
.post(RequestBody.create("ABC", null))
|
||||
.build());
|
||||
assertThat(readAscii(response.body().byteStream(), Integer.MAX_VALUE)).isEqualTo(
|
||||
"A");
|
||||
@ -2884,7 +2884,7 @@ public final class URLConnectionTest {
|
||||
try {
|
||||
new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method("HEAD", RequestBody.create(null, ""))
|
||||
.method("HEAD", RequestBody.create("", null))
|
||||
.build();
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
@ -2998,7 +2998,7 @@ public final class URLConnectionTest {
|
||||
|
||||
Response post = getResponse(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(null, "body!"))
|
||||
.post(RequestBody.create("body!", null))
|
||||
.build());
|
||||
assertContent("def", post);
|
||||
|
||||
@ -3357,7 +3357,7 @@ public final class URLConnectionTest {
|
||||
|
||||
Response response = getResponse(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.method(method, RequestBody.create(null, ""))
|
||||
.method(method, RequestBody.create("", null))
|
||||
.build());
|
||||
assertContent("", response);
|
||||
RecordedRequest zeroLengthPayload = server.takeRequest();
|
||||
@ -3522,7 +3522,7 @@ public final class URLConnectionTest {
|
||||
|
||||
Response response = getResponse(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.delete(RequestBody.create(null, "BODY"))
|
||||
.delete(RequestBody.create("BODY", null))
|
||||
.build());
|
||||
assertThat(response.code()).isEqualTo(200);
|
||||
|
||||
@ -3632,7 +3632,7 @@ public final class URLConnectionTest {
|
||||
|
||||
assertContent("def", getResponse(new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.post(RequestBody.create(null, "123"))
|
||||
.post(RequestBody.create("123", null))
|
||||
.build()));
|
||||
|
||||
RecordedRequest request1 = server.takeRequest();
|
||||
|
@ -75,7 +75,7 @@ public final class WebSocketWriterTest {
|
||||
int length = 5;
|
||||
byte[] bytes = binaryData(length);
|
||||
|
||||
RequestBody body = RequestBody.create(null, bytes);
|
||||
RequestBody body = RequestBody.create(bytes, null);
|
||||
BufferedSink sink = Okio.buffer(serverWriter.newMessageSink(OPCODE_TEXT, length));
|
||||
body.writeTo(sink);
|
||||
sink.close();
|
||||
@ -89,7 +89,7 @@ public final class WebSocketWriterTest {
|
||||
int length = 12345;
|
||||
byte[] bytes = binaryData(length);
|
||||
|
||||
RequestBody body = RequestBody.create(null, bytes);
|
||||
RequestBody body = RequestBody.create(bytes, null);
|
||||
BufferedSink sink = Okio.buffer(serverWriter.newMessageSink(OPCODE_TEXT, length));
|
||||
body.writeTo(sink);
|
||||
sink.close();
|
||||
|
@ -13,7 +13,7 @@ public class PostExample {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
String post(String url, String json) throws IOException {
|
||||
RequestBody body = RequestBody.create(JSON, json);
|
||||
RequestBody body = RequestBody.create(json, JSON);
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
|
@ -34,7 +34,7 @@ public final class PostFile {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.github.com/markdown/raw")
|
||||
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
|
||||
.post(RequestBody.create(file, MEDIA_TYPE_MARKDOWN))
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
|
@ -40,7 +40,7 @@ public final class PostMultipart {
|
||||
.setType(MultipartBody.FORM)
|
||||
.addFormDataPart("title", "Square Logo")
|
||||
.addFormDataPart("image", "logo-square.png",
|
||||
RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
|
||||
RequestBody.create(new File("website/static/logo-square.png"), MEDIA_TYPE_PNG))
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
|
@ -39,7 +39,7 @@ public final class PostString {
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url("https://api.github.com/markdown/raw")
|
||||
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
|
||||
.post(RequestBody.create(postBody, MEDIA_TYPE_MARKDOWN))
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
|
@ -52,7 +52,7 @@ public final class RequestBodyCompression {
|
||||
Map<String, String> requestBody = new LinkedHashMap<>();
|
||||
requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/");
|
||||
RequestBody jsonRequestBody = RequestBody.create(
|
||||
MEDIA_TYPE_JSON, mapJsonAdapter.toJson(requestBody));
|
||||
mapJsonAdapter.toJson(requestBody), MEDIA_TYPE_JSON);
|
||||
Request request = new Request.Builder()
|
||||
.url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY)
|
||||
.post(jsonRequestBody)
|
||||
|
Reference in New Issue
Block a user