1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-08 23:42:08 +03:00

Create request/response API tests to match some URLConnectionTest tests.

This is more work towards being able to later delete HttpURLConnectionImpl
without losing test coverage.

Also do some test cleanup.
This commit is contained in:
jwilson
2016-01-23 19:12:33 -05:00
parent 9920c57e7a
commit 561613d579
14 changed files with 333 additions and 198 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2012 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
public final class FakeDns implements Dns {
private List<String> requestedHosts = new ArrayList<>();
private List<InetAddress> addresses = Collections.emptyList();
/** Sets the addresses to be returned by this fake DNS service. */
public FakeDns addresses(List<InetAddress> addresses) {
this.addresses = new ArrayList<>(addresses);
return this;
}
/** Sets the service to throw when a hostname is requested. */
public FakeDns unknownHost() {
this.addresses = Collections.emptyList();
return this;
}
public InetAddress address(int index) {
return addresses.get(index);
}
@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException {
requestedHosts.add(hostname);
if (addresses.isEmpty()) throw new UnknownHostException();
return addresses;
}
public void assertRequests(String... expectedHosts) {
assertEquals(Arrays.asList(expectedHosts), requestedHosts);
requestedHosts.clear();
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3;
import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public final class FakeProxySelector extends ProxySelector {
public final List<Proxy> proxies = new ArrayList<>();
public FakeProxySelector addProxy(Proxy proxy) {
proxies.add(proxy);
return this;
}
@Override public List<Proxy> select(URI uri) {
// Don't handle 'socket' schemes, which the RI's Socket class may request (for SOCKS).
return uri.getScheme().equals("http") || uri.getScheme().equals("https") ? proxies
: Collections.singletonList(Proxy.NO_PROXY);
}
@Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
}

View File

@@ -0,0 +1,120 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package okhttp3;
import java.security.Principal;
import java.security.cert.Certificate;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;
import javax.security.cert.X509Certificate;
public final class FakeSSLSession implements SSLSession {
private final Certificate[] certificates;
public FakeSSLSession(Certificate... certificates) throws Exception {
this.certificates = certificates;
}
public int getApplicationBufferSize() {
throw new UnsupportedOperationException();
}
public String getCipherSuite() {
throw new UnsupportedOperationException();
}
public long getCreationTime() {
throw new UnsupportedOperationException();
}
public byte[] getId() {
throw new UnsupportedOperationException();
}
public long getLastAccessedTime() {
throw new UnsupportedOperationException();
}
public Certificate[] getLocalCertificates() {
throw new UnsupportedOperationException();
}
public Principal getLocalPrincipal() {
throw new UnsupportedOperationException();
}
public int getPacketBufferSize() {
throw new UnsupportedOperationException();
}
public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
if (certificates.length == 0) {
throw new SSLPeerUnverifiedException("peer not authenticated");
} else {
return certificates;
}
}
public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
throw new UnsupportedOperationException();
}
public String getPeerHost() {
throw new UnsupportedOperationException();
}
public int getPeerPort() {
throw new UnsupportedOperationException();
}
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
throw new UnsupportedOperationException();
}
public String getProtocol() {
throw new UnsupportedOperationException();
}
public SSLSessionContext getSessionContext() {
throw new UnsupportedOperationException();
}
public void putValue(String s, Object obj) {
throw new UnsupportedOperationException();
}
public void removeValue(String s) {
throw new UnsupportedOperationException();
}
public Object getValue(String s) {
throw new UnsupportedOperationException();
}
public String[] getValueNames() {
throw new UnsupportedOperationException();
}
public void invalidate() {
throw new UnsupportedOperationException();
}
public boolean isValid() {
throw new UnsupportedOperationException();
}
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.testing;
package okhttp3;
import java.util.ArrayList;
import java.util.List;