How to implement MockWebServer to work with proxy

半腔热情 提交于 2019-12-22 00:25:43

问题


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!

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?

Edit: When i am behind proxy the dispatcher never gets called


回答1:


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

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



来源:https://stackoverflow.com/questions/41693474/how-to-implement-mockwebserver-to-work-with-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!