Fixing a grid unit test broken after migration to OkHttp
package org.openqa.grid.internal.listener;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.openqa.grid.web.servlet.handler.SeleniumBasedResponse;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
+import org.openqa.selenium.remote.http.HttpClient;
+import org.openqa.selenium.remote.http.HttpResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
@@ -82,49 +86,25 @@ public URL getRemoteHost() {
return null;
}
-// @Override
-// public HttpClientFactory getHttpClientFactory() {
-// // Create mocks for network traffic
-// HttpClientFactory factory = mock(HttpClientFactory.class);
-// HttpClient client = mock(HttpClient.class);
-// HttpResponse response = mock(HttpResponse.class);
-// HttpEntity entity = mock(HttpEntity.class);
-// InputStream stream = mock(InputStream.class);
-// StatusLine line = mock(StatusLine.class);
-//
-// when(line.getStatusCode()).thenReturn(200);
-// when(response.getStatusLine()).thenReturn(line);
-// when(response.getAllHeaders()).thenReturn(new Header[0]);
-// when(response.getEntity()).thenReturn(entity);
-// try {
-// // Create a fake stream that will only return the a single number
-// Answer<Integer> answer = new Answer<Integer>() {
-// boolean hasBeenRead = false;
-// @Override
-// public Integer answer(InvocationOnMock invocation) {
-// if (hasBeenRead) {
-// return -1;
-// }
-// hasBeenRead = true;
-// return 1;
-// }
-// };
-//
-// // Have all the methods return mocks so client.execute returns our
-// // mocked objects
-// when(stream.read(any(byte[].class))).thenAnswer(answer);
-// when(entity.getContent()).thenReturn(stream);
-// when(client.execute(
-// any(HttpHost.class),
-// any(HttpRequest.class), any(
-// HttpContext.class))).thenReturn(response);
-// when(factory.getGridHttpClient(anyInt(), anyInt())).thenReturn(client);
-// } catch (Exception e) {
-// e.printStackTrace();
-// }
-//
-// return factory;
-// }
+ @Override
+ public HttpClient getHttpClient(URL url) {
+ // Create mocks for network traffic
+ HttpClient client = mock(HttpClient.class);
+ HttpResponse response = mock(HttpResponse.class);
+
+ when(response.getStatus()).thenReturn(200);
+ when(response.getHeaderNames()).thenReturn(Collections.emptyList());
+ when(response.getContent()).thenReturn("Done".getBytes());
+ when(response.getContentString()).thenReturn("Done");
+ when(response.getContentEncoding()).thenReturn(Charset.defaultCharset());
+ try {
+ when(client.execute(any())).thenReturn(response);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return client;
+ }
}
private RegistrationRequest req = null;
