/** * Parses message headers. * * @param headersString * @return * @throws MalformedInputException */ @Override public Headers parse(final String headersString) throws MalformedInputException { return parse(headersString, true); }
private List<Locale> toLocales(final List<LocaleWithWeight> localesWithWeight) { Collections.sort(localesWithWeight); List<Locale> locales = new ArrayList<>(); for (LocaleWithWeight localeWithWeight : localesWithWeight) { locales.add(localeWithWeight.getLocale()); } return locales; }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionWhenMissingFromValue() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes=-200"); }
public ServiceContainer(final ServerConfig serverConfig) { HeadersParser headersParser = new HeadersParser(); requestWrapperFactory = new HttpServletRequestImplFactory(headersParser, new QueryStringParser(), new RequestStatusParser(), new CookieParser(), new MultipartHeadersPartParser(headersParser), serverConfig.getTempPath() ); responseFactory = new HttpServletResponseImplFactory( new HeadersSerializer(), new CookieHeaderSerializer(new DateProvider()), new StreamHelper( new RangeHelper(), new RangePartHeaderSerializer() ) ); threadPoolExecutor = new ThreadPoolExecutor(1, serverConfig.getMaxServerThreads(), DEFAULT_TIME_UNITS, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(serverConfig.getMaxServerThreads() * MAX_THREADS_MULTIPLIER), Executors.defaultThreadFactory(), new ServiceUnavailableHandler(responseFactory) ); httpErrorHandlerResolver = new HttpErrorHandlerResolverImpl(serverConfig); pathHelper = new PathHelper(); }
@Test public void shouldIgnoreInvalidWeights() throws MalformedInputException { String localeString = "pl-PL,fr;TT,ru;q=0.2,en;q=0.4,ro;q=0.3,pl"; List<Locale> locales = LOCALE_PARSER.parse(localeString); assertThat(locales.get(0), is(new Locale("pl"))); assertThat(locales.get(1), is(new Locale("en"))); assertThat(locales.get(2), is(new Locale("ro"))); assertThat(locales.get(3), is(new Locale("ru"))); }
@Test(expected = MalformedInputException.class) public void shouldThrowMalformedInputExceptionOnInvalidStatus() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); requestStatusParser.parse("GET HTTP/1.1"); } }
/** * {@inheritDoc} */ @Override public Enumeration getLocales() { if (!StringUtilities.isEmpty(headers.getHeader(Headers.HEADER_ACCEPT_LANGUAGE))) { try { return Collections.enumeration( new LocaleParser().parse(headers.getHeader(Headers.HEADER_ACCEPT_LANGUAGE))); } catch (MalformedInputException e) { // return null } } return null; }
private FileResourceProvider getFileResourceProvider(final ServerConfig serverConfig) { return new FileResourceProvider(new RangeParser(), new RangeHelper(), new RangePartHeaderSerializer(), serverConfig.getMimeTypeMapping(), serverConfig.getDocumentRootPath()); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionOnUnrecognizedUnit() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("octets=1-100"); } }
@Test public void shouldParseLocales() throws MalformedInputException { String localeString = "pl-PL,pl;q=0.8,en;q=0.4,ro;q=0.2,ru;q=0.2"; List<Locale> locales = LOCALE_PARSER.parse(localeString); assertThat(locales.get(0), is(new Locale("pl"))); assertThat(locales.get(1), is(new Locale("en"))); assertThat(locales.get(2), is(new Locale("ro"))); assertThat(locales.get(3), is(new Locale("ru"))); }
@Test public void shouldParseStatusString() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); RequestStatus requestStatus = requestStatusParser.parse("GET /home?param1=ABC¶m2=123 HTTP/1.1"); assertThat(requestStatus.getMethod(), is("GET")); assertThat(requestStatus.getQueryString(), is("param1=ABC¶m2=123")); assertThat(requestStatus.getUri(), is("/home")); assertThat(requestStatus.getProtocol(), is("HTTP/1.1")); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionNonParsableValues() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes=1-a"); }
@Test public void shouldHandleProperlyMissingWeight() throws MalformedInputException { String localeString = "pl-PL,ru;q=0.2,en;q=0.4,ro;q=0.3,pl"; List<Locale> locales = LOCALE_PARSER.parse(localeString); assertThat(locales.get(0), is(new Locale("pl"))); assertThat(locales.get(1), is(new Locale("en"))); assertThat(locales.get(2), is(new Locale("ro"))); assertThat(locales.get(3), is(new Locale("ru"))); }
@Test public void shouldIgnoreTrailingCharacters() throws MalformedInputException { Parser<RequestStatus> requestStatusParser = new RequestStatusParser(); RequestStatus requestStatus = requestStatusParser.parse("GET /home?param1=ABC¶m2=123 HTTP/1.1\r\n"); assertThat(requestStatus.getMethod(), is("GET")); assertThat(requestStatus.getQueryString(), is("param1=ABC¶m2=123")); assertThat(requestStatus.getUri(), is("/home")); assertThat(requestStatus.getProtocol(), is("HTTP/1.1")); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionOnMissingLength() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes=100-"); }
@Test public void shouldParseLocalesAndRespectWeights() throws MalformedInputException { String localeString = "pl-PL,ru;q=0.2,en;q=0.4,ro;q=0.3,pl;q=0.8"; List<Locale> locales = LOCALE_PARSER.parse(localeString); assertThat(locales.get(0), is(new Locale("pl"))); assertThat(locales.get(1), is(new Locale("en"))); assertThat(locales.get(2), is(new Locale("ro"))); assertThat(locales.get(3), is(new Locale("ru"))); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionOnMissingValues() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes="); }
@Test(expected = MalformedInputException.class) public void shouldThrowExceptionOnIncompleteRange() throws MalformedInputException { RangeParser rangeParser = new RangeParser(); rangeParser.parse("bytes=1"); }
@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)); }