1

Square에서 MockWebServer를 구현하려고하는데 프록시가 필요합니다. 문제는 매번 MoodWebServer에 대한 모든 요청에 ​​대해 407을 얻고 있기 때문에 계측 테스트가 실패 할 때마다 실패한다는 것입니다.프록시와 함께 작동하도록 MockWebServer를 구현하는 방법

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

계측 테스트를 수행 할 때 나의 엔드 포인트는 "/"입니다.

이 문제는 프록시 네트워크 뒤에있을 때만 발생합니다. 모바일 장치에서 프록시가 아닌 다른 네트워크로 전환하면 모의 서버가 제대로 작동합니다. 어떤 생각이 내가 뭘 잘못하고 있니?

편집 : 내가 프록시 뒤에 생각하면라는 결코 극복 디스패처

답변

1

좋아, 난 그냥 내 okhttp3 클라이언트가 모의에 실제 프록시 서버를 가리키고되지 않았 음을 결과 .... 결국 밖으로 figuered localhost의 웹 서버. Flavor를 테스트 할 때만 내 okhttp3 클라이언트에 프록시를 추가 한 다음 Retrofit2 빌더에 추가하여이 문제를 해결했습니다. 코드는 다음과 같습니다.

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을 위하여 건물 모의 웹 서버 포트와 동일한 점에 유의하는 것이 중요합니다.

관련 문제