@Test public void noPseudoHeaders() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Invalid character"); Metadata.Key.of(":test-bin", FISH_MARSHALLER); }
@Test public void invalidSuffixBinaryKeyName() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Binary header is named"); Metadata.Key.of("nonbinary", FISH_MARSHALLER); }
@Test public void verifyToString() { Metadata h = new Metadata(); h.put(KEY, new Fish("binary")); h.put(Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER), "ascii"); assertEquals("Metadata(test-bin=YmluYXJ5,test=ascii)", h.toString()); Metadata t = new Metadata(); t.put(Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER), "ascii"); assertEquals("Metadata(test=ascii)", t.toString()); t = new Metadata("test".getBytes(US_ASCII), "ascii".getBytes(US_ASCII), "test-bin".getBytes(US_ASCII), "binary".getBytes(US_ASCII)); assertEquals("Metadata(test=ascii,test-bin=YmluYXJ5)", t.toString()); }
@Test public void verifyToString_usingBinary() { Metadata h = new Metadata(); h.put(KEY, new Fish("binary")); h.put(Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER), "ascii"); assertEquals("Metadata(test-bin=YmluYXJ5,test=ascii)", h.toString()); Metadata t = new Metadata(); t.put(Metadata.Key.of("test", Metadata.ASCII_STRING_MARSHALLER), "ascii"); assertEquals("Metadata(test=ascii)", t.toString()); }
@Test public void testKeyCaseHandling() { Locale originalLocale = Locale.getDefault(); Locale.setDefault(new Locale("tr", "TR")); try { // In Turkish, both I and i (which are in ASCII) change into non-ASCII characters when their // case is changed as ı and İ, respectively. assertEquals("İ", "i".toUpperCase()); assertEquals("ı", "I".toLowerCase()); Metadata.Key<String> keyTitleCase = Metadata.Key.of("If-Modified-Since", Metadata.ASCII_STRING_MARSHALLER); Metadata.Key<String> keyLowerCase = Metadata.Key.of("if-modified-since", Metadata.ASCII_STRING_MARSHALLER); Metadata.Key<String> keyUpperCase = Metadata.Key.of("IF-MODIFIED-SINCE", Metadata.ASCII_STRING_MARSHALLER); Metadata metadata = new Metadata(); metadata.put(keyTitleCase, "plain string"); assertEquals("plain string", metadata.get(keyTitleCase)); assertEquals("plain string", metadata.get(keyLowerCase)); assertEquals("plain string", metadata.get(keyUpperCase)); byte[][] bytes = metadata.serialize(); assertEquals(2, bytes.length); assertArrayEquals("if-modified-since".getBytes(US_ASCII), bytes[0]); assertArrayEquals("plain string".getBytes(US_ASCII), bytes[1]); } finally { Locale.setDefault(originalLocale); } }
@Test public void removeIgnoresMissingValue() { Metadata m = new Metadata(); // Any key will work. Key<String> key = GrpcUtil.USER_AGENT_KEY; boolean success = m.remove(key, "agent"); assertFalse(success); }
@Test public void removeAllIgnoresMissingValue() { Metadata m = new Metadata(); // Any key will work. Key<String> key = GrpcUtil.USER_AGENT_KEY; Iterable<String> removed = m.removeAll(key); assertNull(removed); }
@Test public void keyEqualsHashNameWorks() { Key<?> k1 = Key.of("case", Metadata.ASCII_STRING_MARSHALLER); Key<?> k2 = Key.of("CASE", Metadata.ASCII_STRING_MARSHALLER); assertEquals(k1, k1); assertNotEquals(k1, null); assertNotEquals(k1, new Object(){}); assertEquals(k1, k2); assertEquals(k1.hashCode(), k2.hashCode()); // Check that the casing is preserved. assertEquals("CASE", k2.originalName()); assertEquals("case", k2.name()); }
@Test public void invalidKeyName() { try { Key.of("io.grpc/key1", Metadata.ASCII_STRING_MARSHALLER); fail("Should have thrown"); } catch (IllegalArgumentException e) { assertEquals("Invalid character '/' in key name 'io.grpc/key1'", e.getMessage()); } }
@Internal public static <T> Key<T> keyOf(String name, TrustedAsciiMarshaller<T> marshaller) { boolean isPseudo = name != null && !name.isEmpty() && name.charAt(0) == ':'; return Metadata.Key.of(name, isPseudo, marshaller); }
@Internal public static <T> Key<T> keyOf(String name, AsciiMarshaller<T> marshaller) { boolean isPseudo = name != null && !name.isEmpty() && name.charAt(0) == ':'; return Metadata.Key.of(name, isPseudo, marshaller); }
@Test public void shortBinaryKeyName() { thrown.expect(IllegalArgumentException.class); Metadata.Key.of("-bin", FISH_MARSHALLER); }
@Test(expected = IllegalArgumentException.class) public void binaryHeaderWithoutSuffix() { Key.of("plainstring", UTF8_STRING_MARSHALLER); }