Пример #1
0
  @Test
  public void testDownload() throws IOException {
    response =
        recordEndpoint.downloadFile(AUTH_TOKEN, AUTH_TOKEN, "image", "image:123456789", "master");
    String contentType = response.getHeaderString("Content-Type");
    /*
     * when we detect and store type of file in spider check it like this
     * assertEquals(contentType, "application/octet-stream");
     */
    assertEquals(contentType, "application/octet-stream");
    // assertEquals(contentType, null);
    InputStream stream = (InputStream) response.getEntity();
    assertNotNull(stream);

    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = stream.read(buffer)) != -1) {
      result.write(buffer, 0, length);
    }
    String stringFromStream = result.toString("UTF-8");

    assertEquals(stringFromStream, "a string out");
    assertResponseStatusIs(Response.Status.OK);

    String contentLenght = response.getHeaderString("Content-Length");
    assertEquals(contentLenght, "123");

    String contentDisposition = response.getHeaderString("Content-Disposition");
    assertEquals(contentDisposition, "attachment; filename=adele.png");
  }
  @Test
  public void ContentNegotiationExtensions() {
    Response xmlResponse = target("books").path(book1_id + ".xml").request().get();
    assertEquals(MediaType.APPLICATION_XML, xmlResponse.getHeaderString("Content-Type"));

    Response jsonResponse = target("books").path(book1_id + ".json").request().get();
    assertEquals(MediaType.APPLICATION_JSON, jsonResponse.getHeaderString("Content-Type"));
  }
  @Test
  public void PoweredByHeader() {
    Response response = target("books").path(book1_id).request().get();
    assertEquals("Pluralsight", response.getHeaderString("X-Powered-By"));

    Response response2 = target("books").request().get();
    assertNull(response2.getHeaderString("X-Powered-By"));
  }
  public void handleFileUpload(FileUploadEvent fue) {
    String docId = UUID.randomUUID().toString();
    getDocIds().add(docId);
    try {
      IclubDocumentModel model = new IclubDocumentModel();
      model.setIclubPerson(getSessionUserId());
      model.setDCrtdDt(new Date(System.currentTimeMillis()));
      model.setDId(docId);
      model.setDName(fue.getFile().getFileName());
      model.setDContent(fue.getFile().getContentType());
      model.setDSize(fue.getFile().getSize());

      WebClient client = IclubWebHelper.createCustomClient(D_BASE_URL + "add");
      ResponseModel response =
          client.accept(MediaType.APPLICATION_JSON).post(model, ResponseModel.class);
      client.close();

      if (response.getStatusCode() == 0) {
        ContentDisposition cd =
            new ContentDisposition(
                "attachment;filename="
                    + fue.getFile().getFileName()
                    + ";filetype="
                    + fue.getFile().getContentType());
        List<Attachment> attachments = new ArrayList<Attachment>();
        Attachment attachment = new Attachment(docId, fue.getFile().getInputstream(), cd);
        attachments.add(attachment);

        WebClient uploadClient = WebClient.create(D_BASE_URL + "upload");
        Response res =
            uploadClient.type("multipart/form-data").post(new MultipartBody(attachments));
        uploadClient.close();

        if (res.getStatus() == 200) {
          IclubWebHelper.addMessage(
              getLabelBundle().getString("doucmentuploadedsuccessfully"),
              FacesMessage.SEVERITY_INFO);
        } else {
          IclubWebHelper.addMessage(
              getLabelBundle().getString("doucmentuploadingfailed")
                  + " :: "
                  + (res.getHeaderString("status") != null
                      ? res.getHeaderString("status")
                      : res.getStatusInfo()),
              FacesMessage.SEVERITY_ERROR);
        }
      }
    } catch (Exception e) {
      LOGGER.error(e, e);
      IclubWebHelper.addMessage(
          getLabelBundle().getString("doucmentuploadingerror") + " :: " + e.getMessage(),
          FacesMessage.SEVERITY_ERROR);
    }
  }
  /* post(null) /wishlists/wishlistId/media */
  @Test
  public void testPostByWishlistIdMedia() throws FileNotFoundException {
    Response response = createWishlistMedia();

    // Verify
    assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
    String location =
        response
            .getHeaderString("location")
            .substring(response.getHeaderString("location").lastIndexOf("/") + 1);
    instanceListMedia.add(location);
    assertNotNull(location);
  }
Пример #6
0
  public static String getAuthorizationCode(WebClient client, String scope) {
    // Make initial authorization request
    client.type("application/json").accept("application/json");
    client.query("client_id", "consumer-id");
    client.query("redirect_uri", "http://www.blah.apache.org");
    client.query("response_type", "code");
    if (scope != null) {
      client.query("scope", scope);
    }
    client.path("authorize/");
    Response response = client.get();

    OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);

    // Now call "decision" to get the authorization code grant
    client.path("decision");
    client.type("application/x-www-form-urlencoded");

    Form form = new Form();
    form.param("session_authenticity_token", authzData.getAuthenticityToken());
    form.param("client_id", authzData.getClientId());
    form.param("redirect_uri", authzData.getRedirectUri());
    if (authzData.getProposedScope() != null) {
      form.param("scope", authzData.getProposedScope());
    }
    form.param("oauthDecision", "allow");

    response = client.post(form);
    String location = response.getHeaderString("Location");
    return getSubstring(location, "code");
  }
Пример #7
0
 @Test
 public void testHeadByte() throws Exception {
   Response response = target().path("byte").request().head();
   assertEquals(200, response.getStatus());
   assertEquals(3, Integer.parseInt(response.getHeaderString(HttpHeaders.CONTENT_LENGTH)));
   assertFalse(response.hasEntity());
 }
  @Test
  public void testOverride() throws Exception {
    final Response response = target().path("/").request("text/plain").post(Entity.text("content"));

    assertEquals("content", response.readEntity(String.class));
    assertEquals(HEADER_VALUE_SERVER, response.getHeaderString(HEADER_NAME));
  }
  /* delete() /wishlists/wishlistId/media/mediaId */
  @Test
  public void testDeleteByWishlistIdMediaByMediaId() throws FileNotFoundException {
    Response response = createWishlistMedia();
    String location =
        response
            .getHeaderString("location")
            .substring(response.getHeaderString("location").lastIndexOf("/") + 1);

    final Response responseDelete = deleteWishlistMedia(wishlist.getId(), location);

    Assert.assertNotNull("Response must not be null", responseDelete);
    Assert.assertEquals(
        "Response does not have expected response code",
        Status.NO_CONTENT.getStatusCode(),
        responseDelete.getStatus());
  }
 /** Test with relative location with leading slash */
 @Test
 public void testLocationWithSlash() {
   final Response response =
       target().path("test/locationSlash").request(MediaType.TEXT_PLAIN).get(Response.class);
   String location = response.getHeaderString(HttpHeaders.LOCATION);
   LOGGER.info("Location resolved from response > " + location);
   assertEquals("/location", location);
 }
 /** Test with relative location with leading slash */
 @Test
 public void testNullLocation() {
   final Response response =
       target().path("test/locationNull").request(MediaType.TEXT_PLAIN).get(Response.class);
   String location = response.getHeaderString(HttpHeaders.LOCATION);
   LOGGER.info("Location resolved from response > " + location);
   assertNull(location);
 }
Пример #12
0
 @Test
 public void testHeadContentLengthCustomWriter() throws Exception {
   Response response = target().request().head();
   assertEquals(200, response.getStatus());
   assertEquals(
       STR.length(), Integer.parseInt(response.getHeaderString(HttpHeaders.CONTENT_LENGTH)));
   assertFalse(response.hasEntity());
 }
Пример #13
0
 @Test
 public void testFooBarOptions() {
   Response response = target().path(ROOT_PATH).request().header("Accept", "foo/bar").options();
   assertEquals(200, response.getStatus());
   final String allowHeader = response.getHeaderString("Allow");
   _checkAllowContent(allowHeader);
   assertEquals("foo/bar", response.getMediaType().toString());
   assertEquals(0, response.getLength());
 }
Пример #14
0
 @Test
 public void testTextPlainOptions() {
   Response response =
       target().path(ROOT_PATH).request().header("Accept", MediaType.TEXT_PLAIN).options();
   assertEquals(200, response.getStatus());
   final String allowHeader = response.getHeaderString("Allow");
   _checkAllowContent(allowHeader);
   assertEquals(MediaType.TEXT_PLAIN_TYPE, response.getMediaType());
   final String responseBody = response.readEntity(String.class);
   _checkAllowContent(responseBody);
 }
  /* get() /wishlists/wishlistId/media */
  @Test
  public void testGetByWishlistIdMedia()
      throws MalformedURLException, NoSuchAlgorithmException, IOException {
    Response response = createWishlistMedia();
    String location =
        response
            .getHeaderString("location")
            .substring(response.getHeaderString("location").lastIndexOf("/") + 1);
    instanceListMedia.add(location);

    final WebTarget target =
        getRootTarget(ROOT_RESOURCE_PATH).path("/" + wishlist.getId() + "/media");
    final Response responseGet =
        target
            .request()
            .header(YaasAwareTrait.Headers.CLIENT, CLIENT)
            .header(YaasAwareTrait.Headers.TENANT, TestConstants.TENANT)
            .get();

    Assert.assertNotNull("Response must not be null", responseGet);
    Assert.assertEquals(
        "Response does not have expected response code",
        Status.OK.getStatusCode(),
        responseGet.getStatus());

    WishlistMedia[] wishlistMedias = responseGet.readEntity(WishlistMedia[].class);
    String actMD5 = null;
    for (WishlistMedia wishlistMedia : wishlistMedias) {
      if (location.equals(wishlistMedia.getId())) {
        actMD5 = computeMD5ChecksumForURL(new URL(wishlistMedia.getUri().toString()));
      }
    }

    String expMD5 = computeMD5ChecksumForFile(TEST_FILE_FOR_UPLOAD);
    Assert.assertEquals("File on media repository is different from file sent", expMD5, actMD5);
  }
Пример #16
0
  @Test
  public void testaQueSuportaNovosCarrinhos() {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8080");
    Carrinho carrinho = new Carrinho();
    carrinho.adiciona(new Produto(314L, "Tablet", 999, 1));
    carrinho.setRua("Rua Vergueiro");
    carrinho.setCidade("Sao Paulo");
    String xml = carrinho.toXML();

    Entity<String> entity = Entity.entity(xml, MediaType.APPLICATION_XML);

    Response response = target.path("/carrinhos").request().post(entity);
    String location = response.getHeaderString("Location");
    Assert.assertTrue(location.contains("microfone"));
    Assert.assertEquals(201, response.getStatus());
  }
Пример #17
0
  @Test
  public void spnegoNotAvailableTest() throws Exception {
    initHttpClient(false);

    SpnegoAuthenticator.bypassChallengeJavascript = true;
    driver.navigate().to(KERBEROS_APP_URL);
    String kcLoginPageLocation = driver.getCurrentUrl();

    Response response = client.target(kcLoginPageLocation).request().get();
    Assert.assertEquals(401, response.getStatus());
    Assert.assertEquals(
        KerberosConstants.NEGOTIATE, response.getHeaderString(HttpHeaders.WWW_AUTHENTICATE));
    String responseText = response.readEntity(String.class);
    responseText.contains("Log in to test");
    response.close();
    SpnegoAuthenticator.bypassChallengeJavascript = false;
  }