RangeParser implements Parser<List<Range>> { @Override public List<Range> parse(final String input) throws MalformedInputException { List<Range> rangeList = new ArrayList<>(); String inputNormalized = input.toLowerCase().trim(); if (!inputNormalized.startsWith(START_WORD)) { throw new MalformedInputException("Header value must start with bytes="); } String[] rangesString = inputNormalized.substring(START_WORD.length()).split(RANGES_SEPARATOR); for (String rangeString : rangesString) { if (rangeString.indexOf(RANGE_SEPARATOR) == -1) { throw new MalformedInputException("Invalid range value " + rangeString); } String[] values = rangeString.split(RANGE_SEPARATOR); if (values.length != 2) { throw new MalformedInputException("Invalid range value " + rangeString); } rangeList.add(getRange(values)); } return rangeList; }  @Override List<Range> parse(final String input);  }
@Test public void shouldParseBasicValue() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); List<Range> rageList = rangeParser.parse("bytes=1-100"); assertThat(rageList.size(), is(1)); assertThat(rageList.get(0).getFrom(), is(1L)); assertThat(rageList.get(0).getTo(), is(100L)); }
@Test public void shouldParseMultipleValues() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); List<Range> rageList = rangeParser.parse("bytes=1-100,200-301"); assertThat(rageList.size(), is(2)); assertThat(rageList.get(0).getFrom(), is(1L)); assertThat(rageList.get(0).getTo(), is(100L)); assertThat(rageList.get(1).getFrom(), is(200L)); assertThat(rageList.get(1).getTo(), is(301L)); }
@Test public void shouldParseMultipleValuesSpaceSeparated() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); List<Range> rageList = rangeParser.parse("bytes=1-100, 200-301"); assertThat(rageList.size(), is(2)); assertThat(rageList.get(0).getFrom(), is(1L)); assertThat(rageList.get(0).getTo(), is(100L)); assertThat(rageList.get(1).getFrom(), is(200L)); assertThat(rageList.get(1).getTo(), is(301L)); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionOnMissingLength() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes=100-"); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionOnMissingValues() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes="); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionWhenMissingFromValue() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes=-200"); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionNonParsableValues() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes=1-a"); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionOnIncompleteRange() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes=1"); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionOnUnrecognizedUnit() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("octets=1-100"); }