1 /* 2 * Copyright (C) 2011 Google Inc. 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 com.squareup.okhttp.mockwebserver; 18 19 import com.squareup.okhttp.Headers; 20 import com.squareup.okhttp.TlsVersion; 21 import java.net.Socket; 22 import java.util.List; 23 import javax.net.ssl.SSLSocket; 24 import okio.Buffer; 25 26 /** An HTTP request that came into the mock web server. */ 27 public final class RecordedRequest { 28 private final String requestLine; 29 private final String method; 30 private final String path; 31 private final Headers headers; 32 private final List<Integer> chunkSizes; 33 private final long bodySize; 34 private final Buffer body; 35 private final int sequenceNumber; 36 private final TlsVersion tlsVersion; 37 RecordedRequest(String requestLine, Headers headers, List<Integer> chunkSizes, long bodySize, Buffer body, int sequenceNumber, Socket socket)38 public RecordedRequest(String requestLine, Headers headers, List<Integer> chunkSizes, 39 long bodySize, Buffer body, int sequenceNumber, Socket socket) { 40 this.requestLine = requestLine; 41 this.headers = headers; 42 this.chunkSizes = chunkSizes; 43 this.bodySize = bodySize; 44 this.body = body; 45 this.sequenceNumber = sequenceNumber; 46 this.tlsVersion = socket instanceof SSLSocket 47 ? TlsVersion.forJavaName(((SSLSocket) socket).getSession().getProtocol()) 48 : null; 49 50 if (requestLine != null) { 51 int methodEnd = requestLine.indexOf(' '); 52 int pathEnd = requestLine.indexOf(' ', methodEnd + 1); 53 this.method = requestLine.substring(0, methodEnd); 54 this.path = requestLine.substring(methodEnd + 1, pathEnd); 55 } else { 56 this.method = null; 57 this.path = null; 58 } 59 } 60 getRequestLine()61 public String getRequestLine() { 62 return requestLine; 63 } 64 getMethod()65 public String getMethod() { 66 return method; 67 } 68 getPath()69 public String getPath() { 70 return path; 71 } 72 73 /** Returns all headers. */ getHeaders()74 public Headers getHeaders() { 75 return headers; 76 } 77 78 /** Returns the first header named {@code name}, or null if no such header exists. */ getHeader(String name)79 public String getHeader(String name) { 80 List<String> values = headers.values(name); 81 return values.isEmpty() ? null : values.get(0); 82 } 83 84 /** 85 * Returns the sizes of the chunks of this request's body, or an empty list 86 * if the request's body was empty or unchunked. 87 */ getChunkSizes()88 public List<Integer> getChunkSizes() { 89 return chunkSizes; 90 } 91 92 /** 93 * Returns the total size of the body of this POST request (before 94 * truncation). 95 */ getBodySize()96 public long getBodySize() { 97 return bodySize; 98 } 99 100 /** Returns the body of this POST request. This may be truncated. */ getBody()101 public Buffer getBody() { 102 return body; 103 } 104 105 /** @deprecated Use {@link #getBody() getBody().readUtf8()}. */ getUtf8Body()106 public String getUtf8Body() { 107 return getBody().readUtf8(); 108 } 109 110 /** 111 * Returns the index of this request on its HTTP connection. Since a single 112 * HTTP connection may serve multiple requests, each request is assigned its 113 * own sequence number. 114 */ getSequenceNumber()115 public int getSequenceNumber() { 116 return sequenceNumber; 117 } 118 119 /** Returns the connection's TLS version or null if the connection doesn't use SSL. */ getTlsVersion()120 public TlsVersion getTlsVersion() { 121 return tlsVersion; 122 } 123 toString()124 @Override public String toString() { 125 return requestLine; 126 } 127 } 128