diff options
author | Steve Howard <showard@google.com> | 2010-06-17 14:56:48 -0700 |
---|---|---|
committer | Steve Howard <showard@google.com> | 2010-06-21 14:26:11 -0700 |
commit | 4c54154c83a5b3516d81903140b31349f196cbe2 (patch) | |
tree | 1b2b5d01da3e6a17377b5aba3f5b8ff4bb56d69b /tests/src/tests/http/MockResponse.java | |
parent | 055286d603745cb3281ddb04b0aa41a660624e65 (diff) | |
download | android_packages_providers_DownloadProvider-4c54154c83a5b3516d81903140b31349f196cbe2.tar.gz android_packages_providers_DownloadProvider-4c54154c83a5b3516d81903140b31349f196cbe2.tar.bz2 android_packages_providers_DownloadProvider-4c54154c83a5b3516d81903140b31349f196cbe2.zip |
Check in copies of MockWebServer + friends.
This commit includes unmodified copies of MockWebServer and its
supporting classes, taken from
dalvik-dev:libcore/support/src/tests/java. My test will require some
changes, but I'm committing unchanged versions first so I can more
easily track my changes and eventually merge them back over.
Change-Id: Ic4b79c7a2cbd3bd439864ed94c43fe985eae8e78
Diffstat (limited to 'tests/src/tests/http/MockResponse.java')
-rw-r--r-- | tests/src/tests/http/MockResponse.java | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/tests/src/tests/http/MockResponse.java b/tests/src/tests/http/MockResponse.java new file mode 100644 index 00000000..9893e2fc --- /dev/null +++ b/tests/src/tests/http/MockResponse.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tests.http; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import static tests.http.MockWebServer.ASCII; + +/** + * A scripted response to be replayed by the mock web server. + */ +public class MockResponse { + private static final String EMPTY_BODY_HEADER = "Content-Length: 0"; + private static final String CHUNKED_BODY_HEADER = "Transfer-encoding: chunked"; + private static final byte[] EMPTY_BODY = new byte[0]; + + private String status = "HTTP/1.1 200 OK"; + private List<String> headers = new ArrayList<String>(); + private byte[] body = EMPTY_BODY; + + public MockResponse() { + headers.add(EMPTY_BODY_HEADER); + } + + /** + * Returns the HTTP response line, such as "HTTP/1.1 200 OK". + */ + public String getStatus() { + return status; + } + + public MockResponse setResponseCode(int code) { + this.status = "HTTP/1.1 " + code + " OK"; + return this; + } + + /** + * Returns the HTTP headers, such as "Content-Length: 0". + */ + public List<String> getHeaders() { + return headers; + } + + public MockResponse addHeader(String header) { + headers.add(header); + return this; + } + + /** + * Returns an input stream containing the raw HTTP payload. + */ + public byte[] getBody() { + return body; + } + + public MockResponse setBody(byte[] body) { + if (this.body == EMPTY_BODY) { + headers.remove(EMPTY_BODY_HEADER); + } + this.headers.add("Content-Length: " + body.length); + this.body = body; + return this; + } + + public MockResponse setBody(String body) { + try { + return setBody(body.getBytes(ASCII)); + } catch (UnsupportedEncodingException e) { + throw new AssertionError(); + } + } + + public MockResponse setChunkedBody(byte[] body, int maxChunkSize) throws IOException { + headers.remove(EMPTY_BODY_HEADER); + headers.add(CHUNKED_BODY_HEADER); + + ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); + int pos = 0; + while (pos < body.length) { + int chunkSize = Math.min(body.length - pos, maxChunkSize); + bytesOut.write(Integer.toHexString(chunkSize).getBytes(ASCII)); + bytesOut.write("\r\n".getBytes(ASCII)); + bytesOut.write(body, pos, chunkSize); + bytesOut.write("\r\n".getBytes(ASCII)); + pos += chunkSize; + } + bytesOut.write("0\r\n".getBytes(ASCII)); + this.body = bytesOut.toByteArray(); + return this; + } + + public MockResponse setChunkedBody(String body, int maxChunkSize) throws IOException { + return setChunkedBody(body.getBytes(ASCII), maxChunkSize); + } + + @Override public String toString() { + return status; + } +} |