Search in sources :

Example 1 with RecordedRequest

use of com.squareup.okhttp.mockwebserver.RecordedRequest in project mbed-cloud-sdk-java by ARMmbed.

the class TestApiClientWrapper method testClient.

/**
 * Tests that the HTTP client wrapper works as expected by spawning a mock server and checking received requests.
 */
@Test
public void testClient() {
    try {
        MockWebServer server = new MockWebServer();
        server.enqueue(new MockResponse().setBody("hello, world!"));
        server.start();
        HttpUrl baseUrl = server.url("");
        ConnectionOptions opt = new ConnectionOptions("apikey");
        opt.setHost(baseUrl.toString());
        ApiClientWrapper clientWrapper = new ApiClientWrapper(opt);
        TestApiService testService = clientWrapper.createService(TestApiService.class);
        assertTrue(testService.getEndpointValue().execute().isSuccessful());
        RecordedRequest request = server.takeRequest();
        assertEquals("/" + AN_ENDPOINT_PATH, request.getPath());
        assertNotNull(request.getHeader("Authorization"));
        assertTrue(request.getHeader("Authorization").contains("Bearer"));
        assertNotNull(request.getHeader("User-Agent"));
        assertTrue(request.getHeader("User-Agent").contains(ApiClientWrapper.UserAgent.MBED_CLOUD_SDK_IDENTIFIER));
        server.shutdown();
    } catch (IOException | InterruptedException e) {
        fail(e.getMessage());
    }
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) IOException(java.io.IOException) HttpUrl(com.squareup.okhttp.HttpUrl) Test(org.junit.Test)

Example 2 with RecordedRequest

use of com.squareup.okhttp.mockwebserver.RecordedRequest in project enroscar by stanfy.

the class RequestDescriptionTest method makePostConnectionShouldReceiveCorrectResponse.

@Test
public void makePostConnectionShouldReceiveCorrectResponse() throws Exception {
    getWebServer().enqueue(new MockResponse().setBody("POST response"));
    final URLConnection connection = makeConnection(new MyRequestBuilder<String>(Robolectric.application) {
    }.setUrl(getWebServer().getUrl("/post").toString()).addParam("p1", "v1").setOperationType(OperationType.SIMPLE_POST));
    assertThat(ResponseCache.getDefault()).isNull();
    final String response = read(connection);
    final RecordedRequest request = getWebServer().takeRequest();
    assertThat(request.getMethod()).isEqualTo("POST");
    assertThat(request.getBody().readUtf8()).isEqualTo("p1=v1");
    final HttpURLConnection http = (HttpURLConnection) UrlConnectionWrapper.unwrap(connection);
    assertThat(http.getResponseCode()).isEqualTo(HttpURLConnection.HTTP_OK);
    assertThat(response).isEqualTo("POST response");
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Test(org.junit.Test) AbstractMockServerTest(com.stanfy.enroscar.net.test.AbstractMockServerTest)

Example 3 with RecordedRequest

use of com.squareup.okhttp.mockwebserver.RecordedRequest in project data-transfer-project by google.

the class MicrosoftCalendarImportTest method testImport.

@Test
@SuppressWarnings("unchecked")
public void testImport() throws Exception {
    server.enqueue(new MockResponse().setBody(BATCH_CALENDAR_RESPONSE));
    server.enqueue(new MockResponse().setResponseCode(201).setBody(BATCH_EVENT_RESPONSE));
    server.start();
    HttpUrl baseUrl = server.url("");
    MicrosoftCalendarImporter importer = new MicrosoftCalendarImporter(baseUrl.toString(), client, mapper, transformerService, jobStore);
    CalendarModel calendarModel = new CalendarModel("OldId1", "name", "name");
    CalendarAttendeeModel attendeeModel = new CalendarAttendeeModel("Test Attendee", "test@test.com", false);
    CalendarEventModel.CalendarEventTime start = new CalendarEventModel.CalendarEventTime(ZonedDateTime.now(ZoneId.of("GMT")).toOffsetDateTime(), false);
    CalendarEventModel.CalendarEventTime end = new CalendarEventModel.CalendarEventTime(ZonedDateTime.now(ZoneId.of("GMT")).toOffsetDateTime(), false);
    CalendarEventModel eventModel = new CalendarEventModel("OldId1", "Event1", "Test Notes", singletonList(attendeeModel), "Location1", start, end);
    CalendarContainerResource resource = new CalendarContainerResource(singleton(calendarModel), singleton(eventModel));
    ImportResult result = importer.importItem(JOB_ID, token, resource);
    Assert.assertEquals(ImportResult.ResultType.OK, result.getType());
    // verify the batch calendar request
    RecordedRequest calendarBatch = server.takeRequest();
    Map<String, Object> calendarBody = (Map<String, Object>) mapper.readValue(calendarBatch.getBody().readUtf8(), Map.class);
    List<Map<String, Object>> calendarRequests = (List<Map<String, Object>>) calendarBody.get("requests");
    Assert.assertNotNull(calendarRequests);
    Assert.assertEquals(1, calendarRequests.size());
    Map<String, Object> calendarRequest = calendarRequests.get(0);
    Assert.assertNotNull(calendarRequest.get("headers"));
    Assert.assertEquals("POST", calendarRequest.get("method"));
    Assert.assertEquals("/v1.0/me/calendars", calendarRequest.get("url"));
    Map<String, Object> calendarRequestBody = (Map<String, Object>) calendarRequest.get("body");
    Assert.assertNotNull(calendarRequestBody);
    Assert.assertEquals("name", calendarRequestBody.get("name"));
    // verify the calendar id mapping from old id to new id was saved
    Assert.assertEquals("NewId1", jobStore.findData(TempCalendarData.class, JOB_ID).getImportedId("OldId1"));
    // verify the batch event request
    RecordedRequest eventBatch = server.takeRequest();
    Map<String, Object> eventRequests = (Map<String, Object>) mapper.readValue(eventBatch.getBody().readUtf8(), Map.class);
    Map<String, Object> eventRequest = (Map<String, Object>) ((List<Map<String, Object>>) eventRequests.get("requests")).get(0);
    Assert.assertNotNull(eventRequest.get("headers"));
    Assert.assertEquals("POST", eventRequest.get("method"));
    Assert.assertEquals("/v1.0/me/calendars/NewId1/events", // verify the URL is contructed correctly with NewId
    eventRequest.get("url"));
    Map<String, Object> eventRequestBody = (Map<String, Object>) eventRequest.get("body");
    Assert.assertNotNull(eventRequestBody);
    Assert.assertEquals("Event1", eventRequestBody.get("subject"));
    Map<String, Object> location = (Map<String, Object>) eventRequestBody.get("location");
    Assert.assertEquals("Location1", location.get("displayName"));
    Assert.assertEquals("Default", location.get("locationType"));
    Map<String, Object> body = (Map<String, Object>) eventRequestBody.get("body");
    Assert.assertEquals("Test Notes", body.get("content"));
    Assert.assertEquals("HTML", body.get("contentType"));
    List<Map<String, Object>> attendees = (List<Map<String, Object>>) eventRequestBody.get("attendees");
    Assert.assertEquals(1, attendees.size());
    Map<String, Object> attendee = (Map<String, Object>) attendees.get(0);
    Assert.assertEquals("required", attendee.get("type"));
    Map<String, Object> emailAddress = (Map<String, Object>) attendee.get("emailAddress");
    Assert.assertEquals("test@test.com", emailAddress.get("address"));
    Assert.assertEquals("Test Attendee", emailAddress.get("name"));
    // verify dates
    Map<String, Object> startDate = (Map<String, Object>) eventRequestBody.get("start");
    Assert.assertNotNull(startDate.get("dateTime"));
    Assert.assertEquals("UTC", startDate.get("timeZone"));
    Map<String, Object> endDate = (Map<String, Object>) eventRequestBody.get("end");
    Assert.assertNotNull(endDate.get("dateTime"));
    Assert.assertEquals("UTC", endDate.get("timeZone"));
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MicrosoftCalendarImporter(org.dataportabilityproject.transfer.microsoft.calendar.MicrosoftCalendarImporter) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) CalendarModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarModel) CalendarEventModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) HttpUrl(com.squareup.okhttp.HttpUrl) CalendarAttendeeModel(org.dataportabilityproject.types.transfer.models.calendar.CalendarAttendeeModel) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 4 with RecordedRequest

use of com.squareup.okhttp.mockwebserver.RecordedRequest in project enroscar by stanfy.

the class RequestDescriptionTest method makeGetConnectionShouldSendGoodParametersAndHeaders.

@Test
public void makeGetConnectionShouldSendGoodParametersAndHeaders() throws Exception {
    getWebServer().enqueue(new MockResponse().setBody("test response"));
    read(makeConnection(new MyRequestBuilder<String>(Robolectric.application) {
    }.setUrl(getWebServer().getUrl("/r1").toString()).addParam("p1", "v1").addParam("p2", "v2")));
    // check that request was as expected
    final RecordedRequest request = getWebServer().takeRequest();
    // url
    assertThat(request.getPath()).isEqualTo("/r1?p1=v1&p2=v2");
    // headers: language, gzip
    final String lang = Robolectric.application.getResources().getConfiguration().locale.getLanguage();
    assertThat(request.getHeaders().toMultimap()).containsEntry("Accept-Language: " + lang, Collections.singletonList("Accept-Encoding: gzip"));
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) Test(org.junit.Test) AbstractMockServerTest(com.stanfy.enroscar.net.test.AbstractMockServerTest)

Aggregations

MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)4 RecordedRequest (com.squareup.okhttp.mockwebserver.RecordedRequest)4 Test (org.junit.Test)4 HttpUrl (com.squareup.okhttp.HttpUrl)2 AbstractMockServerTest (com.stanfy.enroscar.net.test.AbstractMockServerTest)2 MockWebServer (com.squareup.okhttp.mockwebserver.MockWebServer)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URLConnection (java.net.URLConnection)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 ImportResult (org.dataportabilityproject.spi.transfer.provider.ImportResult)1 MicrosoftCalendarImporter (org.dataportabilityproject.transfer.microsoft.calendar.MicrosoftCalendarImporter)1 CalendarAttendeeModel (org.dataportabilityproject.types.transfer.models.calendar.CalendarAttendeeModel)1 CalendarContainerResource (org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource)1 CalendarEventModel (org.dataportabilityproject.types.transfer.models.calendar.CalendarEventModel)1 CalendarModel (org.dataportabilityproject.types.transfer.models.calendar.CalendarModel)1