Esempio n. 1
0
  /**
   * Test the http protocol handler with the given authentication schemes in the WWW-Authenticate
   * header.
   */
  static void test(String... schemes) throws IOException {

    // the authentication scheme that the client is expected to choose
    String expected = null;
    for (String s : schemes) {
      if (expected == null) {
        expected = s;
      } else if (s.equals("Digest")) {
        expected = s;
      }
    }

    // server reply
    String reply = authReplyFor(schemes);

    System.out.println("====================================");
    System.out.println("Expect client to choose: " + expected);
    System.out.println(reply);

    try (ServerSocket ss = new ServerSocket(0)) {
      Client.start(ss.getLocalPort());

      // client ---- GET ---> server
      // client <--- 401 ---- server
      try (Socket s = ss.accept()) {
        new MessageHeader().parseHeader(s.getInputStream());
        s.getOutputStream().write(reply.getBytes("US-ASCII"));
      }

      // client ---- GET ---> server
      // client <--- 200 ---- server
      String auth;
      try (Socket s = ss.accept()) {
        MessageHeader mh = new MessageHeader();
        mh.parseHeader(s.getInputStream());
        s.getOutputStream().write(OKAY.getBytes("US-ASCII"));
        auth = mh.findValue("Authorization");
      }

      // check Authorization header
      if (auth == null) throw new RuntimeException("Authorization header not found");
      System.out.println("Server received Authorization header: " + auth);
      String[] values = auth.split(" ");
      if (!values[0].equals(expected)) throw new RuntimeException("Unexpected value");
    }
  }
Esempio n. 2
0
 public MessageHeader(InputStream is) throws java.io.IOException {
   parseHeader(is);
 }