Java Code Examples for okhttp3.mockwebserver.RecordedRequest#getBody()

The following examples show how to use okhttp3.mockwebserver.RecordedRequest#getBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMockWebServerTestCase.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private MockResponse multipartRequest(RecordedRequest request) {
	MediaType mediaType = MediaType.parseMediaType(request.getHeader("Content-Type"));
	assertTrue(mediaType.isCompatibleWith(MediaType.MULTIPART_FORM_DATA));
	String boundary = mediaType.getParameter("boundary");
	Buffer body = request.getBody();
	try {
		assertPart(body, "form-data", boundary, "name 1", "text/plain", "value 1");
		assertPart(body, "form-data", boundary, "name 2", "text/plain", "value 2+1");
		assertPart(body, "form-data", boundary, "name 2", "text/plain", "value 2+2");
		assertFilePart(body, "form-data", boundary, "logo", "logo.jpg", "image/jpeg");
	}
	catch (EOFException ex) {
		throw new IllegalStateException(ex);
	}
	return new MockResponse().setResponseCode(200);
}
 
Example 2
Source File: AbstractMockWebServerTestCase.java    From java-technology-stack with MIT License 6 votes vote down vote up
private MockResponse multipartRequest(RecordedRequest request) {
	MediaType mediaType = MediaType.parseMediaType(request.getHeader("Content-Type"));
	assertTrue(mediaType.isCompatibleWith(MediaType.MULTIPART_FORM_DATA));
	String boundary = mediaType.getParameter("boundary");
	Buffer body = request.getBody();
	try {
		assertPart(body, "form-data", boundary, "name 1", "text/plain", "value 1");
		assertPart(body, "form-data", boundary, "name 2", "text/plain", "value 2+1");
		assertPart(body, "form-data", boundary, "name 2", "text/plain", "value 2+2");
		assertFilePart(body, "form-data", boundary, "logo", "logo.jpg", "image/jpeg");
	}
	catch (EOFException ex) {
		throw new IllegalStateException(ex);
	}
	return new MockResponse().setResponseCode(200);
}
 
Example 3
Source File: TestHttpNotificationServiceCommon.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testStartNotification() throws ParserConfigurationException, SAXException, IOException, InterruptedException {
    mockWebServer.enqueue(new MockResponse().setResponseCode(200));

    NotificationServiceManager notificationServiceManager = new NotificationServiceManager();
    notificationServiceManager.setMaxNotificationAttempts(1);
    notificationServiceManager.loadNotificationServices(new File(tempConfigFilePath));
    notificationServiceManager.registerNotificationService(NotificationType.NIFI_STARTED, "http-notification");
    notificationServiceManager.notify(NotificationType.NIFI_STARTED, "Subject", "Message");

    RecordedRequest recordedRequest = mockWebServer.takeRequest(2, TimeUnit.SECONDS);
    assertNotNull(recordedRequest);
    assertEquals(NotificationType.NIFI_STARTED.name(), recordedRequest.getHeader(NOTIFICATION_TYPE_KEY));
    assertEquals("Subject", recordedRequest.getHeader(NOTIFICATION_SUBJECT_KEY));
    assertEquals("testing", recordedRequest.getHeader("testProp"));

    Buffer bodyBuffer = recordedRequest.getBody();
    String bodyString =new String(bodyBuffer.readByteArray(), UTF_8);
    assertEquals("Message", bodyString);
}
 
Example 4
Source File: TestHttpNotificationServiceCommon.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testStopNotification() throws ParserConfigurationException, SAXException, IOException, InterruptedException {
    mockWebServer.enqueue(new MockResponse().setResponseCode(200));

    NotificationServiceManager notificationServiceManager = new NotificationServiceManager();
    notificationServiceManager.setMaxNotificationAttempts(1);
    notificationServiceManager.loadNotificationServices(new File(tempConfigFilePath));
    notificationServiceManager.registerNotificationService(NotificationType.NIFI_STOPPED, "http-notification");
    notificationServiceManager.notify(NotificationType.NIFI_STOPPED, "Subject", "Message");

    RecordedRequest recordedRequest = mockWebServer.takeRequest(2, TimeUnit.SECONDS);
    assertNotNull(recordedRequest);
    assertEquals(NotificationType.NIFI_STOPPED.name(), recordedRequest.getHeader(NOTIFICATION_TYPE_KEY));
    assertEquals("Subject", recordedRequest.getHeader(NOTIFICATION_SUBJECT_KEY));

    Buffer bodyBuffer = recordedRequest.getBody();
    String bodyString =new String(bodyBuffer.readByteArray(), UTF_8);
    assertEquals("Message", bodyString);
}
 
Example 5
Source File: TestHttpNotificationServiceCommon.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testDiedNotification() throws ParserConfigurationException, SAXException, IOException, InterruptedException {
    mockWebServer.enqueue(new MockResponse().setResponseCode(200));

    NotificationServiceManager notificationServiceManager = new NotificationServiceManager();
    notificationServiceManager.setMaxNotificationAttempts(1);
    notificationServiceManager.loadNotificationServices(new File(tempConfigFilePath));
    notificationServiceManager.registerNotificationService(NotificationType.NIFI_DIED, "http-notification");
    notificationServiceManager.notify(NotificationType.NIFI_DIED, "Subject", "Message");

    RecordedRequest recordedRequest = mockWebServer.takeRequest(2, TimeUnit.SECONDS);
    assertNotNull(recordedRequest);

    assertEquals(NotificationType.NIFI_DIED.name(), recordedRequest.getHeader(NOTIFICATION_TYPE_KEY));
    assertEquals("Subject", recordedRequest.getHeader(NOTIFICATION_SUBJECT_KEY));

    Buffer bodyBuffer = recordedRequest.getBody();
    String bodyString =new String(bodyBuffer.readByteArray(), UTF_8);
    assertEquals("Message", bodyString);
}
 
Example 6
Source File: TestHttpNotificationServiceCommon.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testStartNotificationFailure() throws ParserConfigurationException, SAXException, IOException, InterruptedException {
    // Web server will still get the request but will return an error. Observe that it is gracefully handled.

    mockWebServer.enqueue(new MockResponse().setResponseCode(500));

    NotificationServiceManager notificationServiceManager = new NotificationServiceManager();
    notificationServiceManager.setMaxNotificationAttempts(1);
    notificationServiceManager.loadNotificationServices(new File(tempConfigFilePath));
    notificationServiceManager.registerNotificationService(NotificationType.NIFI_STARTED, "http-notification");
    notificationServiceManager.notify(NotificationType.NIFI_STARTED, "Subject", "Message");

    RecordedRequest recordedRequest = mockWebServer.takeRequest(2, TimeUnit.SECONDS);
    assertNotNull(recordedRequest);
    assertEquals(NotificationType.NIFI_STARTED.name(), recordedRequest.getHeader(NOTIFICATION_TYPE_KEY));
    assertEquals("Subject", recordedRequest.getHeader(NOTIFICATION_SUBJECT_KEY));

    Buffer bodyBuffer = recordedRequest.getBody();
    String bodyString =new String(bodyBuffer.readByteArray(), UTF_8);
    assertEquals("Message", bodyString);
}
 
Example 7
Source File: MockServer.java    From auth0-java with MIT License 5 votes vote down vote up
public static Map<String, Object> bodyFromRequest(RecordedRequest request) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
    try (Buffer body = request.getBody()) {
        return mapper.readValue(body.inputStream(), mapType);
    }
}
 
Example 8
Source File: ZooniverseClientTest.java    From android-galaxyzoo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testLoginWithSuccess() throws IOException, InterruptedException, ZooniverseClient.LoginException {
    final MockWebServer server = new MockWebServer();

    final String strResponse = getStringFromStream(
            MoreItemsJsonParserTest.class.getClassLoader().getResourceAsStream("test_login_response_success.json"));
    assertNotNull(strResponse);
    server.enqueue(new MockResponse().setBody(strResponse));
    server.start();

    final ZooniverseClient client = createZooniverseClient(server);

    final LoginUtils.LoginResult result = client.loginSync("testusername", "testpassword");
    assertNotNull(result);
    assertTrue(result.getSuccess());
    assertEquals("testapikey", result.getApiKey());

    //Test what the server received:
    assertEquals(1, server.getRequestCount());
    final RecordedRequest request = server.takeRequest();
    assertEquals("POST", request.getMethod());
    assertEquals("/login", request.getPath());
    assertEquals("application/x-www-form-urlencoded", request.getHeader("Content-Type"));

    final Buffer contents = request.getBody();
    final String strContents = contents.readUtf8();
    assertEquals("username=testusername&password=testpassword",
            strContents);

    server.shutdown();
}
 
Example 9
Source File: MockServer.java    From auth0-java with MIT License 4 votes vote down vote up
public static String readFromRequest(RecordedRequest request) {
    try (Buffer body = request.getBody()) {
        return body.readUtf8();
    }
}
 
Example 10
Source File: ZooniverseClientTest.java    From android-galaxyzoo with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testUploadWithSuccess() throws IOException, InterruptedException, ZooniverseClient.UploadException {
    final MockWebServer server = new MockWebServer();


    final MockResponse response = new MockResponse();
    response.setResponseCode(HttpURLConnection.HTTP_CREATED);
    response.setBody("TODO");
    server.enqueue(response);
    server.start();

    final ZooniverseClient client = createZooniverseClient(server);

    //SyncAdapter.doUploadSync() adds an "interface" parameter too,
    //but we are testing a more generic API here:
    final List<HttpUtils.NameValuePair> values = new ArrayList<>();
    values.add(new HttpUtils.NameValuePair("classification[subject_ids][]", "504e4a38c499611ea6010c6a"));
    values.add(new HttpUtils.NameValuePair("classification[favorite][]", "true"));
    values.add(new HttpUtils.NameValuePair("classification[annotations][0][sloan-0]", "a-0"));
    values.add(new HttpUtils.NameValuePair("classification[annotations][1][sloan-7]", "a-1"));
    values.add(new HttpUtils.NameValuePair("classification[annotations][2][sloan-5]", "a-0"));
    values.add(new HttpUtils.NameValuePair("classification[annotations][3][sloan-6]", "x-5"));

    final boolean result = client.uploadClassificationSync("testAuthName",
            "testAuthApiKey", TEST_GROUP_ID, values);
    assertTrue(result);

    assertEquals(1, server.getRequestCount());

    //This is really just a regression test,
    //so we notice if something changes unexpectedly:
    final RecordedRequest request = server.takeRequest();
    assertEquals("POST", request.getMethod());
    assertEquals("/workflows/" + TEST_GROUP_ID + "/classifications", request.getPath());
    assertNotNull(request.getHeader("Authorization"));
    assertEquals("application/x-www-form-urlencoded", request.getHeader("Content-Type"));

    final Buffer contents = request.getBody();
    final String strContents = contents.readUtf8();
    assertEquals("classification%5Bsubject_ids%5D%5B%5D=504e4a38c499611ea6010c6a&classification%5Bfavorite%5D%5B%5D=true&classification%5Bannotations%5D%5B0%5D%5Bsloan-0%5D=a-0&classification%5Bannotations%5D%5B1%5D%5Bsloan-7%5D=a-1&classification%5Bannotations%5D%5B2%5D%5Bsloan-5%5D=a-0&classification%5Bannotations%5D%5B3%5D%5Bsloan-6%5D=x-5",
            strContents);

    server.shutdown();
}