Search code examples
androidproxyokhttpandroid-instrumentationmockwebserver

How to implement MockWebServer to work with proxy


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


Solution

  • 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.