@Test
  public void headers() throws URISyntaxException {
    MediaType accept = MediaType.TEXT_PLAIN;
    Charset charset = Charset.forName("UTF-8");
    long ifModifiedSince = 12345L;
    String ifNoneMatch = "\"foo\"";
    long contentLength = 67890;
    MediaType contentType = MediaType.TEXT_PLAIN;

    RequestEntity<Void> responseEntity =
        RequestEntity.post(new URI("http://example.com"))
            .accept(accept)
            .acceptCharset(charset)
            .ifModifiedSince(ifModifiedSince)
            .ifNoneMatch(ifNoneMatch)
            .contentLength(contentLength)
            .contentType(contentType)
            .build();

    assertNotNull(responseEntity);
    assertEquals(HttpMethod.POST, responseEntity.getMethod());
    assertEquals(new URI("http://example.com"), responseEntity.getUrl());
    HttpHeaders responseHeaders = responseEntity.getHeaders();

    assertEquals("text/plain", responseHeaders.getFirst("Accept"));
    assertEquals("utf-8", responseHeaders.getFirst("Accept-Charset"));
    assertEquals("Thu, 01 Jan 1970 00:00:12 GMT", responseHeaders.getFirst("If-Modified-Since"));
    assertEquals(ifNoneMatch, responseHeaders.getFirst("If-None-Match"));
    assertEquals(String.valueOf(contentLength), responseHeaders.getFirst("Content-Length"));
    assertEquals(contentType.toString(), responseHeaders.getFirst("Content-Type"));

    assertNull(responseEntity.getBody());
  }
예제 #2
0
 @Test
 public void parseURLConnectionMediaType() throws Exception {
   String s = "*; q=.2";
   MediaType mediaType = MediaType.parseMediaType(s);
   assertEquals("Invalid type", "*", mediaType.getType());
   assertEquals("Invalid subtype", "*", mediaType.getSubtype());
   assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D);
 }
예제 #3
0
 @Test
 public void parseCharset() throws Exception {
   String s = "text/html; charset=iso-8859-1";
   MediaType mediaType = MediaType.parseMediaType(s);
   assertEquals("Invalid type", "text", mediaType.getType());
   assertEquals("Invalid subtype", "html", mediaType.getSubtype());
   assertEquals("Invalid charset", Charset.forName("ISO-8859-1"), mediaType.getCharSet());
 }
예제 #4
0
 @Test
 public void parseQuotedCharset() {
   String s = "application/xml;charset=\"utf-8\"";
   MediaType mediaType = MediaType.parseMediaType(s);
   assertEquals("Invalid type", "application", mediaType.getType());
   assertEquals("Invalid subtype", "xml", mediaType.getSubtype());
   assertEquals("Invalid charset", Charset.forName("UTF-8"), mediaType.getCharSet());
 }
예제 #5
0
  @Test
  public void parseMediaTypes() throws Exception {
    String s = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c";
    List<MediaType> mediaTypes = MediaType.parseMediaTypes(s);
    assertNotNull("No media types returned", mediaTypes);
    assertEquals("Invalid amount of media types", 4, mediaTypes.size());

    mediaTypes = MediaType.parseMediaTypes(null);
    assertNotNull("No media types returned", mediaTypes);
    assertEquals("Invalid amount of media types", 0, mediaTypes.size());
  }
예제 #6
0
  @Test
  public void compareToConsistentWithEquals() {
    MediaType m1 = MediaType.parseMediaType("text/html; q=0.7; charset=iso-8859-1");
    MediaType m2 = MediaType.parseMediaType("text/html; charset=iso-8859-1; q=0.7");

    assertEquals("Media types not equal", m1, m2);
    assertEquals("compareTo() not consistent with equals", 0, m1.compareTo(m2));
    assertEquals("compareTo() not consistent with equals", 0, m2.compareTo(m1));

    m1 = MediaType.parseMediaType("text/html; q=0.7; charset=iso-8859-1");
    m2 = MediaType.parseMediaType("text/html; Q=0.7; charset=iso-8859-1");
    assertEquals("Media types not equal", m1, m2);
    assertEquals("compareTo() not consistent with equals", 0, m1.compareTo(m2));
    assertEquals("compareTo() not consistent with equals", 0, m2.compareTo(m1));
  }
예제 #7
0
  @Test
  public void sortByQualityRelated() {
    MediaType audioBasic = new MediaType("audio", "basic");
    MediaType audio = new MediaType("audio");
    MediaType audio03 = new MediaType("audio", "*", 0.3);
    MediaType audio07 = new MediaType("audio", "*", 0.7);
    MediaType audioBasicLevel =
        new MediaType("audio", "basic", Collections.singletonMap("level", "1"));
    MediaType all = MediaType.ALL;

    List<MediaType> expected = new ArrayList<MediaType>();
    expected.add(audioBasicLevel);
    expected.add(audioBasic);
    expected.add(audio);
    expected.add(all);
    expected.add(audio07);
    expected.add(audio03);

    List<MediaType> result = new ArrayList<MediaType>(expected);
    Random rnd = new Random();
    // shuffle & sort 10 times
    for (int i = 0; i < 10; i++) {
      Collections.shuffle(result, rnd);
      MediaType.sortByQualityValue(result);

      for (int j = 0; j < result.size(); j++) {
        assertSame("Invalid media type at " + j, expected.get(j), result.get(j));
      }
    }
  }
예제 #8
0
 @Test
 public void testWithConversionService() {
   ConversionService conversionService = new DefaultConversionService();
   assertTrue(conversionService.canConvert(String.class, MediaType.class));
   MediaType mediaType = MediaType.parseMediaType("application/xml");
   assertEquals(mediaType, conversionService.convert("application/xml", MediaType.class));
 }
예제 #9
0
  @Test
  public void compareTo() {
    MediaType audioBasic = new MediaType("audio", "basic");
    MediaType audio = new MediaType("audio");
    MediaType audioWave = new MediaType("audio", "wave");
    MediaType audioBasicLevel =
        new MediaType("audio", "basic", Collections.singletonMap("level", "1"));
    MediaType audioBasic07 = new MediaType("audio", "basic", 0.7);

    // equal
    assertEquals("Invalid comparison result", 0, audioBasic.compareTo(audioBasic));
    assertEquals("Invalid comparison result", 0, audio.compareTo(audio));
    assertEquals("Invalid comparison result", 0, audioBasicLevel.compareTo(audioBasicLevel));

    assertTrue("Invalid comparison result", audioBasicLevel.compareTo(audio) > 0);

    List<MediaType> expected = new ArrayList<MediaType>();
    expected.add(audio);
    expected.add(audioBasic);
    expected.add(audioBasicLevel);
    expected.add(audioBasic07);
    expected.add(audioWave);

    List<MediaType> result = new ArrayList<MediaType>(expected);
    Random rnd = new Random();
    // shuffle & sort 10 times
    for (int i = 0; i < 10; i++) {
      Collections.shuffle(result, rnd);
      Collections.sort(result);

      for (int j = 0; j < result.size(); j++) {
        assertSame("Invalid media type at " + j + ", run " + i, expected.get(j), result.get(j));
      }
    }
  }
예제 #10
0
  @Test
  public void compareToCaseSensitivity() {
    MediaType m1 = new MediaType("audio", "basic");
    MediaType m2 = new MediaType("Audio", "Basic");
    assertEquals("Invalid comparison result", 0, m1.compareTo(m2));
    assertEquals("Invalid comparison result", 0, m2.compareTo(m1));

    m1 = new MediaType("audio", "basic", Collections.singletonMap("foo", "bar"));
    m2 = new MediaType("audio", "basic", Collections.singletonMap("Foo", "bar"));
    assertEquals("Invalid comparison result", 0, m1.compareTo(m2));
    assertEquals("Invalid comparison result", 0, m2.compareTo(m1));

    m1 = new MediaType("audio", "basic", Collections.singletonMap("foo", "bar"));
    m2 = new MediaType("audio", "basic", Collections.singletonMap("foo", "Bar"));
    assertTrue("Invalid comparison result", m1.compareTo(m2) != 0);
    assertTrue("Invalid comparison result", m2.compareTo(m1) != 0);
  }
예제 #11
0
  @Test
  public void sortByQualityUnrelated() {
    MediaType audioBasic = new MediaType("audio", "basic");
    MediaType audioWave = new MediaType("audio", "wave");
    MediaType textHtml = new MediaType("text", "html");

    List<MediaType> expected = new ArrayList<MediaType>();
    expected.add(textHtml);
    expected.add(audioBasic);
    expected.add(audioWave);

    List<MediaType> result = new ArrayList<MediaType>(expected);
    MediaType.sortBySpecificity(result);

    for (int i = 0; i < result.size(); i++) {
      assertSame("Invalid media type at " + i, expected.get(i), result.get(i));
    }
  }
예제 #12
0
  @Test
  public void includes() throws Exception {
    MediaType textPlain = MediaType.TEXT_PLAIN;
    assertTrue("Equal types is not inclusive", textPlain.includes(textPlain));
    MediaType allText = new MediaType("text");

    assertTrue("All subtypes is not inclusive", allText.includes(textPlain));
    assertFalse("All subtypes is inclusive", textPlain.includes(allText));

    assertTrue("All types is not inclusive", MediaType.ALL.includes(textPlain));
    assertFalse("All types is inclusive", textPlain.includes(MediaType.ALL));

    assertTrue("All types is not inclusive", MediaType.ALL.includes(textPlain));
    assertFalse("All types is inclusive", textPlain.includes(MediaType.ALL));

    MediaType applicationSoapXml = new MediaType("application", "soap+xml");
    MediaType applicationWildcardXml = new MediaType("application", "*+xml");

    assertTrue(applicationSoapXml.includes(applicationSoapXml));
    assertTrue(applicationWildcardXml.includes(applicationWildcardXml));

    assertTrue(applicationWildcardXml.includes(applicationSoapXml));
    assertFalse(applicationSoapXml.includes(applicationWildcardXml));
  }
예제 #13
0
 @Test(expected = IllegalArgumentException.class)
 public void parseMediaTypeIllegalSubtype() {
   MediaType.parseMediaType("audio/basic)");
 }
예제 #14
0
 @Test
 public void testToString() throws Exception {
   MediaType mediaType = new MediaType("text", "plain", 0.7);
   String result = mediaType.toString();
   assertEquals("Invalid toString() returned", "text/plain;q=0.7", result);
 }
예제 #15
0
 @Test(expected = IllegalArgumentException.class)
 public void parseMediaTypeIllegalParameterAttribute() {
   MediaType.parseMediaType("audio/*;attr<=value");
 }
예제 #16
0
 @Test(expected = IllegalArgumentException.class)
 public void parseMediaTypeIllegalQuotedParameterValue() {
   MediaType.parseMediaType("audio/*;attr=\"");
 }
예제 #17
0
 @Test
 public void parseMediaTypeSingleQuotedParameterValue() {
   MediaType mediaType = MediaType.parseMediaType("audio/*;attr='v>alue'");
   assertEquals("'v>alue'", mediaType.getParameter("attr"));
 }
예제 #18
0
 @Test
 public void parseMediaTypeQuotedParameterValue() {
   MediaType mediaType = MediaType.parseMediaType("audio/*;attr=\"v>alue\"");
   assertEquals("\"v>alue\"", mediaType.getParameter("attr"));
 }
예제 #19
0
 @Test(expected = IllegalArgumentException.class)
 public void parseMediaTypeIllegalCharset() {
   MediaType.parseMediaType("text/html; charset=foo-bar");
 }
예제 #20
0
 @Test(expected = IllegalArgumentException.class)
 public void parseMediaTypeIllegalQualityFactor() {
   MediaType.parseMediaType("audio/basic;q=1.1");
 }
예제 #21
0
  @Test
  public void isCompatible() throws Exception {
    MediaType textPlain = MediaType.TEXT_PLAIN;
    assertTrue("Equal types is not compatible", textPlain.isCompatibleWith(textPlain));
    MediaType allText = new MediaType("text");

    assertTrue("All subtypes is not compatible", allText.isCompatibleWith(textPlain));
    assertTrue("All subtypes is not compatible", textPlain.isCompatibleWith(allText));

    assertTrue("All types is not compatible", MediaType.ALL.isCompatibleWith(textPlain));
    assertTrue("All types is not compatible", textPlain.isCompatibleWith(MediaType.ALL));

    assertTrue("All types is not compatible", MediaType.ALL.isCompatibleWith(textPlain));
    assertTrue("All types is compatible", textPlain.isCompatibleWith(MediaType.ALL));

    MediaType applicationSoapXml = new MediaType("application", "soap+xml");
    MediaType applicationWildcardXml = new MediaType("application", "*+xml");

    assertTrue(applicationSoapXml.isCompatibleWith(applicationSoapXml));
    assertTrue(applicationWildcardXml.isCompatibleWith(applicationWildcardXml));

    assertTrue(applicationWildcardXml.isCompatibleWith(applicationSoapXml));
    assertTrue(applicationSoapXml.isCompatibleWith(applicationWildcardXml));
  }
예제 #22
0
 @Test(expected = IllegalArgumentException.class)
 public void parseMediaTypeNoSubtypeSlash() {
   MediaType.parseMediaType("audio/");
 }
예제 #23
0
 @Test
 public void getDefaultQualityValue() {
   MediaType mediaType = new MediaType("text", "plain");
   assertEquals("Invalid quality value", 1, mediaType.getQualityValue(), 0D);
 }
예제 #24
0
 @Test(expected = IllegalArgumentException.class)
 public void parseMediaTypeTypeRange() {
   MediaType.parseMediaType("*/json");
 }