org.eclipse.jetty.http.HttpMethods Java Examples
The following examples show how to use
org.eclipse.jetty.http.HttpMethods.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetricsFilterTest.java From client_java with Apache License 2.0 | 6 votes |
@Test public void doFilter() throws Exception { HttpServletRequest req = mock(HttpServletRequest.class); final String path = "/foo/bar/baz/bang/zilch/zip/nada"; when(req.getRequestURI()).thenReturn(path); when(req.getMethod()).thenReturn(HttpMethods.GET); HttpServletResponse res = mock(HttpServletResponse.class); FilterChain c = mock(FilterChain.class); String name = "foo"; FilterConfig cfg = mock(FilterConfig.class); when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn(name); when(cfg.getInitParameter(MetricsFilter.PATH_COMPONENT_PARAM)).thenReturn("0"); f.init(cfg); f.doFilter(req, res, c); verify(c).doFilter(req, res); final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(name + "_count", new String[]{"path", "method"}, new String[]{path, HttpMethods.GET}); assertNotNull(sampleValue); assertEquals(1, sampleValue, 0.0001); }
Example #2
Source File: CrossOriginConstraintSecurityHandler.java From sql-layer with GNU Affero General Public License v3.0 | 5 votes |
private static boolean isPreFlightRequest(Request request) { if(HttpMethods.OPTIONS.equalsIgnoreCase(request.getMethod())) { // If the origin does not match allowed the filter will skip anyway so don't bother checking it. if(request.getHeader(ORIGIN_HEADER) != null && request.getHeader(CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD_HEADER) != null) { return true; } } return false; }
Example #3
Source File: MetricsFilterTest.java From client_java with Apache License 2.0 | 5 votes |
@Test public void testConstructor() throws Exception { HttpServletRequest req = mock(HttpServletRequest.class); final String path = "/foo/bar/baz/bang"; when(req.getRequestURI()).thenReturn(path); when(req.getMethod()).thenReturn(HttpMethods.POST); FilterChain c = mock(FilterChain.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { Thread.sleep(100); return null; } }).when(c).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); MetricsFilter constructed = new MetricsFilter( "foobar_baz_filter_duration_seconds", "Help for my filter", 0, null ); constructed.init(mock(FilterConfig.class)); HttpServletResponse res = mock(HttpServletResponse.class); constructed.doFilter(req, res, c); final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foobar_baz_filter_duration_seconds_sum", new String[]{"path", "method"}, new String[]{path, HttpMethods.POST}); assertNotNull(sum); assertEquals(0.1, sum, 0.01); }
Example #4
Source File: EdmFunctionImportImplProvTest.java From cloud-odata-java with Apache License 2.0 | 5 votes |
@BeforeClass public static void getEdmEntityContainerImpl() throws Exception { EdmProvider edmProvider = mock(EdmProvider.class); EdmImplProv edmImplProv = new EdmImplProv(edmProvider); EntityContainerInfo containerInfo = new EntityContainerInfo().setName("Container"); when(edmProvider.getEntityContainerInfo("Container")).thenReturn(containerInfo); edmEntityContainer = new EdmEntityContainerImplProv(edmImplProv, containerInfo); EntitySet fooEntitySet = new EntitySet().setName("fooEntitySet"); when(edmProvider.getEntitySet("Container", "fooEntitySet")).thenReturn(fooEntitySet); ReturnType fooReturnType = new ReturnType().setTypeName(EdmSimpleTypeKind.String.getFullQualifiedName()).setMultiplicity(EdmMultiplicity.ONE); List<FunctionImportParameter> parameters = new ArrayList<FunctionImportParameter>(); FunctionImportParameter parameter = new FunctionImportParameter().setName("fooParameter1").setType(EdmSimpleTypeKind.String); parameters.add(parameter); parameter = new FunctionImportParameter().setName("fooParameter2").setType(EdmSimpleTypeKind.String); parameters.add(parameter); parameter = new FunctionImportParameter().setName("fooParameter3").setType(EdmSimpleTypeKind.String); parameters.add(parameter); FunctionImport functionImportFoo = new FunctionImport().setName("foo").setHttpMethod(HttpMethods.GET).setReturnType(fooReturnType).setEntitySet("fooEntitySet").setParameters(parameters); when(edmProvider.getFunctionImport("Container", "foo")).thenReturn(functionImportFoo); edmFunctionImport = new EdmFunctionImportImplProv(edmImplProv, functionImportFoo, edmEntityContainer); FunctionImport functionImportBar = new FunctionImport().setName("bar").setHttpMethod(HttpMethods.GET); when(edmProvider.getFunctionImport("Container", "bar")).thenReturn(functionImportBar); edmFunctionImportWithoutParameters = new EdmFunctionImportImplProv(edmImplProv, functionImportBar, edmEntityContainer); }
Example #5
Source File: JsonErrorHandler.java From sql-layer with GNU Affero General Public License v3.0 | 4 votes |
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { baseRequest.setHandled(true); String method = request.getMethod(); if(!method.equals(HttpMethods.HEAD) && !method.equals(HttpMethods.GET) && !method.equals(HttpMethods.POST) && !method.equals(PATCH_METHOD) && !method.equals(HttpMethods.PUT) && !method.equals(HttpMethods.DELETE)) { return; } final String message; final ErrorCode error; final String note; if(response.getStatus() == HttpServletResponse.SC_NOT_FOUND) { message = "Path not found"; if (!request.getRequestURI().contains("/v1/")) { note = "try including /v1/ in the path"; } else { note = null; } error = ErrorCode.MALFORMED_REQUEST; } else { if (response instanceof Response) { note = ((Response)response).getReason(); } else { note = null; } message = HttpStatus.getMessage(response.getStatus()); error = ErrorCode.INTERNAL_ERROR; } response.setContentType(MediaType.APPLICATION_JSON); response.setHeader(HttpHeaders.CACHE_CONTROL, getCacheControl()); StringBuilder builder = new StringBuilder(); RestResponseBuilder.formatJsonError(builder, error.getFormattedValue(), message, note); builder.append('\n'); response.setContentLength(builder.length()); OutputStream out = response.getOutputStream(); out.write(builder.toString().getBytes()); out.close(); }
Example #6
Source File: MetricsFilterTest.java From client_java with Apache License 2.0 | 4 votes |
@Test public void testBucketsAndName() throws Exception { HttpServletRequest req = mock(HttpServletRequest.class); final String path = "/foo/bar/baz/bang"; when(req.getRequestURI()).thenReturn(path); when(req.getMethod()).thenReturn(HttpMethods.POST); FilterChain c = mock(FilterChain.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { Thread.sleep(100); return null; } }).when(c).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); final String buckets = "0.01,0.05,0.1,0.15,0.25"; FilterConfig cfg = mock(FilterConfig.class); when(cfg.getInitParameter(MetricsFilter.BUCKET_CONFIG_PARAM)).thenReturn(buckets); when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn("foo"); HttpServletResponse res = mock(HttpServletResponse.class); f.init(cfg); f.doFilter(req, res, c); final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foo_sum", new String[]{"path", "method"}, new String[]{"/foo", HttpMethods.POST}); assertEquals(0.1, sum, 0.01); final Double le05 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", HttpMethods.POST, "0.05"}); assertNotNull(le05); assertEquals(0, le05, 0.01); final Double le15 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", HttpMethods.POST, "0.15"}); assertNotNull(le15); assertEquals(1, le15, 0.01); final Enumeration<Collector.MetricFamilySamples> samples = CollectorRegistry.defaultRegistry.metricFamilySamples(); Collector.MetricFamilySamples sample = null; while(samples.hasMoreElements()) { sample = samples.nextElement(); if (sample.name.equals("foo")) { break; } } assertNotNull(sample); int count = 0; for (Collector.MetricFamilySamples.Sample s : sample.samples) { if (s.name.equals("foo_bucket")) { count++; } } // +1 because of the final le=+infinity bucket assertEquals(buckets.split(",").length+1, count); }
Example #7
Source File: EdmFunctionImportImplProvTest.java From cloud-odata-java with Apache License 2.0 | 4 votes |
@Test public void functionImport() throws Exception { assertEquals("foo", edmFunctionImport.getName()); assertEquals(HttpMethods.GET, edmFunctionImport.getHttpMethod()); }
Example #8
Source File: MetricsFilterTest.java From client_java with Apache License 2.0 | 3 votes |
@Test public void init() throws Exception { FilterConfig cfg = mock(FilterConfig.class); when(cfg.getInitParameter(anyString())).thenReturn(null); String metricName = "foo"; when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn(metricName); when(cfg.getInitParameter(MetricsFilter.PATH_COMPONENT_PARAM)).thenReturn("4"); f.init(cfg); assertEquals(f.pathComponents, 4); HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn("/foo/bar/baz/bang/zilch/zip/nada"); when(req.getMethod()).thenReturn(HttpMethods.GET); HttpServletResponse res = mock(HttpServletResponse.class); FilterChain c = mock(FilterChain.class); f.doFilter(req, res, c); verify(c).doFilter(req, res); final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(metricName + "_count", new String[]{"path", "method"}, new String[]{"/foo/bar/baz/bang", HttpMethods.GET}); assertNotNull(sampleValue); assertEquals(1, sampleValue, 0.0001); }