use of okhttp3.MediaType in project okhttp by square.
the class HttpOverHttp2Test method closeAfterFlush.
@Test
public void closeAfterFlush() throws Exception {
final byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8);
server.enqueue(new MockResponse().setBody("ABCDE"));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).post(new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain; charset=utf-8");
}
@Override
public long contentLength() throws IOException {
return postBytes.length;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
// push bytes into the stream's buffer
sink.write(postBytes);
// Http2Connection.writeData subject to write window
sink.flush();
// Http2Connection.writeData empty frame
sink.close();
}
}).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
RecordedRequest request = server.takeRequest();
assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
assertArrayEquals(postBytes, request.getBody().readByteArray());
assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
}
use of okhttp3.MediaType in project okhttp by square.
the class HttpOverHttp2Test method userSuppliedContentLengthHeader.
@Test
public void userSuppliedContentLengthHeader() throws Exception {
final byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8);
server.enqueue(new MockResponse().setBody("ABCDE"));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).post(new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain; charset=utf-8");
}
@Override
public long contentLength() throws IOException {
return postBytes.length;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write(postBytes);
}
}).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
RecordedRequest request = server.takeRequest();
assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
assertArrayEquals(postBytes, request.getBody().readByteArray());
assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
}
use of okhttp3.MediaType in project picasso by square.
the class NetworkRequestHandlerTest method unknownContentLengthFromDiskThrows.
@Test
public void unknownContentLengthFromDiskThrows() throws Exception {
final AtomicBoolean closed = new AtomicBoolean();
ResponseBody body = new ResponseBody() {
@Override
public MediaType contentType() {
return null;
}
@Override
public long contentLength() {
return 0;
}
@Override
public BufferedSource source() {
return new Buffer();
}
@Override
public void close() {
closed.set(true);
super.close();
}
};
responses.add(responseOf(body).newBuilder().cacheResponse(responseOf(null)).build());
Action action = TestUtils.mockAction(URI_KEY_1, URI_1);
try {
networkHandler.load(action.getRequest(), 0);
fail();
} catch (IOException expected) {
verifyZeroInteractions(stats);
assertTrue(closed.get());
}
}
use of okhttp3.MediaType in project camel by apache.
the class MultiPartFormOkHttpTest method createMultipartRequest.
private Request createMultipartRequest() throws Exception {
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
RequestBody body = RequestBody.create(mediaType, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\nsome data here\r\n-----011000010111000001101001--");
Request request = new Request.Builder().url("http://localhost:" + getPort() + "/test").post(body).addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001").addHeader("cache-control", "no-cache").addHeader("postman-token", "a9fd95b6-04b9-ea7a-687e-ff828ea00774").build();
return request;
}
use of okhttp3.MediaType in project WeexErosFramework by bmfe.
the class WeexOkhttp3Interceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
String requestId = String.valueOf(mNextRequestId.getAndIncrement());
Request request = chain.request();
mEventReporter = NetworkEventReporterManager.get();
RequestBodyHelper requestBodyHelper = null;
if (mEventReporter != null) {
requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
mEventReporter.requestWillBeSent(inspectorRequest);
}
Response response;
try {
response = chain.proceed(request);
} catch (IOException e) {
if (mEventReporter != null) {
mEventReporter.httpExchangeFailed(requestId, e.toString());
}
throw e;
}
if (mEventReporter != null) {
if (requestBodyHelper.hasBody()) {
requestBodyHelper.reportDataSent();
}
Connection connection = chain.connection();
mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response));
ResponseBody body = response.body();
MediaType contentType = null;
InputStream responseStream = null;
if (body != null) {
contentType = body.contentType();
responseStream = body.byteStream();
}
responseStream = mEventReporter.interpretResponseStream(requestId, contentType != null ? contentType.toString() : null, response.header("Content-Encoding"), responseStream, new DefaultResponseHandler(mEventReporter, requestId));
if (responseStream != null) {
response = response.newBuilder().body(new ForwardingResponseBody(body, responseStream)).build();
}
}
return response;
}
Aggregations