如何实现MockWebServer与代理一起使用 [英] How to implement MockWebServer to work with proxy

查看:357
本文介绍了如何实现MockWebServer与代理一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Square实现MockWebServer,但我在代理后面.问题在于,每次执行仪器测试都会失败,因为我对MockWebServer所做的每个请求都会得到407.

I am trying to implement a MockWebServer from Square and i am behind a proxy. The problem is that every time i am executing my instrumentation test will fail because i am getting a 407 for every request i am doing to my MockWebServer.

debug.level.titleD/OkHttp: <-- 407 Proxy Authentication Required http://localhost:12345/user/login (767ms)

当您看到我指向我的本地主机时,我不知道为什么会得到这个!

As u see i am pointing to my localhost and i dont know why i am getting this!

这是我的MockWebServer实现!

Here is my MockWebServer implementation!

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();
}

我执行仪器测试时的终点是"/".

My end point when i am executing instrumentation test is "/".

仅当我位于代理网络后方时,才会出现此问题,如果我在移动设备中切换到不在代理服务器后方的另一个网络,则模拟服务器运行良好.知道我在做什么错吗?

This problem only occurs when i am behind a proxy network, if in my mobile device i switch to another network that is not behind proxy the mock server works well. Any idea what i am doing wrong?

当我落后于代理人时,调度员将永远不会被呼叫

When i am behind proxy the dispatcher never gets called

推荐答案

好吧,我终于弄清楚了....结果是我的okhttp3客户端指向了真正的代理服务器,而不是本地主机中的模拟Web服务器. .通过仅在测试Flavor时将代理添加到okhttp3客户端中,然后将其添加到Retrofit2 Builder中,我解决了这个问题.代码看起来像这样.

Ok i just figuered out in the end.... Result that my okhttp3 client was pointing to the real proxy server and not to the mock web server in localhost. I solved this by adding a proxy to my okhttp3 client only when in testing Flavour and then add it to Retrofit2 builder. The code looks like this.

if (BuildConfig.TEST_PROXY){
            try {
                InetSocketAddress sock = new InetSocketAddress(InetAddress.getByName("localhost"),12345);
                builderOkhttpClient.proxy(new Proxy(Proxy.Type.HTTP, sock));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

请务必注意,在构建 InetSocketAddress 时,端口模拟Web服务器端口相同.

It's important to note that the port when building the InetSocketAddress is the same as the mock web server port.

这篇关于如何实现MockWebServer与代理一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆