Ejemplo n.º 1
0
 @Test
 public void testBadURIResource() throws ExecutionException, InterruptedException {
   initiateWebApplication(BadURIResource.class, URIStringReaderProvider.class);
   final ContainerResponse responseContext =
       getResponseContext(UriBuilder.fromPath("/").queryParam("d", "::::123").build().toString());
   assertEquals(404, responseContext.getStatus());
 }
 private void _test(ApplicationHandler handler, String requestUri, String expected)
     throws InterruptedException, ExecutionException {
   final ContainerResponse response =
       handler.apply(RequestContextBuilder.from(requestUri, "GET").build()).get();
   assertEquals(200, response.getStatus());
   assertEquals(expected, response.getEntity());
 }
Ejemplo n.º 3
0
 @Test
 public void testLazyConverter() throws Exception {
   final ApplicationHandler application =
       new ApplicationHandler(new ResourceConfig(MyLazyParamProvider.class, Resource.class));
   final ContainerResponse response =
       application.apply(RequestContextBuilder.from("/resource", "GET").build()).get();
   assertEquals(400, response.getStatus());
 }
Ejemplo n.º 4
0
 @Test
 public void testClientAndServerProvider2() throws ExecutionException, InterruptedException {
   final ResourceConfig resourceConfig =
       new ResourceConfig(Resource.class, MyServerAndClientContrainedToClientFilter.class);
   ApplicationHandler handler = new ApplicationHandler(resourceConfig);
   final ContainerResponse response =
       handler.apply(RequestContextBuilder.from("/resource", "GET").build()).get();
   Assert.assertEquals(200, response.getStatus());
 }
  @Test
  public void testBadPrimitiveWrapperValue() throws ExecutionException, InterruptedException {
    final ContainerResponse responseContext =
        apply(
            RequestContextBuilder.from("/wrappers", "GET")
                .accept("application/int")
                .header("int", "abcdef")
                .build());

    assertEquals(400, responseContext.getStatus());
  }
 @Test
 public void testInvalidSubResource() throws ExecutionException, InterruptedException {
   ApplicationHandler handler = new ApplicationHandler(new ResourceConfig(WrongResource.class));
   _test(handler, "/wrong", "ok");
   try {
     final ContainerResponse response =
         handler.apply(RequestContextBuilder.from("/wrong/locator", "GET").build()).get();
     assertEquals(500, response.getStatus());
     fail("Should throw exception caused by validation errors of Sub Resource.");
   } catch (Throwable e) {
     // ok - Should throw exception caused by validation errors of Sub Resource.
   }
 }
Ejemplo n.º 7
0
 @Test
 public void testResourceAndProviderConstrainedToServer()
     throws ExecutionException, InterruptedException {
   final ResourceConfig resourceConfig =
       new ResourceConfig(ResourceAndProviderConstrainedToServer.class);
   ApplicationHandler handler = new ApplicationHandler(resourceConfig);
   final ContainerResponse response =
       handler
           .apply(RequestContextBuilder.from("/resource-and-provider-server", "GET").build())
           .get();
   Assert.assertEquals(200, response.getStatus());
   Assert.assertEquals(
       "called", response.getHeaderString("ResourceAndProviderConstrainedToServer"));
 }
Ejemplo n.º 8
0
    @Test
    public void testEmptyProduces() throws Exception {
      final ResourceConfig rc = new ResourceConfig(EmptyProducesTestResource.class);
      rc.property(ServerProperties.WADL_FEATURE_DISABLE, false);

      final ApplicationHandler applicationHandler = new ApplicationHandler(rc);

      final ContainerResponse containerResponse =
          applicationHandler
              .apply(
                  new ContainerRequest(
                      URI.create("/"),
                      URI.create("/application.wadl"),
                      "GET",
                      null,
                      new MapPropertiesDelegate()))
              .get();

      assertEquals(200, containerResponse.getStatus());

      ((ByteArrayInputStream) containerResponse.getEntity()).reset();

      final DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
      bf.setNamespaceAware(true);
      bf.setValidating(false);
      if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
        bf.setXIncludeAware(false);
      }
      final DocumentBuilder b = bf.newDocumentBuilder();
      final Document d = b.parse((InputStream) containerResponse.getEntity());
      printSource(new DOMSource(d));
      final XPath xp = XPathFactory.newInstance().newXPath();
      xp.setNamespaceContext(
          new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));

      final NodeList responseElements =
          (NodeList)
              xp.evaluate(
                  "/wadl:application/wadl:resources[@path!='application.wadl']//wadl:method/wadl:response",
                  d,
                  XPathConstants.NODESET);

      for (int i = 0; i < responseElements.getLength(); i++) {
        final Node item = responseElements.item(i);

        assertEquals(0, item.getChildNodes().getLength());
        assertEquals(0, item.getAttributes().getLength());
      }
    }
Ejemplo n.º 9
0
 @Test
 public void testSpecificApplication() throws ExecutionException, InterruptedException {
   Application app =
       new Application() {
         @Override
         public Set<Class<?>> getClasses() {
           final HashSet<Class<?>> classes = Sets.newHashSet();
           classes.add(Resource.class);
           classes.add(MyClientFilter.class);
           classes.add(MyServerWrongFilter.class);
           return classes;
         }
       };
   ApplicationHandler handler = new ApplicationHandler(app);
   final ContainerResponse response =
       handler.apply(RequestContextBuilder.from("/resource", "GET").build()).get();
   Assert.assertEquals(200, response.getStatus());
 }
Ejemplo n.º 10
0
    @Test
    public void testEnableWadl() throws ExecutionException, InterruptedException {
      final ResourceConfig rc = new ResourceConfig(WidgetsResource.class, ExtraResource.class);
      rc.property(ServerProperties.WADL_FEATURE_DISABLE, false);

      final ApplicationHandler applicationHandler = new ApplicationHandler(rc);

      final ContainerResponse containerResponse =
          applicationHandler
              .apply(
                  new ContainerRequest(
                      URI.create("/"),
                      URI.create("/application.wadl"),
                      "GET",
                      null,
                      new MapPropertiesDelegate()))
              .get();

      assertEquals(200, containerResponse.getStatus());
    }
 public String test(HttpMethodOverrideFilter.Source... sources) {
   ResourceConfig rc = new ResourceConfig(Resource.class);
   HttpMethodOverrideFilter.enableFor(rc, sources);
   ApplicationHandler handler = new ApplicationHandler(rc);
   try {
     ContainerResponse response =
         handler
             .apply(
                 RequestContextBuilder.from("", "/?_method=DELETE", "POST")
                     .header("X-HTTP-Method-Override", "PUT")
                     .build())
             .get();
     if (Response.Status.OK.equals(response.getStatusInfo())) {
       return (String) response.getEntity();
     } else {
       return "" + response.getStatus();
     }
   } catch (Exception e) {
     e.printStackTrace();
     return e.getMessage();
   }
 }