HttpPrintStream is now using the HttpHeader object instead of having internal variables aalso added some additional test classes
This commit is contained in:
parent
8a72e2bc01
commit
ad3cd8a8e8
13 changed files with 604 additions and 250 deletions
158
test/zutil/net/http/HttpHeaderTest.java
Normal file
158
test/zutil/net/http/HttpHeaderTest.java
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.net.http;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HttpHeaderTest {
|
||||
|
||||
@Test
|
||||
public void setIsRequest() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertTrue(header.isRequest());
|
||||
assertFalse(header.isResponse());
|
||||
|
||||
header.setIsRequest(false);
|
||||
assertFalse(header.isRequest());
|
||||
assertTrue(header.isResponse());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setProtocol() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertEquals("HTTP", header.getProtocol());
|
||||
|
||||
header.setProtocol("RTSP");
|
||||
assertEquals("RTSP", header.getProtocol());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setProtocolVersion() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertEquals(1.0f, header.getProtocolVersion(), 0);
|
||||
|
||||
header.setProtocolVersion(1.1f);
|
||||
assertEquals(1.1f, header.getProtocolVersion(), 0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setRequestType() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertEquals("GET", header.getRequestType());
|
||||
|
||||
header.setRequestType("POST");
|
||||
assertEquals("POST", header.getRequestType());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setResponseStatusCode() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertEquals(200, header.getResponseStatusCode());
|
||||
|
||||
header.setResponseStatusCode(400);
|
||||
assertEquals(400, header.getResponseStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setRequestURL() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertEquals("/", header.getRequestURL());
|
||||
|
||||
header.setRequestURL("/page/test");
|
||||
assertEquals("/page/test", header.getRequestURL());
|
||||
|
||||
header.setRequestURL(" /page/1test ");
|
||||
assertEquals("/page/1test", header.getRequestURL());
|
||||
|
||||
header.setRequestURL("/page//2test ");
|
||||
assertEquals("/page/2test", header.getRequestURL());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestPage() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
header.setRequestURL("/page/test?param=a&dd=tt");
|
||||
assertEquals("page/test", header.getRequestPage());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setURLAttribute() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertNull(header.getURLAttribute("param1"));
|
||||
|
||||
header.setURLAttribute("param1", "value1");
|
||||
assertEquals("value1", header.getURLAttribute("param1"));
|
||||
assertNull(header.getURLAttribute("Param1"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setHeader() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertNull(header.getHeader("param1"));
|
||||
|
||||
header.setHeader("param1", "value1");
|
||||
header.setHeader("param2", "value2");
|
||||
assertEquals("value1", header.getHeader("param1"));
|
||||
assertEquals("value1", header.getHeader("PARAM1"));
|
||||
assertEquals("value2", header.getHeader("param2"));
|
||||
assertEquals("value2", header.getHeader("Param2"));
|
||||
|
||||
header.setHeader("param2", "value3");
|
||||
assertEquals("value3", header.getHeader("param2"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setCookie() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertNull(header.getCookie("param1"));
|
||||
|
||||
header.setCookie("param1", "value1");
|
||||
header.setCookie("param2", "value2");
|
||||
assertEquals("value1", header.getCookie("param1"));
|
||||
assertEquals("value2", header.getCookie("param2"));
|
||||
assertNull("value2", header.getCookie("Param2"));
|
||||
|
||||
header.setCookie("param2", "value3");
|
||||
assertEquals("value3", header.getCookie("param2"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getResponseStatusString() {
|
||||
HttpHeader header = new HttpHeader();
|
||||
assertEquals("OK", header.getResponseStatusString());
|
||||
|
||||
header.setResponseStatusCode(400);
|
||||
assertEquals("Bad Request", header.getResponseStatusString());
|
||||
}
|
||||
}
|
||||
198
test/zutil/net/http/HttpPrintStreamTest.java
Normal file
198
test/zutil/net/http/HttpPrintStreamTest.java
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.net.http;
|
||||
|
||||
import org.junit.Test;
|
||||
import zutil.io.StringOutputStream;
|
||||
import zutil.net.http.HttpPrintStream.HttpMessageType;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HttpPrintStreamTest {
|
||||
|
||||
@Test
|
||||
public void requestDefaultOutput() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.REQUEST);
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"GET / HTTP/1.0" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestSetType() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.REQUEST);
|
||||
|
||||
httpOut.setRequestType("POST");
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"POST / HTTP/1.0" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestSetUrl() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.REQUEST);
|
||||
|
||||
httpOut.setRequestURL("/test/path/to/page?param=aa&tt=aa");
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"GET /test/path/to/page?param=aa&tt=aa HTTP/1.0" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestSetCookie() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.REQUEST);
|
||||
|
||||
httpOut.setCookie("Test", "value");
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"GET / HTTP/1.0" + System.lineSeparator() +
|
||||
"Cookie: Test=value;" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestSetCookieMultiple() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.REQUEST);
|
||||
|
||||
httpOut.setCookie("Test1", "value1");
|
||||
httpOut.setCookie("Test2", "value2");
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"GET / HTTP/1.0" + System.lineSeparator() +
|
||||
"Cookie: Test1=value1; Test2=value2;" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void responseDefaultOutput() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.RESPONSE);
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"HTTP/1.0 200 OK" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseStatusCode() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.RESPONSE);
|
||||
|
||||
httpOut.setResponseStatusCode(400);
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"HTTP/1.0 400 Bad Request" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseSetCookie() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.RESPONSE);
|
||||
|
||||
httpOut.setCookie("Test", "value");
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"HTTP/1.0 200 OK" + System.lineSeparator() +
|
||||
"Set-Cookie: Test=value;" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseSetCookieMultiple() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out, HttpMessageType.RESPONSE);
|
||||
|
||||
httpOut.setCookie("Test1", "value1");
|
||||
httpOut.setCookie("Test2", "value2");
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"HTTP/1.0 200 OK" + System.lineSeparator() +
|
||||
"Set-Cookie: Test1=value1;" + System.lineSeparator() +
|
||||
"Set-Cookie: Test2=value2;" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setHeader() {
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
HttpPrintStream httpOut = new HttpPrintStream(out);
|
||||
|
||||
httpOut.setHeader("Test1", "value1");
|
||||
httpOut.setHeader("Test2", "value2");
|
||||
|
||||
httpOut.flush();
|
||||
assertEquals(
|
||||
"HTTP/1.0 200 OK" + System.lineSeparator() +
|
||||
"Test1: value1" + System.lineSeparator() +
|
||||
"Test2: value2" + System.lineSeparator() +
|
||||
System.lineSeparator(),
|
||||
out.toString()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// TODO @Test
|
||||
public void enableBuffering() {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -20,8 +20,9 @@ public class HttpTestUtil {
|
|||
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());
|
||||
|
||||
page.respond(out, headers, session, new HashMap(), new HashMap());
|
||||
|
||||
out.flush();
|
||||
HttpHeaderParser parser = new HttpHeaderParser(buff.toString());
|
||||
return parser.read();
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ public class HttpDigestAuthPageTest {
|
|||
public void cleanRequest() throws IOException {
|
||||
HttpHeader rspHeader = HttpTestUtil.makeRequest(page);
|
||||
|
||||
assertEquals(401, rspHeader.getStatusCode());
|
||||
assertEquals(401, rspHeader.getResponseStatusCode());
|
||||
assertNotNull(rspHeader.getHeader("WWW-Authenticate"));
|
||||
assertEquals("Digest", parseAuthType(rspHeader));
|
||||
Map<String,String> authHeader = parseAuthHeader(rspHeader);
|
||||
|
|
@ -49,21 +49,21 @@ public class HttpDigestAuthPageTest {
|
|||
@Test
|
||||
public void authenticate() throws IOException {
|
||||
HttpHeader rspHeader = authenticate(PAGE_USERNAME, PAGE_PASSWORD);
|
||||
assertEquals(200, rspHeader.getStatusCode());
|
||||
assertEquals(200, rspHeader.getResponseStatusCode());
|
||||
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.getStatusCode());
|
||||
assertEquals(403, rspHeader.getResponseStatusCode());
|
||||
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.getStatusCode());
|
||||
assertEquals(403, rspHeader.getResponseStatusCode());
|
||||
assertThat(IOUtil.readContentAsString(rspHeader.getInputStream()),
|
||||
not(containsString(PAGE_CONTENT)));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue