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