Home | History | Annotate | Download | only in http
      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 java.io.ByteArrayOutputStream;
     20 import java.io.IOException;
     21 import java.io.UnsupportedEncodingException;
     22 import java.util.ArrayList;
     23 import java.util.List;
     24 
     25 import static tests.http.MockWebServer.ASCII;
     26 
     27 /**
     28  * A scripted response to be replayed by the mock web server.
     29  */
     30 public class MockResponse {
     31     private static final String EMPTY_BODY_HEADER = "Content-Length: 0";
     32     private static final String CHUNKED_BODY_HEADER = "Transfer-encoding: chunked";
     33     private static final byte[] EMPTY_BODY = new byte[0];
     34 
     35     private String status = "HTTP/1.1 200 OK";
     36     private List<String> headers = new ArrayList<String>();
     37     private byte[] body = EMPTY_BODY;
     38     private boolean disconnectAtStart;
     39     private boolean disconnectAtEnd;
     40 
     41     public MockResponse() {
     42         headers.add(EMPTY_BODY_HEADER);
     43     }
     44 
     45     /**
     46      * Returns the HTTP response line, such as "HTTP/1.1 200 OK".
     47      */
     48     public String getStatus() {
     49         return status;
     50     }
     51 
     52     public MockResponse setResponseCode(int code) {
     53         this.status = "HTTP/1.1 " + code + " OK";
     54         return this;
     55     }
     56 
     57     public MockResponse setStatus(String status) {
     58         this.status = status;
     59         return this;
     60     }
     61 
     62     /**
     63      * Returns the HTTP headers, such as "Content-Length: 0".
     64      */
     65     public List<String> getHeaders() {
     66         return headers;
     67     }
     68 
     69     public MockResponse clearHeaders() {
     70         headers.clear();
     71         return this;
     72     }
     73 
     74     public MockResponse addHeader(String header) {
     75         headers.add(header);
     76         return this;
     77     }
     78 
     79     /**
     80      * Returns an input stream containing the raw HTTP payload.
     81      */
     82     public byte[] getBody() {
     83         return body;
     84     }
     85 
     86     public MockResponse setBody(byte[] body) {
     87         if (this.body == EMPTY_BODY) {
     88             headers.remove(EMPTY_BODY_HEADER);
     89         }
     90         this.headers.add("Content-Length: " + body.length);
     91         this.body = body;
     92         return this;
     93     }
     94 
     95     public MockResponse setBody(String body) {
     96         try {
     97             return setBody(body.getBytes(ASCII));
     98         } catch (UnsupportedEncodingException e) {
     99             throw new AssertionError();
    100         }
    101     }
    102 
    103     public MockResponse setChunkedBody(byte[] body, int maxChunkSize) throws IOException {
    104         headers.remove(EMPTY_BODY_HEADER);
    105         headers.add(CHUNKED_BODY_HEADER);
    106 
    107         ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    108         int pos = 0;
    109         while (pos < body.length) {
    110             int chunkSize = Math.min(body.length - pos, maxChunkSize);
    111             bytesOut.write(Integer.toHexString(chunkSize).getBytes(ASCII));
    112             bytesOut.write("\r\n".getBytes(ASCII));
    113             bytesOut.write(body, pos, chunkSize);
    114             bytesOut.write("\r\n".getBytes(ASCII));
    115             pos += chunkSize;
    116         }
    117         bytesOut.write("0\r\n\r\n".getBytes(ASCII)); // last chunk + empty trailer + crlf
    118         this.body = bytesOut.toByteArray();
    119         return this;
    120     }
    121 
    122     public MockResponse setChunkedBody(String body, int maxChunkSize) throws IOException {
    123         return setChunkedBody(body.getBytes(ASCII), maxChunkSize);
    124     }
    125 
    126     /**
    127      * Request immediate close of connection without even reading the
    128      * request.
    129      * <p>
    130      * Use to simulate the real life case of losing connection
    131      * because of bugger SSL server close connection when it seems
    132      * something like a compression method or TLS extension it doesn't
    133      * understand, instead of simply ignoring it like it should.
    134      */
    135     public MockResponse setDisconnectAtStart(boolean disconnectAtStart) {
    136         this.disconnectAtStart = disconnectAtStart;
    137         return this;
    138     }
    139 
    140     public boolean getDisconnectAtStart() {
    141         return disconnectAtStart;
    142     }
    143 
    144     /**
    145      * Request close of connection after the response. This is the
    146      * default HTTP/1.0 behavior.
    147      */
    148     public MockResponse setDisconnectAtEnd(boolean disconnectAtEnd) {
    149         this.disconnectAtEnd = disconnectAtEnd;
    150         return this;
    151     }
    152 
    153     public boolean getDisconnectAtEnd() {
    154         return disconnectAtEnd;
    155     }
    156 
    157     @Override public String toString() {
    158         return status;
    159     }
    160 }
    161