Added JsonPage and finished implementation of digest auth page

This commit is contained in:
Ziver Koc 2016-11-04 16:52:10 +01:00
parent 2c7a9a6eff
commit 26c09452f3
10 changed files with 244 additions and 52 deletions

View file

@ -0,0 +1,30 @@
package zutil.net.http;
import zutil.io.StringOutputStream;
import zutil.net.http.page.HttpDigestAuthPageTest;
import java.io.IOException;
import java.util.HashMap;
public class HttpTestUtil {
public static HashMap<String,Object> session = new HashMap();
/**
* Make a simple http request on the given page object
*/
public static HttpHeader makeRequest(HttpPage page) throws IOException {
return makeRequest(page, new HttpHeader());
}
/**
* Make a simple http request on the given page object
*/
public static HttpHeader makeRequest(HttpPage page, HttpHeader headers) throws IOException {
StringOutputStream buff = new StringOutputStream();
HttpPrintStream out = new HttpPrintStream(buff);
page.respond(
out, headers, session, new HashMap(), new HashMap());
out.flush();
HttpHeaderParser parser = new HttpHeaderParser(buff.toString());
return parser.read();
}
}

View file

@ -1,11 +1,16 @@
package zutil.net.http.page;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import zutil.Hasher;
import zutil.io.StringOutputStream;
import zutil.io.IOUtil;
import zutil.net.http.HttpHeader;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPrintStream;
import zutil.net.http.HttpTestUtil;
import java.io.IOException;
import java.util.HashMap;
@ -18,10 +23,22 @@ import static org.junit.Assert.*;
*/
public class HttpDigestAuthPageTest {
private static final String PAGE_CONTENT = "Hello World!";
private static final String PAGE_USERNAME = "username";
private static final String PAGE_PASSWORD = "password";
private HttpDigestTestPage page;
@Before
public void init(){
page = new HttpDigestTestPage();
page.addUser(PAGE_USERNAME, PAGE_PASSWORD.toCharArray());
}
@Test
public void cleanRequest() throws IOException {
HttpHeader rspHeader = makeRequest(new HttpHeader());
HttpHeader rspHeader = HttpTestUtil.makeRequest(page);
assertEquals(401, rspHeader.getHTTPCode());
assertTrue(rspHeader.getHeader("WWW-Authenticate") != null);
@ -29,47 +46,60 @@ public class HttpDigestAuthPageTest {
Map<String,String> authHeader = parseAuthHeader(rspHeader);
assertTrue(authHeader.containsKey("realm"));
assertTrue(authHeader.containsKey("nonce"));
assertThat(IOUtil.readContentAsString(rspHeader.getInputStream()),
not(containsString(PAGE_CONTENT)));
}
@Test
public void authenticate() throws IOException {
HttpHeader rspHeader = authenticate(PAGE_USERNAME, PAGE_PASSWORD);
assertEquals(200, rspHeader.getHTTPCode());
assertThat(IOUtil.readContentAsString(rspHeader.getInputStream()),
containsString(PAGE_CONTENT));
}
@Test
public void wrongUsername() throws IOException {
HttpHeader rspHeader = authenticate(PAGE_USERNAME+"wrong", PAGE_PASSWORD);
assertEquals(403, rspHeader.getHTTPCode());
assertThat(IOUtil.readContentAsString(rspHeader.getInputStream()),
not(containsString(PAGE_CONTENT)));
}
@Test
public void wrongPassword() throws IOException {
HttpHeader rspHeader = authenticate(PAGE_USERNAME, PAGE_PASSWORD+"wrong");
assertEquals(403, rspHeader.getHTTPCode());
assertThat(IOUtil.readContentAsString(rspHeader.getInputStream()),
not(containsString(PAGE_CONTENT)));
}
public HttpHeader authenticate(String username, String password) throws IOException {
HttpHeader reqHeader = new HttpHeader();
HttpHeader rspHeader = makeRequest(reqHeader);
HttpHeader rspHeader = HttpTestUtil.makeRequest(page, reqHeader);
Map<String,String> authHeader = parseAuthHeader(rspHeader);
reqHeader = new HttpHeader();
String realm = authHeader.get("realm");
String nonce = authHeader.get("nonce");
String uri = "/login";
String ha1 = Hasher.MD5("username:password");
String ha2 = Hasher.MD5("MD5:/" +uri);
String ha1 = Hasher.MD5(username+":"+realm+":"+password);
String ha2 = Hasher.MD5("MD5:" +uri);
String response = Hasher.MD5(ha1 +":"+ nonce +":"+ ha2);
reqHeader.setHeader("Authorization", "Digest username=\"username\", " +
reqHeader.setRequestURL(uri);
reqHeader.setHeader("Authorization", "Digest " +
"username=\""+username+"\", " +
"realm=\""+realm+"\", " +
"nonce=\""+nonce+"\", " +
"uri=\""+uri+"\", " +
"response=\""+response+"\"");
rspHeader = makeRequest(reqHeader);
assertEquals(200, rspHeader.getHTTPCode());
return HttpTestUtil.makeRequest(page, reqHeader);
}
public static HttpHeader makeRequest(HttpHeader headers) throws IOException {
StringOutputStream buff = new StringOutputStream();
HttpPrintStream out = new HttpPrintStream(buff);
new HttpDigestTestPage().respond(
out, headers, new HashMap(), new HashMap(), new HashMap());
out.flush();
HttpHeaderParser parser = new HttpHeaderParser(buff.toString());
return parser.read();
}
public static String parseAuthType(HttpHeader headers){
String tmp = headers.getHeader("WWW-Authenticate");
return tmp.substring(0, tmp.indexOf(' '));

View file

@ -0,0 +1,34 @@
package zutil.net.http.page;
import org.junit.Test;
import zutil.io.IOUtil;
import zutil.net.http.HttpHeader;
import zutil.net.http.HttpTestUtil;
import zutil.parser.DataNode;
import java.io.IOException;
import java.util.Map;
import static org.junit.Assert.*;
/**
* @author Ziver on 2016-11-04.
*/
public class HttpJsonPageTest {
private HttpJsonPage page = new HttpJsonPage() {
@Override
protected DataNode jsonRespond(HttpHeader headers, Map<String, Object> session, Map<String, String> cookie, Map<String, String> request) {
return new DataNode(DataNode.DataType.Map);
}
};
@Test
public void simpleResponse() throws IOException {
HttpHeader header = HttpTestUtil.makeRequest(page);
assertEquals("application/json", header.getHeader("Content-Type"));
assertEquals("{}", IOUtil.readContentAsString(header.getInputStream()));
}
}