Search in sources :

Example 6 with HttpUrl

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

the class MicrosoftCalendarExportTest method testExport.

@Test
public void testExport() throws Exception {
    server.enqueue(new MockResponse().setBody(CALENDARS_RESPONSE));
    server.enqueue(new MockResponse().setBody(CALENDAR1_EVENTS_RESPONSE));
    server.enqueue(new MockResponse().setBody(CALENDAR2_EVENTS_RESPONSE));
    server.start();
    HttpUrl baseUrl = server.url("");
    MicrosoftCalendarExporter exporter = new MicrosoftCalendarExporter(baseUrl.toString(), client, mapper, transformerService);
    ExportResult<CalendarContainerResource> resource = exporter.export(UUID.randomUUID(), token);
    CalendarContainerResource calendarResource = resource.getExportedData();
    Assert.assertEquals(2, calendarResource.getCalendars().size());
    Assert.assertFalse(calendarResource.getCalendars().stream().anyMatch(c -> "Calendar1".equals(c.getId()) && "Calendar2".equals(c.getId())));
    Assert.assertEquals(2, calendarResource.getEvents().size());
    Assert.assertFalse(calendarResource.getEvents().stream().anyMatch(e -> "Test Appointment 1".equals(e.getTitle()) && "Test Appointment 2".equals(e.getTitle())));
}
Also used : MicrosoftCalendarExporter(org.dataportabilityproject.transfer.microsoft.calendar.MicrosoftCalendarExporter) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) TokenAuthData(org.dataportabilityproject.types.transfer.auth.TokenAuthData) Test(org.junit.Test) UUID(java.util.UUID) MicrosoftCalendarExporter(org.dataportabilityproject.transfer.microsoft.calendar.MicrosoftCalendarExporter) HttpUrl(com.squareup.okhttp.HttpUrl) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) TransformerServiceImpl(org.dataportabilityproject.transfer.microsoft.transformer.TransformerServiceImpl) OkHttpClient(okhttp3.OkHttpClient) After(org.junit.After) Assert(org.junit.Assert) Before(org.junit.Before) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) CalendarContainerResource(org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource) HttpUrl(com.squareup.okhttp.HttpUrl) Test(org.junit.Test)

Example 7 with HttpUrl

use of com.squareup.okhttp.HttpUrl 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 8 with HttpUrl

use of com.squareup.okhttp.HttpUrl in project grpc-java by grpc.

the class OkHttpClientTransport method createHttpProxySocket.

private Socket createHttpProxySocket(InetSocketAddress address, InetSocketAddress proxyAddress, String proxyUsername, String proxyPassword) throws StatusException {
    try {
        Socket sock;
        // The proxy address may not be resolved
        if (proxyAddress.getAddress() != null) {
            sock = socketFactory.createSocket(proxyAddress.getAddress(), proxyAddress.getPort());
        } else {
            sock = socketFactory.createSocket(proxyAddress.getHostName(), proxyAddress.getPort());
        }
        sock.setTcpNoDelay(true);
        Source source = Okio.source(sock);
        BufferedSink sink = Okio.buffer(Okio.sink(sock));
        // Prepare headers and request method line
        Request proxyRequest = createHttpProxyRequest(address, proxyUsername, proxyPassword);
        HttpUrl url = proxyRequest.httpUrl();
        String requestLine = String.format("CONNECT %s:%d HTTP/1.1", url.host(), url.port());
        // Write request to socket
        sink.writeUtf8(requestLine).writeUtf8("\r\n");
        for (int i = 0, size = proxyRequest.headers().size(); i < size; i++) {
            sink.writeUtf8(proxyRequest.headers().name(i)).writeUtf8(": ").writeUtf8(proxyRequest.headers().value(i)).writeUtf8("\r\n");
        }
        sink.writeUtf8("\r\n");
        // Flush buffer (flushes socket and sends request)
        sink.flush();
        // Read status line, check if 2xx was returned
        StatusLine statusLine = StatusLine.parse(readUtf8LineStrictUnbuffered(source));
        // Drain rest of headers
        while (!readUtf8LineStrictUnbuffered(source).equals("")) {
        }
        if (statusLine.code < 200 || statusLine.code >= 300) {
            Buffer body = new Buffer();
            try {
                sock.shutdownOutput();
                source.read(body, 1024);
            } catch (IOException ex) {
                body.writeUtf8("Unable to read body: " + ex.toString());
            }
            try {
                sock.close();
            } catch (IOException ignored) {
            // ignored
            }
            String message = String.format("Response returned from proxy was not successful (expected 2xx, got %d %s). " + "Response body:\n%s", statusLine.code, statusLine.message, body.readUtf8());
            throw Status.UNAVAILABLE.withDescription(message).asException();
        }
        return sock;
    } catch (IOException e) {
        throw Status.UNAVAILABLE.withDescription("Failed trying to connect with proxy").withCause(e).asException();
    }
}
Also used : StatusLine(com.squareup.okhttp.internal.http.StatusLine) Buffer(okio.Buffer) Request(com.squareup.okhttp.Request) BufferedSink(okio.BufferedSink) ByteString(okio.ByteString) IOException(java.io.IOException) SSLSocket(javax.net.ssl.SSLSocket) Socket(java.net.Socket) BufferedSource(okio.BufferedSource) Source(okio.Source) HttpUrl(com.squareup.okhttp.HttpUrl)

Example 9 with HttpUrl

use of com.squareup.okhttp.HttpUrl in project pinpoint by naver.

the class HttpEngineConnectMethodFromUserRequestInterceptor method doInAfterTrace.

@Override
protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
    recorder.recordApi(methodDescriptor);
    recorder.recordServiceType(OkHttpConstants.OK_HTTP_CLIENT_INTERNAL);
    recorder.recordException(throwable);
    if (target instanceof UserRequestGetter) {
        final Request request = ((UserRequestGetter) target)._$PINPOINT$_getUserRequest();
        if (request != null && request.httpUrl() != null) {
            final HttpUrl httpUrl = request.httpUrl();
            final String hostAndPort = HostAndPort.toHostAndPortString(httpUrl.host(), httpUrl.port());
            recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, hostAndPort);
        }
    }
}
Also used : UserRequestGetter(com.navercorp.pinpoint.plugin.okhttp.v2.UserRequestGetter) Request(com.squareup.okhttp.Request) HttpUrl(com.squareup.okhttp.HttpUrl)

Example 10 with HttpUrl

use of com.squareup.okhttp.HttpUrl 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);
    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, null);
    CalendarContainerResource resource = new CalendarContainerResource(singleton(calendarModel), singleton(eventModel));
    FakeIdempotentImportExecutor executor = new FakeIdempotentImportExecutor();
    ImportResult result = importer.importItem(JOB_ID, executor, 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", executor.getCachedValue("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.datatransferproject.transfer.microsoft.calendar.MicrosoftCalendarImporter) ImportResult(org.datatransferproject.spi.transfer.provider.ImportResult) FakeIdempotentImportExecutor(org.datatransferproject.test.types.FakeIdempotentImportExecutor) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) HttpUrl(com.squareup.okhttp.HttpUrl) CalendarAttendeeModel(org.datatransferproject.types.common.models.calendar.CalendarAttendeeModel) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Aggregations

HttpUrl (com.squareup.okhttp.HttpUrl)10 Request (com.squareup.okhttp.Request)5 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)5 Test (org.junit.Test)5 MockWebServer (com.squareup.okhttp.mockwebserver.MockWebServer)3 RecordedRequest (com.squareup.okhttp.mockwebserver.RecordedRequest)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 OkHttpClient (okhttp3.OkHttpClient)2 CalendarContainerResource (org.dataportabilityproject.types.transfer.models.calendar.CalendarContainerResource)2 CalendarContainerResource (org.datatransferproject.types.common.models.calendar.CalendarContainerResource)2 WorkerThread (android.support.annotation.WorkerThread)1 InterceptorScopeInvocation (com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation)1 UserRequestGetter (com.navercorp.pinpoint.plugin.okhttp.v2.UserRequestGetter)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Response (com.squareup.okhttp.Response)1