|
32 | 32 | import java.util.Arrays; |
33 | 33 | import java.util.Collections; |
34 | 34 | import java.util.LinkedHashSet; |
| 35 | +import java.util.LinkedList; |
35 | 36 | import java.util.List; |
36 | 37 | import java.util.Locale; |
37 | 38 | import java.util.Set; |
|
44 | 45 | import org.apache.hc.core5.http.HttpMessage; |
45 | 46 | import org.apache.hc.core5.http.HttpResponse; |
46 | 47 | import org.apache.hc.core5.http.HttpStatus; |
| 48 | +import org.apache.hc.core5.http.Method; |
47 | 49 | import org.apache.hc.core5.http.NameValuePair; |
| 50 | +import org.apache.hc.core5.http.ProtocolException; |
48 | 51 | import org.apache.hc.core5.http.io.entity.HttpEntities; |
49 | 52 | import org.apache.hc.core5.http.support.BasicResponseBuilder; |
50 | 53 | import org.apache.hc.core5.util.CharArrayBuffer; |
@@ -330,6 +333,88 @@ void testParseParams() { |
330 | 333 | Assertions.assertFalse(cursor.atEnd()); |
331 | 334 | } |
332 | 335 |
|
| 336 | + static String copyHeader(final CharSequence charSequence, final ParserCursor cursor) { |
| 337 | + final CharArrayBuffer buf = new CharArrayBuffer(10); |
| 338 | + int pos = cursor.getPos(); |
| 339 | + while (pos < cursor.getUpperBound()) { |
| 340 | + final char ch = charSequence.charAt(pos); |
| 341 | + buf.append(ch); |
| 342 | + pos++; |
| 343 | + } |
| 344 | + cursor.updatePos(pos); |
| 345 | + return buf.toString(); |
| 346 | + } |
| 347 | + |
| 348 | + static String copyToken(final CharSequence charSequence, final ParserCursor cursor) { |
| 349 | + final CharArrayBuffer buf = new CharArrayBuffer(10); |
| 350 | + int pos = cursor.getPos(); |
| 351 | + while (pos < cursor.getUpperBound()) { |
| 352 | + final char ch = charSequence.charAt(pos); |
| 353 | + if (ch == ',') { |
| 354 | + break; |
| 355 | + } |
| 356 | + buf.append(ch); |
| 357 | + pos++; |
| 358 | + } |
| 359 | + cursor.updatePos(pos); |
| 360 | + return buf.substringTrimmed(0, buf.length()); |
| 361 | + } |
| 362 | + |
| 363 | + @Test |
| 364 | + void testParseHeaders() { |
| 365 | + final HttpMessage message = new BasicHttpRequest(Method.GET, "/"); |
| 366 | + message.addHeader("Some-Header", "this"); |
| 367 | + message.addHeader("Some-Header", "that"); |
| 368 | + message.addHeader("Some-Header", " this, that, what not"); |
| 369 | + |
| 370 | + final List<String> headerValues = new LinkedList<>(); |
| 371 | + MessageSupport.parseHeaders(message, "Some-header", (charSequence, cursor) -> { |
| 372 | + final String headerValue = copyHeader(charSequence, cursor); |
| 373 | + headerValues.add(headerValue); |
| 374 | + }); |
| 375 | + Assertions.assertEquals(Arrays.asList("this", "that", " this, that, what not"), headerValues); |
| 376 | + |
| 377 | + final List<String> tokens = new LinkedList<>(); |
| 378 | + MessageSupport.parseElementList(message, "Some-header", (charSequence, cursor) -> { |
| 379 | + final String token = copyToken(charSequence, cursor); |
| 380 | + tokens.add(token); |
| 381 | + }); |
| 382 | + Assertions.assertEquals(Arrays.asList("this", "that", "this", "that", "what not"), tokens); |
| 383 | + } |
| 384 | + |
| 385 | + @Test |
| 386 | + void testParseHeadersStrict() throws Exception { |
| 387 | + final HttpMessage message = new BasicHttpRequest(Method.GET, "/"); |
| 388 | + message.addHeader("Some-Header", "this"); |
| 389 | + message.addHeader("Some-Header", "that"); |
| 390 | + message.addHeader("Some-Header", " this, that, what not"); |
| 391 | + |
| 392 | + final List<String> headerValues = new LinkedList<>(); |
| 393 | + MessageSupport.parseHeadersStrict(message, "Some-header", (charSequence, cursor) -> { |
| 394 | + final String headerValue = copyHeader(charSequence, cursor); |
| 395 | + headerValues.add(headerValue); |
| 396 | + }); |
| 397 | + Assertions.assertEquals(Arrays.asList("this", "that", " this, that, what not"), headerValues); |
| 398 | + |
| 399 | + final List<String> tokens = new LinkedList<>(); |
| 400 | + MessageSupport.parseElementListStrict(message, "Some-header", (charSequence, cursor) -> { |
| 401 | + final String token = copyToken(charSequence, cursor); |
| 402 | + tokens.add(token); |
| 403 | + }); |
| 404 | + Assertions.assertEquals(Arrays.asList("this", "that", "this", "that", "what not"), tokens); |
| 405 | + |
| 406 | + Assertions.assertThrows(ProtocolException.class, () -> |
| 407 | + MessageSupport.parseElementListStrict( |
| 408 | + message, |
| 409 | + "Some-header", |
| 410 | + (charSequence, cursor) -> { |
| 411 | + final String token = copyToken(charSequence, cursor); |
| 412 | + if (token.equalsIgnoreCase("what not")) { |
| 413 | + throw new ProtocolException("How awful!"); |
| 414 | + } |
| 415 | + })); |
| 416 | + } |
| 417 | + |
333 | 418 | @Test |
334 | 419 | void testAddContentHeaders() { |
335 | 420 | final HttpEntity entity = HttpEntities.create("some stuff with trailers", StandardCharsets.US_ASCII, |
|
0 commit comments