Esempio n. 1
0
  @Test
  public void testHandlesInvalidAuthentication() throws IOException {
    String message =
        "GET /logs HTTP/1.1\n"
            + "Authorization: Basic YWRtaW46aHVudGVyMh==\r\n\r\n"; // bad_credentials (terminal h
                                                                   // instead of g)

    BufferedReader bufferedReader = new BufferedReader(new StringReader(message));
    HashMap state = new HashMap();
    RequestHandler requestHandler = new RequestHandler(bufferedReader, state);

    HashMap response = requestHandler.respondToRequest();

    Object header = response.get("message");
    Object credentials = response.get("Authorization");

    assertEquals("HTTP/1.1 401\r\n\r\nAuthentication required", new String((byte[]) header));
    assertEquals(
        "Basic YWRtaW46aHVudGVyMh==", credentials); // bad_credentials (terminal h instead of g)

    HashMap returnedState = (HashMap) response.get("state");
    String requests = (String) returnedState.get("requests");

    assert (requests != null);
  }
Esempio n. 2
0
  @Test
  public void testRespondsCorrectlyToQueryString() throws IOException {
    String request = "GET /parameters?variable_1=Operators%20%3C HTTP/1.1\r\n\r\n";
    HashMap state = null;

    BufferedReader bufferedReader = new BufferedReader(new StringReader(request));
    RequestHandler requestHandler = new RequestHandler(bufferedReader, state);

    requestHandler.respondToRequest();

    assertEquals("variable_1 = Operators <", new String(requestHandler.getBody()));
  }
Esempio n. 3
0
  @Test
  public void testPutsBodyIntoHashMap() throws IOException {
    String message =
        "POST /form HTTP/1.1\n" + "Host: localhost:5000\n" + "Content-Length: 5\r\n\r\n" + "hello";

    BufferedReader bufferedReader = new BufferedReader(new StringReader(message));
    HashMap state = new HashMap();
    RequestHandler requestHandler = new RequestHandler(bufferedReader, state);

    HashMap response = requestHandler.respondToRequest();

    HashMap stateObject = (HashMap) response.get("state");
    byte[] nestedBody = (byte[]) stateObject.get("state");

    assertArrayEquals("hello".getBytes(), nestedBody);
  }
Esempio n. 4
0
  @Test
  public void testPutsResponseHeaderIntoHashMap() throws IOException {
    String message = "GET / HTTP/1.1\n" + "Host: localhost:5000\r\n\r\n";

    BufferedReader bufferedReader = new BufferedReader(new StringReader(message));
    HashMap state = new HashMap();
    RequestHandler requestHandler = new RequestHandler(bufferedReader, state);

    HashMap response = requestHandler.respondToRequest();

    Object header = response.get("message");

    assertEquals(
        "HTTP/1.1 200 OK\r\n\r\n<p><a href='ttt'>Play Tic Tac Toe</a></p><a href='file1'>file1</a><p><a href='file2'>file2</a></p><a href='image.gif'>image.gif</a><a href='image.jpeg'>image.jpeg</a><a href='image.png'>image.png</a><a href='text-file.txt'>text-file.txt</a><a href='partial_content.txt'>partial_content.txt</a>",
        (new String((byte[]) header)));
  }
Esempio n. 5
0
  @Test
  public void testHandleRequest() throws IOException {
    String request =
        "GET / HTTP/1.1\n" + "Host: localhost:5000\n" + "Accept-Language: en-US,en;q=0.8\r\n\r\n";
    BufferedReader bufferedReader = new BufferedReader(new StringReader(request));
    HashMap state = new HashMap();
    RequestHandler requestHandler = new RequestHandler(bufferedReader, state);

    HashMap response = requestHandler.respondToRequest();
    byte[] byteResponse = (byte[]) response.get("message");

    assertArrayEquals(
        "HTTP/1.1 200 OK\r\n\r\n<p><a href='ttt'>Play Tic Tac Toe</a></p><a href='file1'>file1</a><p><a href='file2'>file2</a></p><a href='image.gif'>image.gif</a><a href='image.jpeg'>image.jpeg</a><a href='image.png'>image.png</a><a href='text-file.txt'>text-file.txt</a><a href='partial_content.txt'>partial_content.txt</a>"
            .getBytes(),
        byteResponse);
  }
Esempio n. 6
0
  @Test
  public void testHandlesValidAuthentication() throws IOException {
    String message = "GET /logs HTTP/1.1\n" + "Authorization: Basic YWRtaW46aHVudGVyMg==\r\n\r\n";

    BufferedReader bufferedReader = new BufferedReader(new StringReader(message));
    HashMap state = new HashMap();
    RequestHandler requestHandler = new RequestHandler(bufferedReader, state);

    HashMap response = requestHandler.respondToRequest();

    byte[] header = (byte[]) response.get("message");
    byte[] responseFromValidAuthentication =
        "HTTP/1.1 200 OK\r\n\r\nGET /log HTTP/1.1\nPUT /these HTTP/1.1\nHEAD /requests HTTP/1.1"
            .getBytes();

    assertEquals(new String(responseFromValidAuthentication), new String(header));
  }