Skip to content

Commit

Permalink
allow empty headers (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf committed Feb 8, 2023
1 parent a97243d commit 116a208
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void main(String[] args) {
// Build our subscription options.
// * A push subscription means the server will "push" us messages.
// * Durable means the server will remember where we are if we use that name.
// * Durable can by null or empty, the builder treats them the same.
// * Durable can be null or empty, the builder treats them the same.
// * The stream name is not technically required. If it is not provided, the
// code building the subscription will look it up by making a request to the server.
// If you know the stream name, you might as well supply it and save a trip to the server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ private void initHeader(byte[] serialized, int len, Token tCrlf, boolean hadStat
peek = new Token(serialized, len, tCrlf, null);
}
peek.mustBe(TokenType.CRLF);

if ((headers == null || headers.size() == 0) && !hadStatus) {
throw new IllegalArgumentException(INVALID_HEADER_COMPOSITION);
}
}

private Token initStatus(byte[] serialized, int len, Token tSpace) {
Expand Down
19 changes: 11 additions & 8 deletions src/test/java/io/nats/client/impl/HeadersTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,11 @@ public void constructHeadersWithInvalidBytes() {
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0 \r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0X\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0 \r\n\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0\r\n\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0 503\r".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0 503\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0 FiveOhThree\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0\r\n\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0\r\n\r\n\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0\r\nk1:v1".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0\r\nk1:v1\r\n".getBytes()));
assertThrows(IllegalArgumentException.class, () -> new IncomingHeadersProcessor("NATS/1.0\r\nk1:v1\r\r\n".getBytes()));
Expand All @@ -492,6 +489,7 @@ public void constructHeadersWithValidBytes() {
assertValidHeader("NATS/1.0\r\nk1:\r\n\r\n", "k1", EMPTY);
assertValidHeader("NATS/1.0\r\nks1: \r\n\r\n", "ks1", EMPTY);
assertValidHeader("NATS/1.0\r\ncolons::::\r\n\r\n", "colons", ":::");
assertValidHeader("NATS/1.0\r\n\r\n\r\n", null, null);
}

@Test
Expand Down Expand Up @@ -540,11 +538,16 @@ private IncomingHeadersProcessor assertValidHeader(String test, String key, Stri

private IncomingHeadersProcessor assertValidHeader(IncomingHeadersProcessor ihp, String key, String val) {
Headers headers = ihp.getHeaders();
assertNotNull(headers);
assertEquals(1, headers.size());
assertTrue(headers.containsKey(key));
assertEquals(1, headers.get(key).size());
assertEquals(val, headers.get(key).get(0));
if (key == null) {
assertNull(headers);
}
else {
assertNotNull(headers);
assertEquals(1, headers.size());
assertTrue(headers.containsKey(key));
assertEquals(1, headers.get(key).size());
assertEquals(val, headers.get(key).get(0));
}
return ihp;
}

Expand Down

0 comments on commit 116a208

Please sign in to comment.