Example #1
0
  /** @see HttpHeaderTestService#getLanguage(javax.ws.rs.core.HttpHeaders) */
  public void testLanguage() throws IOException {
    final List<Preference<Language>> acceptedLanguages = new ArrayList<Preference<Language>>();
    acceptedLanguages.add(new Preference<Language>(Language.ENGLISH));
    final ClientInfo clientInfo = new ClientInfo();
    clientInfo.setAcceptedLanguages(acceptedLanguages);

    final Request request =
        new Request(Method.POST, createReference(HttpHeaderTestService.class, "language"));
    request.setClientInfo(clientInfo);
    request.setEntity(new StringRepresentation("entity", Language.ENGLISH));
    final Response response = accessServer(request);

    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("en", response.getEntity().getText());
  }
Example #2
0
  public Response sendRequest(Method method, String url, Representation representation) {
    this.logger.debug("Method: " + method.getName() + " url: " + url);

    Request request = new Request();
    request.setResourceRef(url);
    request.setMethod(method);

    if (!Method.GET.equals(method) && !Method.DELETE.equals(method)) {
      request.setEntity(representation);
    }

    request.setChallengeResponse(this.challenge);

    return this.restClient.handle(request);
  }
  public void writeRawData(String path, byte[] data) throws IOException {
    while (path.startsWith("/")) {
      path = path.substring(1);
    }

    Request rr = createRequest(Method.PUT, path);

    ByteArrayRepresentation entity = new ByteArrayRepresentation(MediaType.APPLICATION_XML, data);

    rr.setEntity(entity);

    Response response = client.handle(rr);

    if (!response.getStatus().isSuccess()) {
      throw new IOException("The response was not successful: " + response.getStatus());
    }
  }
  @Test
  public void testJSON() throws IOException {
    JSONObject obj = new JSONObject();
    obj.put("title", "TITLE");
    JSONArray stuff = new JSONArray();
    for (int i = 0; i < 10; i++) {
      stuff.add(i);
    }
    obj.put("stuff", stuff);
    stuff = new JSONArray();
    for (int i = 0; i < 10; i++) {
      JSONObject s = new JSONObject();
      s.put("name", i);
      stuff.add(s);
    }
    obj.put("moreStuff", stuff);
    obj.put("missing", null);

    FreemarkerReaderTest.createTemplate(
        "farby.ftl",
        new StringReader(
            "${title}"
                + " <#list stuff as x>${x}</#list> "
                + " <#list moreStuff as s>${s.name}</#list>"
                + " ${missing!\"MISSING\"}"));

    FreemarkerReaderSource src = new FreemarkerReaderSource();
    Request req = new Request();
    Reference reference = new Reference();
    reference.setPath("/unused");
    reference.setQuery("");
    req.setResourceRef(reference);
    req.getAttributes().put("template", "farby");
    String json = obj.toString();
    req.setEntity(json, MediaType.APPLICATION_JSON);
    String text = new Scanner(src.printngReader(req).reader()).useDelimiter("\\Z").next();
    assertEquals("TITLE 0123456789  0123456789 MISSING", text);
  }