HttpRequest { public Range range() throws HttpException { final Range range; if (this.hdrs.containsKey(HttpRequest.RANGE_HEADER)) { final Matcher matcher = HttpRequest.RANGE_PATTERN.matcher( this.hdrs.get(HttpRequest.RANGE_HEADER).iterator().next() ); if (!matcher.matches()) { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, "invalid Range header format" ); } final long last; if (matcher.group(2) == null) { last = Long.MAX_VALUE; } else { last = Long.parseLong(matcher.group(2)); } range = new Range.Simple(Long.parseLong(matcher.group(1)), last); } else { range = Range.ENTIRE; } return range; } HttpRequest(@NotNull final Socket socket); Map<String, Collection<String>> headers(); Map<String, Collection<String>> parameters(); URI requestUri(); String method(); Range range();  }
@Test public void canFetchFullByteRange() throws Exception { final HttpRequest request = HttpRequestMocker.toRequest( new StringBuilder("GET /test.html HTTP/1.1\n") .append("Host:local\n") .append("Accept:text/plain\n") .append("Range: bytes=100-200\n\nbody") .toString() ); final Range range = request.range(); MatcherAssert.assertThat( range.first(), Matchers.is(100L) ); MatcherAssert.assertThat( range.last(), Matchers.is(200L) ); }
@Test public void canFetchRangeFromFirstByte() throws Exception { final HttpRequest request = HttpRequestMocker.toRequest( new StringBuilder("GET /test.html HTTP/1.1\n") .append("Host:local\n") .append("Accept:text/plain\n") .append("Range: bytes=100-\n\nbody") .toString() ); final Range range = request.range(); MatcherAssert.assertThat( range.first(), Matchers.is(100L) ); MatcherAssert.assertThat( range.last(), Matchers.is(Long.MAX_VALUE) ); }