@Before
 public void setUp() throws Exception {
   when(request.getRemoteAddr()).thenReturn("127.0.0.1");
   when(messageHandler.handle(any(UpdateRequest.class), any(UpdateContext.class)))
       .thenReturn(new UpdateResponse(UpdateStatus.SUCCESS, "OK"));
   when(sourceContext.getCurrentSource()).thenReturn(Source.master("TEST"));
 }
示例#2
0
  private Response doSyncUpdate(final Request request) {
    loggerContext.init(getRequestId(request.getRemoteAddress()));

    try {
      if (!sourceMatchesContext(request.getSource())) {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("Invalid source specified: " + request.getSource())
            .build();
      }

      boolean notificationsEnabled = true;
      if (request.isParam(Command.REDIRECT)) {
        if (!ipRanges.isTrusted(IpInterval.parse(request.getRemoteAddress()))) {
          return Response.status(Response.Status.FORBIDDEN)
              .entity("Not allowed to disable notifications: " + request.getRemoteAddress())
              .build();
        }

        notificationsEnabled = false;
      }

      if (!request.hasParam(Command.DATA) && request.isParam(Command.NEW)) {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("DATA parameter is missing")
            .build();
      }

      if (!request.hasParam(Command.DATA) && !request.isParam(Command.HELP)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Invalid request").build();
      }

      loggerContext.log("msg-in.txt", new SyncUpdateLogCallback(request.toString()));

      final UpdateContext updateContext = new UpdateContext(loggerContext);

      final String content = request.hasParam("DATA") ? request.getParam("DATA") : "";

      final UpdateRequest updateRequest =
          new UpdateRequest(
              new SyncUpdate(dateTimeProvider, request.getRemoteAddress()),
              getKeyword(request),
              content,
              updatesParser.parse(
                  updateContext, Lists.newArrayList(new ContentWithCredentials(content))),
              notificationsEnabled);

      final UpdateResponse updateResponse =
          updateRequestHandler.handle(updateRequest, updateContext);
      loggerContext.log("msg-out.txt", new SyncUpdateLogCallback(updateResponse.getResponse()));
      return getResponse(updateResponse);

    } finally {
      loggerContext.remove();
    }
  }
  @Test
  public void handle_unauthorised() throws Exception {
    final String data = "person";
    final String help = null;
    final String nnew = null;
    final String diff = null;
    final String redirect = null;
    final String source = "test";
    final String contentType = "UTF-8";

    when(messageHandler.handle(any(UpdateRequest.class), any(UpdateContext.class)))
        .thenReturn(new UpdateResponse(UpdateStatus.FAILED_AUTHENTICATION, "FAILED"));
    final Response response =
        subject.doGet(request, source, data, help, nnew, diff, redirect, contentType);

    assertThat(response.getStatus(), is(HttpURLConnection.HTTP_UNAUTHORIZED));
    assertThat(response.getEntity().toString(), is("FAILED"));
  }