### Question:
Http { public Response get(String url, Map<String, String> headers, String username, String password) throws IOException, URISyntaxException { URI uri = new URI(url); return makeRequest(headers, username, password, new HttpGet(uri), uri.getHost()); }  Response get(String url, Map<String, String> headers, String username, String password); Response post(String url, Map<String, String> headers, String postBody, String username, String password);  }### Answer:
@Test public void testGet_FormsCorrectRequest_noBasicAuth() throws Exception { Robolectric.addPendingHttpResponse(200, "OK"); http.get("www.example.com", new HashMap<String, String>(), null, null); assertThat(((HttpUriRequest) Robolectric.getSentHttpRequest(0)).getURI(), equalTo(URI.create("www.example.com"))); }
@Test public void testGet_shouldApplyCorrectHeaders() throws Exception { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("foo", "bar"); http.get("www.example.com", headers, null, null); HttpRequest sentHttpRequest = Robolectric.getSentHttpRequest(0); assertThat(sentHttpRequest.getHeaders("foo")[0].getValue(), equalTo("bar")); }
@Test public void testGet_ShouldUseCorrectHttpMethod() throws Exception { http.get("www.example.com", new HashMap<String, String>(), null, null); HttpUriRequest sentHttpRequest = (HttpUriRequest) Robolectric.getSentHttpRequest(0); assertThat(sentHttpRequest.getMethod(), equalTo(HttpGet.METHOD_NAME)); }
@Test public void testGet_FormsCorrectRequest_withBasicAuth() throws Exception { Robolectric.addPendingHttpResponse(200, "OK"); http.get("www.example.com", new HashMap<String, String>(), "username", "password"); HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0); CredentialsProvider credentialsProvider = (CredentialsProvider) sentHttpRequestData.getHttpContext().getAttribute(ClientContext.CREDS_PROVIDER); assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), equalTo("username")); assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), equalTo("password")); }
@Test public void shouldReturnCorrectResponse() throws Exception { Robolectric.addPendingHttpResponse(666, "it's all cool"); Http.Response response = http.get("www.example.com", new HashMap<String, String>(), null, null); assertThat(fromStream(response.getResponseBody()), equalTo("it's all cool")); assertThat(response.getStatusCode(), equalTo(666)); }