下面列出了com.squareup.okhttp.mockwebserver.MockWebServer#url ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ApiInterface getApiInterface(MockWebServer mockWebServer) throws IOException {
mockWebServer.start();
TestUtils testUtils = new TestUtils();
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/repos.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/branches.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/contributors.json"));
}
return new MockResponse().setResponseCode(404);
}
};
mockWebServer.setDispatcher(dispatcher);
HttpUrl baseUrl = mockWebServer.url("/");
return ApiModule.getApiInterface(baseUrl.toString());
}
@Before
public void setup() {
Robolectric.getBackgroundThreadScheduler().reset();
Robolectric.getForegroundThreadScheduler().reset();
ShadowLog.stream = System.out;
activity = Robolectric.buildActivity(MockMainActivity.class).create().start().resume().visible().get();
shadowOf(activity).grantPermissions("android.permission.INTERNET");
server= new MockWebServer();
try {
server.start();
HttpUrl url= server.url("/");
UTConstants.REQUEST_BASE_URL_UT = url.toString();
System.out.println(UTConstants.REQUEST_BASE_URL_UT);
ShadowSettings.setTestURL(url.toString());
} catch (IOException e) {
System.out.print("IOException");
}
bgScheduler = Robolectric.getBackgroundThreadScheduler();
uiScheduler = Robolectric.getForegroundThreadScheduler();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
bgScheduler.pause();
uiScheduler.pause();
}
@Before
public void setup() {
SDKSettings.setExternalExecutor(null);
Robolectric.getBackgroundThreadScheduler().reset();
Robolectric.getForegroundThreadScheduler().reset();
ShadowLog.stream = System.out;
activity = Robolectric.buildActivity(MockMainActivity.class).create().start().resume().visible().get();
shadowOf(activity).grantPermissions("android.permission.INTERNET");
server= new MockWebServer();
try {
server.start();
HttpUrl url= server.url("/");
UTConstants.REQUEST_BASE_URL_UT = url.toString();
System.out.println(UTConstants.REQUEST_BASE_URL_UT);
ShadowSettings.setTestURL(url.toString());
TestResponsesUT.setTestURL(url.toString());
} catch (IOException e) {
System.out.print("IOException");
}
bgScheduler = Robolectric.getBackgroundThreadScheduler();
uiScheduler = Robolectric.getForegroundThreadScheduler();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
bgScheduler.pause();
uiScheduler.pause();
}
@Before
public void setUp() throws Exception {
super.setUp();
server = new MockWebServer();
server.start();
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/repos.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/branches.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/contributors.json"));
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
HttpUrl baseUrl = server.url("/");
apiInterface = ApiModule.getApiInterface(baseUrl.toString());
}
@BeforeMethod
public final void setUp() throws Exception {
mockWebServer = new MockWebServer();
HttpUrl serverUrl = mockWebServer.url("/");
// Override factory endpoint
getProxy().setHost(serverUrl.host());
getProxy().setPort(serverUrl.port());
}
@Test
public void nuroMetricTest() throws Exception{
MockWebServer mockWebServer = new MockWebServer();
NuroMetric metric = new NuroMetric();
double sample;
for(int i=0; i<=NUMBER_OF_METRICS; i++){
mockWebServer.enqueue(new MockResponse()
.setBody(NuroInputExample.EXAMPLE_INPUT)
.setHeader("Content-Type", MediaType.APPLICATION_JSON));
}
mockWebServer.start();
HttpUrl serverUrl = mockWebServer.url("/sensor.php");
metric.setMonitoredMetric("NUROServerLastMinuteAverageRunTime");
sample = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), TEST_APPLICATION_USER, TEST_APPLICATION_PASSWORD).doubleValue();
Assert.assertTrue(sample == EXPECTED_LAST_MINUTE_AVERAGE_RUNTIME);
metric.setMonitoredMetric("NUROServerLastMinuteAverageThroughput");
sample = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), TEST_APPLICATION_USER, TEST_APPLICATION_PASSWORD).doubleValue();
Assert.assertTrue(sample == EXPECTED_LAST_MINUTE_AVERAGE_THROUGHPUT);
metric.setMonitoredMetric("NUROServerLastMinutePlayerCount");
sample = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), TEST_APPLICATION_USER, TEST_APPLICATION_PASSWORD).doubleValue();
Assert.assertTrue(sample == EXPECTED_LAST_MINUTE_PLAYER_COUNT);
metric.setMonitoredMetric("NUROServerLastMinuteRequestCount");
sample = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), TEST_APPLICATION_USER, TEST_APPLICATION_PASSWORD).doubleValue();
Assert.assertTrue(sample == EXPECTED_LAST_MINUTE_REQUEST_COUNT);
metric.setMonitoredMetric("NUROServerLastTenSecondsAverageRunTime");
sample = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), TEST_APPLICATION_USER, TEST_APPLICATION_PASSWORD).doubleValue();
Assert.assertTrue(sample == EXPECTED_LAST_TEN_SECONDS_AVERAGE_RUNTIME);
metric.setMonitoredMetric("NUROServerLastTenSecondsAverageThroughput");
sample = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), TEST_APPLICATION_USER, TEST_APPLICATION_PASSWORD).doubleValue();
Assert.assertTrue(sample == EXPECTED_LAST_TEN_SECONDS_AVERAGE_THROUGHPUT);
metric.setMonitoredMetric("NUROServerLastTenSecondsPlayerCount");
sample = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), TEST_APPLICATION_USER, TEST_APPLICATION_PASSWORD).doubleValue();
Assert.assertTrue(sample == EXPECTED_LAST_TEN_SECONDS_PLAYER_COUNT);
metric.setMonitoredMetric("NUROServerLastTenSecondsRequestCount");
sample = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), TEST_APPLICATION_USER, TEST_APPLICATION_PASSWORD).doubleValue();
Assert.assertTrue(sample == EXPECTED_LAST_TEN_SECONDS_REQUEST_COUNT);
mockWebServer.shutdown();
}
@Test
public void moduleAvailabilityTest() throws Exception {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(new MockResponse());
HttpUrl serverUrl = mockWebServer.url("/");
ModuleAvailability metric = new ModuleAvailability();
Number response = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), "", "");
Assert.assertEquals(response, 1);
mockWebServer.shutdown();
response = metric.getSample("http://"+serverUrl.host()+":"+serverUrl.port(), "", "");
Assert.assertEquals(response, 0);
}