我正在尝试从 Square 实现 MockWebServer 并且我在代理后面。问题是每次我执行仪器测试都会失败,因为我对 MockWebServer 所做的每个请求都会收到 407。
debug.level.titleD/OkHttp: <-- 407 Proxy Authentication Required http://localhost:12345/user/login (767ms)
如您所见,我指向我的本地主机,但我不知道为什么会得到这个!
这是我的 MockWebServer 实现!
public class MockedTestServer {
private final int PORT = 12345;
private final MockWebServer server;
private int lastResponseCode;
private String lastRequestPath;
/**
* Creates and starts a new server, with a non-default dispatcher
*
* @throws Exception
*/
public MockedTestServer() throws Exception {
server = new MockWebServer();
server.start(PORT);
setDispatcher();
}
private void setDispatcher() {
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(final RecordedRequest request) throws InterruptedException {
try {
final String requestPath = request.getPath();
final MockResponse response = new MockResponse().setResponseCode(200);
String filename;
// response for alerts
if (requestPath.equals(Constantes.ACTION_LOGIN)) {
filename = ConstantesJSON.LOGIN_OK;
} else {
// no response
lastResponseCode = 404;
return new MockResponse().setResponseCode(404);
}
lastResponseCode = 200;
response.setBody(RestServiceTestHelper.getStringFromFile(filename));
lastRequestPath = requestPath;
return response;
} catch (final Exception e) {
throw new InterruptedException(e.getMessage());
}
}
};
server.setDispatcher(dispatcher);
}
public String getLastRequestPath() {
return lastRequestPath;
}
public String getUrl() {
return server.url("/").toString();
}
public int getLastResponseCode() {
return lastResponseCode;
}
public void setDefaultDispatcher() {
server.setDispatcher(new QueueDispatcher());
}
public void enqueueResponse(final MockResponse response) {
server.enqueue(response);
}
public void shutdownServer() throws IOException {
server.shutdown();
}
我执行仪器测试时的终点是“/”。
仅当我在代理网络后面时才会出现此问题,如果在我的移动设备中我切换到不在代理后面的另一个网络,则模拟服务器运行良好。知道我在做什么错吗?
编辑:当我在代理后面时,调度员永远不会被调用