/** * Removes all headers named {@code name}, then adds a new header with the name and value. */ public MockResponse setHeader(String name, Object value) { removeHeader(name); return addHeader(name, value); }
/** * Sets the response body to {@code body}, chunked every {@code maxChunkSize} bytes. */ public MockResponse setChunkedBody(Buffer body, int maxChunkSize) { removeHeader("Content-Length"); headers.add(CHUNKED_BODY_HEADER); Buffer bytesOut = new Buffer(); while (!body.exhausted()) { long chunkSize = Math.min(body.size(), maxChunkSize); bytesOut.writeHexadecimalUnsignedLong(chunkSize); bytesOut.writeUtf8("\r\n"); bytesOut.write(body, chunkSize); bytesOut.writeUtf8("\r\n"); } bytesOut.writeUtf8("0\r\n"); // Last chunk. Trailers follow! this.body = bytesOut; return this; }
/** * Removes all headers named {@code name}, then adds a new header with the name and value. */ public MockResponse setHeader(String name, Object value) { removeHeader(name); return addHeader(name, value); }
/** * Sets the response body to {@code body}, chunked every {@code maxChunkSize} bytes. */ public MockResponse setChunkedBody(Buffer body, int maxChunkSize) { removeHeader("Content-Length"); headers.add(CHUNKED_BODY_HEADER); Buffer bytesOut = new Buffer(); while (!body.exhausted()) { long chunkSize = Math.min(body.size(), maxChunkSize); bytesOut.writeHexadecimalUnsignedLong(chunkSize); bytesOut.writeUtf8("\r\n"); bytesOut.write(body, chunkSize); bytesOut.writeUtf8("\r\n"); } bytesOut.writeUtf8("0\r\n\r\n"); // Last chunk + empty trailer + CRLF. this.body = bytesOut; return this; }
@Test public void successfulRequestResponseWhenMimeTypeMissing() throws Exception { Retrofit retrofit = new Retrofit.Builder() .baseUrl(mServer.url("/")) .addConverterFactory(new ToStringConverterFactory()) .addCallAdapterFactory(buildSmartCacheFactory()) .build(); Service example = retrofit.create(Service.class); mServer.enqueue(new MockResponse().setBody("Hi").removeHeader("Content-Type")); Response<String> response = example.getString().execute(); assertThat(response.body()).isEqualTo("Hi"); }