本文整理匯總了Java中com.squareup.okhttp.mockwebserver.MockWebServer.takeRequest方法的典型用法代碼示例。如果您正苦於以下問題:Java MockWebServer.takeRequest方法的具體用法?Java MockWebServer.takeRequest怎麽用?Java MockWebServer.takeRequest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.squareup.okhttp.mockwebserver.MockWebServer
的用法示例。
在下文中一共展示了MockWebServer.takeRequest方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: assertSent
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
protected RecordedRequest assertSent(final MockWebServer server,
final String method,
final String expectedPath,
final Map<String, ?> queryParams) throws InterruptedException {
final RecordedRequest request = server.takeRequest();
assertThat(request.getMethod()).isEqualTo(method);
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(APPLICATION_JSON);
final String path = request.getPath();
final String rawPath = path.contains("?") ? path.substring(0, path.indexOf('?')) : path;
assertThat(rawPath).isEqualTo(expectedPath);
final Map<String, String> normalizedParams = Maps.transformValues(queryParams, Functions.toStringFunction());
assertThat(normalizedParams).isEqualTo(extractParams(path));
return request;
}
示例2: testThatTheProjectIDHeaderIsSet
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test
public void testThatTheProjectIDHeaderIsSet() {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
String projectId = "this-is-a-project-id";
ConnectAPI connectAPI = new ConnectAPI(projectId, "", server.url("").toString());
Map<String, Object> event = new HashMap<String, Object>();
event.put("test", "test");
connectAPI.pushEvent("test", new Event(event));
RecordedRequest request = null;
try {
request = server.takeRequest();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals(projectId, request.getHeader("X-Project-Id"));
}
示例3: testThatTheAPIKeyHeaderIsSet
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test
public void testThatTheAPIKeyHeaderIsSet() {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
String apiKey = "this-is-an-api-key";
ConnectAPI connectAPI = new ConnectAPI("", apiKey, server.url("").toString());
Map<String, Object> event = new HashMap<String, Object>();
event.put("test", "test");
connectAPI.pushEvent("test", new Event(event));
RecordedRequest request = null;
try {
request = server.takeRequest();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals(apiKey, request.getHeader("X-Api-Key"));
}
示例4: testThatTheContentTypeHeaderIsSet
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test
public void testThatTheContentTypeHeaderIsSet() {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
ConnectAPI connectAPI = new ConnectAPI("", "", server.url("").toString());
Map<String, Object> event = new HashMap<String, Object>();
event.put("test", "test");
connectAPI.pushEvent("test", new Event(event));
RecordedRequest request = null;
try {
request = server.takeRequest();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals("application/json; charset=utf-8", request.getHeader("Content-Type"));
}
示例5: testSendingCookiesFromStore
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testSendingCookiesFromStore() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
server.play();
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookieA = new HttpCookie("a", "android");
cookieA.setDomain(server.getCookieDomain());
cookieA.setPath("/");
cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieA);
HttpCookie cookieB = new HttpCookie("b", "banana");
cookieB.setDomain(server.getCookieDomain());
cookieB.setPath("/");
cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieB);
CookieHandler.setDefault(cookieManager);
get(server, "/");
RecordedRequest request = server.takeRequest();
List<String> receivedHeaders = request.getHeaders();
assertContains(receivedHeaders, "Cookie: $Version=\"1\"; "
+ "a=\"android\";$Path=\"/\";$Domain=\"" + server.getCookieDomain() + "\"; "
+ "b=\"banana\";$Path=\"/\";$Domain=\"" + server.getCookieDomain() + "\"");
}
示例6: testCookiesSentIgnoresCase
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testCookiesSentIgnoresCase() throws Exception {
CookieHandler.setDefault(new CookieManager() {
@Override public Map<String, List<String>> get(URI uri,
Map<String, List<String>> requestHeaders) throws IOException {
Map<String, List<String>> result = new HashMap<String, List<String>>();
result.put("COOKIE", Collections.singletonList("Bar=bar"));
result.put("cooKIE2", Collections.singletonList("Baz=baz"));
return result;
}
});
MockWebServer server = new MockWebServer();
server. enqueue(new MockResponse());
server.play();
get(server, "/");
RecordedRequest request = server.takeRequest();
assertContainsAll(request.getHeaders(), "COOKIE: Bar=bar", "cooKIE2: Baz=baz");
assertFalse(request.getHeaders().contains("Quux: quux"));
}
示例7: assertSent
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
protected RecordedRequest assertSent(MockWebServer server, String method, String path, String applicationJson) throws InterruptedException {
RecordedRequest request = server.takeRequest();
assertThat(request.getMethod()).isEqualTo(method);
assertThat(request.getPath()).isEqualTo(path);
// TODO - header is "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2" not XML.
// assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.APPLICATION_XML);
return request;
}
示例8: assertSent
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
protected RecordedRequest assertSent(MockWebServer server, String method, String path) throws InterruptedException {
RecordedRequest request = server.takeRequest();
assertThat(request.getMethod()).isEqualTo(method);
assertThat(request.getPath()).isEqualTo(path);
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.APPLICATION_JSON);
return request;
}
示例9: assertSentWithFormData
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
protected RecordedRequest assertSentWithFormData(final MockWebServer server,
final String method,
final String path,
final String body) throws InterruptedException {
final RecordedRequest request = server.takeRequest();
assertThat(request.getMethod()).isEqualTo(method);
assertThat(request.getPath()).isEqualTo(path);
assertThat(request.getUtf8Body()).isEqualTo(body);
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(APPLICATION_JSON);
assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(MediaType.APPLICATION_FORM_URLENCODED);
return request;
}
示例10: assertSentAcceptText
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
protected RecordedRequest assertSentAcceptText(final MockWebServer server,
final String method,
final String path) throws InterruptedException {
final RecordedRequest request = server.takeRequest();
assertThat(request.getMethod()).isEqualTo(method);
assertThat(request.getPath()).isEqualTo(path);
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaType.TEXT_PLAIN);
return request;
}
示例11: testRedirectsDoNotIncludeTooManyCookies
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
MockWebServer redirectTarget = new MockWebServer();
redirectTarget.enqueue(new MockResponse().setBody("A"));
redirectTarget.play();
MockWebServer redirectSource = new MockWebServer();
redirectSource.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: " + redirectTarget.getUrl("/")));
redirectSource.play();
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(redirectSource.getCookieDomain());
cookie.setPath("/");
String portList = Integer.toString(redirectSource.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(redirectSource.getUrl("/").toURI(), cookie);
CookieHandler.setDefault(cookieManager);
get(redirectSource, "/");
RecordedRequest request = redirectSource.takeRequest();
assertContains(request.getHeaders(), "Cookie: $Version=\"1\"; "
+ "c=\"cookie\";$Path=\"/\";$Domain=\"" + redirectSource.getCookieDomain()
+ "\";$Port=\"" + portList + "\"");
for (String header : redirectTarget.takeRequest().getHeaders()) {
if (header.startsWith("Cookie")) {
fail(header);
}
}
}
示例12: testSuccess
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test
public final void testSuccess() throws Throwable {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(200));
server.start();
String path = "/foo/bar/data/";
String callbackUrl = server.getUrl(path).toString();
ObjectNode sentData = new ObjectNode(JsonNodeFactory.instance);
sentData.put("sourceId", SOURCE_ID);
ArrayNode data = sentData.putArray("data");
data.addObject().put("hello", "world");
data.addObject().put("and", "again");
setupSender(new HttpClient(SOURCE_ID, callbackUrl, true));
sender.onNewDataStart();
for (JsonNode item : data) sender.onNewDataItem((ObjectNode) item);
sender.onNewDataComplete();
assertEquals(SenderResult.Status.SUCCESS, sender.getSenderResult().getStatus());
RecordedRequest request = server.takeRequest();
assertEquals(path, request.getPath());
assertEquals(sentData.toString(), request.getUtf8Body());
server.shutdown();
}
示例13: testHeadersSentToCookieHandler
import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
/**
* Test which headers show up where. The cookie manager should be notified
* of both user-specified and derived headers like {@code Host}. Headers
* named {@code Cookie} or {@code Cookie2} that are returned by the cookie
* manager should show up in the request and in {@code
* getRequestProperties}.
*/
@Test public void testHeadersSentToCookieHandler() throws IOException, InterruptedException {
final Map<String, List<String>> cookieHandlerHeaders = new HashMap<String, List<String>>();
CookieHandler.setDefault(new CookieManager() {
@Override
public Map<String, List<String>> get(URI uri,
Map<String, List<String>> requestHeaders) throws IOException {
cookieHandlerHeaders.putAll(requestHeaders);
Map<String, List<String>> result = new HashMap<String, List<String>>();
result.put("Cookie", Collections.singletonList("Bar=bar"));
result.put("Cookie2", Collections.singletonList("Baz=baz"));
result.put("Quux", Collections.singletonList("quux"));
return result;
}
});
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
server.play();
HttpURLConnection connection = client.open(server.getUrl("/"));
assertEquals(Collections.<String, List<String>>emptyMap(),
connection.getRequestProperties());
connection.setRequestProperty("Foo", "foo");
connection.setDoOutput(true);
connection.getOutputStream().write(5);
connection.getOutputStream().close();
connection.getInputStream().close();
RecordedRequest request = server.takeRequest();
assertContainsAll(cookieHandlerHeaders.keySet(), "Foo");
assertContainsAll(cookieHandlerHeaders.keySet(),
"Content-type", "User-Agent", "Connection", "Host");
assertFalse(cookieHandlerHeaders.containsKey("Cookie"));
/*
* The API specifies that calling getRequestProperties() on a connected instance should fail
* with an IllegalStateException, but the RI violates the spec and returns a valid map.
* http://www.mail-archive.com/[email protected]/msg01768.html
*/
try {
assertContainsAll(connection.getRequestProperties().keySet(), "Foo");
assertContainsAll(connection.getRequestProperties().keySet(),
"Content-type", "Content-Length", "User-Agent", "Connection", "Host");
assertContainsAll(connection.getRequestProperties().keySet(), "Cookie", "Cookie2");
assertFalse(connection.getRequestProperties().containsKey("Quux"));
} catch (IllegalStateException expected) {
}
assertContainsAll(request.getHeaders(), "Foo: foo", "Cookie: Bar=bar", "Cookie2: Baz=baz");
assertFalse(request.getHeaders().contains("Quux: quux"));
}