Java源码示例:org.eclipse.jetty.http.HttpMethods

示例1
@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);
}
 
示例2
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;
}
 
示例3
@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);
}
 
示例4
@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);

}
 
示例5
@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();
}
 
示例6
@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);
}
 
示例7
@Test
public void functionImport() throws Exception {
  assertEquals("foo", edmFunctionImport.getName());
  assertEquals(HttpMethods.GET, edmFunctionImport.getHttpMethod());

}
 
示例8
@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);
}