1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package tests.http; 18 19 import static tests.http.MockWebServer.ASCII; 20 21 import java.io.ByteArrayOutputStream; 22 import java.io.IOException; 23 import java.io.UnsupportedEncodingException; 24 import java.util.ArrayList; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 29 /** 30 * A scripted response to be replayed by the mock web server. 31 */ 32 public class MockResponse { 33 private static final byte[] EMPTY_BODY = new byte[0]; 34 35 private String status = "HTTP/1.1 200 OK"; 36 private Map<String, String> headers = new HashMap<String, String>(); 37 private byte[] body = EMPTY_BODY; 38 private boolean closeConnectionAfter = false; 39 MockResponse()40 public MockResponse() { 41 addHeader("Content-Length", 0); 42 } 43 44 /** 45 * Returns the HTTP response line, such as "HTTP/1.1 200 OK". 46 */ getStatus()47 public String getStatus() { 48 return status; 49 } 50 setResponseCode(int code)51 public MockResponse setResponseCode(int code) { 52 this.status = "HTTP/1.1 " + code + " OK"; 53 return this; 54 } 55 56 /** 57 * Returns the HTTP headers, such as "Content-Length: 0". 58 */ getHeaders()59 public List<String> getHeaders() { 60 List<String> headerStrings = new ArrayList<String>(); 61 for (String header : headers.keySet()) { 62 headerStrings.add(header + ": " + headers.get(header)); 63 } 64 return headerStrings; 65 } 66 addHeader(String header, String value)67 public MockResponse addHeader(String header, String value) { 68 headers.put(header.toLowerCase(), value); 69 return this; 70 } 71 addHeader(String header, long value)72 public MockResponse addHeader(String header, long value) { 73 return addHeader(header, Long.toString(value)); 74 } 75 removeHeader(String header)76 public MockResponse removeHeader(String header) { 77 headers.remove(header.toLowerCase()); 78 return this; 79 } 80 81 /** 82 * Returns an input stream containing the raw HTTP payload. 83 */ getBody()84 public byte[] getBody() { 85 return body; 86 } 87 setBody(byte[] body)88 public MockResponse setBody(byte[] body) { 89 addHeader("Content-Length", body.length); 90 this.body = body; 91 return this; 92 } 93 setBody(String body)94 public MockResponse setBody(String body) { 95 try { 96 return setBody(body.getBytes(ASCII)); 97 } catch (UnsupportedEncodingException e) { 98 throw new AssertionError(); 99 } 100 } 101 setChunkedBody(byte[] body, int maxChunkSize)102 public MockResponse setChunkedBody(byte[] body, int maxChunkSize) throws IOException { 103 addHeader("Transfer-encoding", "chunked"); 104 105 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 106 int pos = 0; 107 while (pos < body.length) { 108 int chunkSize = Math.min(body.length - pos, maxChunkSize); 109 bytesOut.write(Integer.toHexString(chunkSize).getBytes(ASCII)); 110 bytesOut.write("\r\n".getBytes(ASCII)); 111 bytesOut.write(body, pos, chunkSize); 112 bytesOut.write("\r\n".getBytes(ASCII)); 113 pos += chunkSize; 114 } 115 bytesOut.write("0\r\n".getBytes(ASCII)); 116 this.body = bytesOut.toByteArray(); 117 return this; 118 } 119 setChunkedBody(String body, int maxChunkSize)120 public MockResponse setChunkedBody(String body, int maxChunkSize) throws IOException { 121 return setChunkedBody(body.getBytes(ASCII), maxChunkSize); 122 } 123 toString()124 @Override public String toString() { 125 return status; 126 } 127 shouldCloseConnectionAfter()128 public boolean shouldCloseConnectionAfter() { 129 return closeConnectionAfter; 130 } 131 setCloseConnectionAfter(boolean closeConnectionAfter)132 public MockResponse setCloseConnectionAfter(boolean closeConnectionAfter) { 133 this.closeConnectionAfter = closeConnectionAfter; 134 return this; 135 } 136 } 137