Java Code Examples for org.apache.olingo.commons.api.http.HttpHeader#CONTENT_LENGTH

The following examples show how to use org.apache.olingo.commons.api.http.HttpHeader#CONTENT_LENGTH . 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: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void negativeContentLengthRequest() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.PATCH + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + HttpHeader.CONTENT_LENGTH + ": -2" + CRLF
      + CRLF
      + "{\"PropertyString\":\"new\"}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  parseInvalidBatchBody(batch, BatchDeserializerException.MessageKeys.INVALID_CONTENT_LENGTH);
}
 
Example 2
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void nonNumericContentLength() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.PATCH + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 10abc" + CRLF
      + CRLF
      + "{\"PropertyString\":\"new\"}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  parseInvalidBatchBody(batch, BatchDeserializerException.MessageKeys.INVALID_CONTENT_LENGTH);
}
 
Example 3
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void contentTypeCaseInsensitive() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 200" + CRLF
      + CRLF
      + HttpMethod.PATCH + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + "{\"PropertyString\":\"new\"}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  parse(batch);
}
 
Example 4
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void imageInContent() throws Exception {
  final String content = IOUtils.toString(readFile("/batchWithContent.batch"));
  final String batch = "--" + BOUNDARY + CRLF
      + GET_REQUEST
      + "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.POST + " ESMedia" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 100000" + CRLF
      + HttpHeader.CONTENT_TYPE + ": image/jpeg" + CRLF
      + "Content-Transfer-Encoding: base64" + CRLF
      + CRLF
      + content
      + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + "--" + BOUNDARY + "--";
  final List<BatchRequestPart> BatchRequestParts = parse(batch);

  for (final BatchRequestPart part : BatchRequestParts) {
    Assert.assertEquals(1, part.getRequests().size());
    final ODataRequest request = part.getRequests().get(0);
    if (!part.isChangeSet()) {
      Assert.assertEquals(HttpMethod.GET, request.getMethod());
      Assert.assertEquals(SERVICE_ROOT + "/" + PROPERTY_URI, request.getRawRequestUri());
      Assert.assertEquals(SERVICE_ROOT, request.getRawBaseUri());
      Assert.assertEquals("/" + PROPERTY_URI, request.getRawODataPath());
    } else {
      Assert.assertEquals(HttpMethod.POST, request.getMethod());
      Assert.assertEquals("100000", request.getHeader(HttpHeader.CONTENT_LENGTH));
      Assert.assertEquals("1", request.getHeader(HttpHeader.CONTENT_ID));
      Assert.assertEquals("image/jpeg", request.getHeader(HttpHeader.CONTENT_TYPE));
      Assert.assertEquals(content, IOUtils.toString(request.getBody()));
    }
  }
}
 
Example 5
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void postWithoutBody() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": changeRequest1" + CRLF
      + CRLF
      + HttpMethod.POST + " ESAllPrim" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 100" + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_OCTET_STREAM + CRLF
      + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";
  final List<BatchRequestPart> batchRequestParts = parse(batch);

  Assert.assertEquals(1, batchRequestParts.size());
  Assert.assertTrue(batchRequestParts.get(0).isChangeSet());
  Assert.assertEquals(1, batchRequestParts.get(0).getRequests().size());
  final ODataRequest request = batchRequestParts.get(0).getRequests().get(0);
  Assert.assertEquals(HttpMethod.POST, request.getMethod());
  Assert.assertEquals("100", request.getHeader(HttpHeader.CONTENT_LENGTH));
  Assert.assertEquals(APPLICATION_OCTET_STREAM, request.getHeader(HttpHeader.CONTENT_TYPE));
  Assert.assertNotNull(request.getBody());
  Assert.assertEquals(-1, request.getBody().read());
}
 
Example 6
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void contentLengthGreatherThanBodyLength() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.PATCH + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 100000" + CRLF
      + CRLF
      + "{\"PropertyString\":\"new\"}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";
  final List<BatchRequestPart> batchRequestParts = parse(batch);

  Assert.assertNotNull(batchRequestParts);
  Assert.assertEquals(1, batchRequestParts.size());

  final BatchRequestPart part = batchRequestParts.get(0);
  Assert.assertTrue(part.isChangeSet());
  Assert.assertEquals(1, part.getRequests().size());

  final ODataRequest request = part.getRequests().get(0);
  Assert.assertEquals("{\"PropertyString\":\"new\"}", IOUtils.toString(request.getBody()));
}
 
Example 7
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void contentLengthSmallerThanBodyLength() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.PATCH + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 10" + CRLF
      + CRLF
      + "{\"PropertyString\":\"new\"}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";
  final List<BatchRequestPart> batchRequestParts = parse(batch);

  Assert.assertNotNull(batchRequestParts);
  Assert.assertEquals(1, batchRequestParts.size());

  final BatchRequestPart part = batchRequestParts.get(0);
  Assert.assertTrue(part.isChangeSet());
  Assert.assertEquals(1, part.getRequests().size());

  final ODataRequest request = part.getRequests().get(0);
  Assert.assertEquals("{\"Property", IOUtils.toString(request.getBody()));
}
 
Example 8
Source File: BatchRequestParserTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void basic() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.GET + " " + PROPERTY_URI + "?$format=json" + HTTP_VERSION + CRLF
      + HttpHeader.ACCEPT_LANGUAGE + ":en-US,en;q=0.7,en-GB;q=0.9" + CRLF
      + CRLF
      + CRLF
      + "--" + BOUNDARY +CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": changeRequest1" + CRLF
      + CRLF
      + HttpMethod.PUT + " " + PROPERTY_URI + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 100000" + CRLF
      + ACCEPT_HEADER
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + "{\"value\":\"€ MODIFIED\"}" + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + CRLF
      + MIME_HEADERS
      + CRLF
      + HttpMethod.GET + " " + PROPERTY_URI + "?$format=json" + HTTP_VERSION + CRLF
      + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";
  final List<BatchRequestPart> batchRequestParts = parse(batch);

  Assert.assertNotNull(batchRequestParts);
  Assert.assertFalse(batchRequestParts.isEmpty());

  for (final BatchRequestPart object : batchRequestParts) {
    Assert.assertEquals(1, object.getRequests().size());
    final ODataRequest request = object.getRequests().get(0);
    Assert.assertEquals(SERVICE_ROOT, request.getRawBaseUri());
    Assert.assertEquals("/" + PROPERTY_URI, request.getRawODataPath());

    if (!object.isChangeSet()) {
      Assert.assertEquals(HttpMethod.GET, request.getMethod());

      if (request.getHeaders(HttpHeader.ACCEPT_LANGUAGE) != null) {
        Assert.assertEquals(3, request.getHeaders(HttpHeader.ACCEPT_LANGUAGE).size());
      }

      Assert.assertEquals(SERVICE_ROOT + "/" + PROPERTY_URI + "?$format=json", request.getRawRequestUri());
      Assert.assertEquals("$format=json", request.getRawQueryPath());

    } else {
      Assert.assertEquals(HttpMethod.PUT, request.getMethod());
      Assert.assertEquals("100000", request.getHeader(HttpHeader.CONTENT_LENGTH));
      Assert.assertEquals(APPLICATION_JSON, request.getHeader(HttpHeader.CONTENT_TYPE));

      final List<String> acceptHeader = request.getHeaders(HttpHeader.ACCEPT);
      Assert.assertEquals(4, request.getHeaders(HttpHeader.ACCEPT).size());
      Assert.assertEquals("application/atom+xml;q=0.8", acceptHeader.get(2));
      Assert.assertEquals("*/*;q=0.1", acceptHeader.get(3));

      Assert.assertEquals(SERVICE_ROOT + "/" + PROPERTY_URI, request.getRawRequestUri());
      Assert.assertEquals("", request.getRawQueryPath()); // No query parameter

      Assert.assertEquals("{\"value\":\"€ MODIFIED\"}" + CRLF, IOUtils.toString(request.getBody()));
    }
  }
}