|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.http; |
| 11 | + |
| 12 | +import org.elasticsearch.common.Strings; |
| 13 | +import org.elasticsearch.test.ESTestCase; |
| 14 | +import org.elasticsearch.test.fixture.HttpHeaderParser; |
| 15 | + |
| 16 | +import java.math.BigInteger; |
| 17 | + |
| 18 | +public class HttpHeaderParserTests extends ESTestCase { |
| 19 | + |
| 20 | + public void testParseRangeHeader() { |
| 21 | + final long start = randomLongBetween(0, 10_000); |
| 22 | + final long end = randomLongBetween(start, start + 10_000); |
| 23 | + assertEquals(new HttpHeaderParser.Range(start, end), HttpHeaderParser.parseRangeHeader("bytes=" + start + "-" + end)); |
| 24 | + } |
| 25 | + |
| 26 | + public void testParseRangeHeaderInvalidLong() { |
| 27 | + final BigInteger longOverflow = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE).add(randomBigInteger()); |
| 28 | + assertNull(HttpHeaderParser.parseRangeHeader("bytes=123-" + longOverflow)); |
| 29 | + assertNull(HttpHeaderParser.parseRangeHeader("bytes=" + longOverflow + "-123")); |
| 30 | + } |
| 31 | + |
| 32 | + public void testParseRangeHeaderMultipleRangesNotMatched() { |
| 33 | + assertNull( |
| 34 | + HttpHeaderParser.parseRangeHeader( |
| 35 | + Strings.format( |
| 36 | + "bytes=%d-%d,%d-%d", |
| 37 | + randomIntBetween(0, 99), |
| 38 | + randomIntBetween(100, 199), |
| 39 | + randomIntBetween(200, 299), |
| 40 | + randomIntBetween(300, 399) |
| 41 | + ) |
| 42 | + ) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + public void testParseRangeHeaderEndlessRangeNotMatched() { |
| 47 | + assertNull(HttpHeaderParser.parseRangeHeader(Strings.format("bytes=%d-", randomLongBetween(0, Long.MAX_VALUE)))); |
| 48 | + } |
| 49 | + |
| 50 | + public void testParseRangeHeaderSuffixLengthNotMatched() { |
| 51 | + assertNull(HttpHeaderParser.parseRangeHeader(Strings.format("bytes=-%d", randomLongBetween(0, Long.MAX_VALUE)))); |
| 52 | + } |
| 53 | +} |
0 commit comments