1 /* 2 * Copyright (C) 2013 Square, 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 package com.squareup.okhttp; 17 18 import com.squareup.okhttp.ws.WebSocket; 19 import java.io.IOException; 20 import java.net.URL; 21 import java.util.Arrays; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assert.assertNotNull; 26 import static org.junit.Assert.assertNull; 27 import static org.junit.Assert.assertTrue; 28 29 /** 30 * A received response or failure recorded by the response recorder. 31 */ 32 public final class RecordedResponse { 33 public final Request request; 34 public final Response response; 35 public final WebSocket webSocket; 36 public final String body; 37 public final IOException failure; 38 RecordedResponse(Request request, Response response, WebSocket webSocket, String body, IOException failure)39 public RecordedResponse(Request request, Response response, WebSocket webSocket, String body, 40 IOException failure) { 41 this.request = request; 42 this.response = response; 43 this.webSocket = webSocket; 44 this.body = body; 45 this.failure = failure; 46 } 47 assertRequestUrl(URL url)48 public RecordedResponse assertRequestUrl(URL url) { 49 assertEquals(url, request.url()); 50 return this; 51 } 52 assertRequestMethod(String method)53 public RecordedResponse assertRequestMethod(String method) { 54 assertEquals(method, request.method()); 55 return this; 56 } 57 assertRequestHeader(String name, String... values)58 public RecordedResponse assertRequestHeader(String name, String... values) { 59 assertEquals(Arrays.asList(values), request.headers(name)); 60 return this; 61 } 62 assertCode(int expectedCode)63 public RecordedResponse assertCode(int expectedCode) { 64 assertEquals(expectedCode, response.code()); 65 return this; 66 } 67 assertSuccessful()68 public RecordedResponse assertSuccessful() { 69 assertTrue(response.isSuccessful()); 70 return this; 71 } 72 assertNotSuccessful()73 public RecordedResponse assertNotSuccessful() { 74 assertFalse(response.isSuccessful()); 75 return this; 76 } 77 assertHeader(String name, String... values)78 public RecordedResponse assertHeader(String name, String... values) { 79 assertEquals(Arrays.asList(values), response.headers(name)); 80 return this; 81 } 82 assertBody(String expectedBody)83 public RecordedResponse assertBody(String expectedBody) { 84 assertEquals(expectedBody, body); 85 return this; 86 } 87 assertHandshake()88 public RecordedResponse assertHandshake() { 89 Handshake handshake = response.handshake(); 90 assertNotNull(handshake.cipherSuite()); 91 assertNotNull(handshake.peerPrincipal()); 92 assertEquals(1, handshake.peerCertificates().size()); 93 assertNull(handshake.localPrincipal()); 94 assertEquals(0, handshake.localCertificates().size()); 95 return this; 96 } 97 98 /** 99 * Asserts that the current response was redirected and returns the prior 100 * response. 101 */ priorResponse()102 public RecordedResponse priorResponse() { 103 Response priorResponse = response.priorResponse(); 104 assertNotNull(priorResponse); 105 assertNull(priorResponse.body()); 106 return new RecordedResponse(priorResponse.request(), priorResponse, null, null, null); 107 } 108 109 /** 110 * Asserts that the current response used the network and returns the network 111 * response. 112 */ networkResponse()113 public RecordedResponse networkResponse() { 114 Response networkResponse = response.networkResponse(); 115 assertNotNull(networkResponse); 116 assertNull(networkResponse.body()); 117 return new RecordedResponse(networkResponse.request(), networkResponse, null, null, null); 118 } 119 120 /** Asserts that the current response didn't use the network. */ assertNoNetworkResponse()121 public RecordedResponse assertNoNetworkResponse() { 122 assertNull(response.networkResponse()); 123 return this; 124 } 125 126 /** Asserts that the current response didn't use the cache. */ assertNoCacheResponse()127 public RecordedResponse assertNoCacheResponse() { 128 assertNull(response.cacheResponse()); 129 return this; 130 } 131 132 /** 133 * Asserts that the current response used the cache and returns the cache 134 * response. 135 */ cacheResponse()136 public RecordedResponse cacheResponse() { 137 Response cacheResponse = response.cacheResponse(); 138 assertNotNull(cacheResponse); 139 assertNull(cacheResponse.body()); 140 return new RecordedResponse(cacheResponse.request(), cacheResponse, null, null, null); 141 } 142 assertFailure(String... messages)143 public void assertFailure(String... messages) { 144 assertNotNull(failure); 145 assertTrue(failure.getMessage(), Arrays.asList(messages).contains(failure.getMessage())); 146 } 147 } 148