Merge pull request #71 from pethers/master
Added nullchecks and test for getters using _metadata to avoid throwing
This commit is contained in:
commit
ca0193b28e
|
@ -207,7 +207,7 @@ public class Request implements HttpServletRequest
|
|||
/* ------------------------------------------------------------ */
|
||||
public HttpFields getHttpFields()
|
||||
{
|
||||
return _metadata.getFields();
|
||||
return _metadata==null?null:_metadata.getFields();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -369,7 +369,7 @@ public class Request implements HttpServletRequest
|
|||
/* ------------------------------------------------------------ */
|
||||
private void extractQueryParameters()
|
||||
{
|
||||
if (_metadata.getURI() == null || !_metadata.getURI().hasQuery())
|
||||
if (_metadata == null || _metadata.getURI() == null || !_metadata.getURI().hasQuery())
|
||||
_queryParameters=NO_PARAMS;
|
||||
else
|
||||
{
|
||||
|
@ -656,7 +656,7 @@ public class Request implements HttpServletRequest
|
|||
@Override
|
||||
public String getContentType()
|
||||
{
|
||||
String content_type = _metadata.getFields().get(HttpHeader.CONTENT_TYPE);
|
||||
String content_type = _metadata==null?null:_metadata.getFields().get(HttpHeader.CONTENT_TYPE);
|
||||
if (_characterEncoding==null && content_type!=null)
|
||||
{
|
||||
MimeTypes.Type mime = MimeTypes.CACHE.get(content_type);
|
||||
|
@ -944,7 +944,7 @@ public class Request implements HttpServletRequest
|
|||
if (local!=null)
|
||||
return local.getHostString();
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
String name =InetAddress.getLocalHost().getHostName();
|
||||
|
@ -1142,7 +1142,7 @@ public class Request implements HttpServletRequest
|
|||
@Override
|
||||
public String getQueryString()
|
||||
{
|
||||
return _metadata.getURI().getQuery();
|
||||
return _metadata==null?null:_metadata.getURI().getQuery();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -1370,7 +1370,7 @@ public class Request implements HttpServletRequest
|
|||
@Override
|
||||
public String getServerName()
|
||||
{
|
||||
String name = _metadata.getURI().getHost();
|
||||
String name = _metadata==null?null:_metadata.getURI().getHost();
|
||||
|
||||
// Return already determined host
|
||||
if (name != null)
|
||||
|
@ -1383,7 +1383,7 @@ public class Request implements HttpServletRequest
|
|||
private String findServerName()
|
||||
{
|
||||
// Return host from header field
|
||||
HttpField host = _metadata.getFields().getField(HttpHeader.HOST);
|
||||
HttpField host = _metadata==null?null:_metadata.getFields().getField(HttpHeader.HOST);
|
||||
if (host!=null)
|
||||
{
|
||||
// TODO is this needed now?
|
||||
|
@ -1418,8 +1418,8 @@ public class Request implements HttpServletRequest
|
|||
@Override
|
||||
public int getServerPort()
|
||||
{
|
||||
HttpURI uri = _metadata.getURI();
|
||||
int port = (uri.getHost()==null)?findServerPort():uri.getPort();
|
||||
HttpURI uri = _metadata==null?null:_metadata.getURI();
|
||||
int port = (uri == null || uri.getHost()==null)?findServerPort():uri.getPort();
|
||||
|
||||
// If no port specified, return the default port for the scheme
|
||||
if (port <= 0)
|
||||
|
@ -1437,7 +1437,7 @@ public class Request implements HttpServletRequest
|
|||
private int findServerPort()
|
||||
{
|
||||
// Return host from header field
|
||||
HttpField host = _metadata.getFields().getField(HttpHeader.HOST);
|
||||
HttpField host = _metadata==null?null:_metadata.getFields().getField(HttpHeader.HOST);
|
||||
if (host!=null)
|
||||
{
|
||||
// TODO is this needed now?
|
||||
|
|
|
@ -217,7 +217,7 @@ public class RequestTest
|
|||
|
||||
@Test
|
||||
public void testMultiPart() throws Exception
|
||||
{
|
||||
{
|
||||
final File testTmpDir = File.createTempFile("reqtest", null);
|
||||
if (testTmpDir.exists())
|
||||
testTmpDir.delete();
|
||||
|
@ -275,10 +275,10 @@ public class RequestTest
|
|||
// System.err.println(responses);
|
||||
assertTrue(responses.startsWith("HTTP/1.1 200"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBadMultiPart() throws Exception
|
||||
{
|
||||
{
|
||||
//a bad multipart where one of the fields has no name
|
||||
final File testTmpDir = File.createTempFile("badmptest", null);
|
||||
if (testTmpDir.exists())
|
||||
|
@ -480,7 +480,7 @@ public class RequestTest
|
|||
assertEquals("0.0.0.0",results.get(i++));
|
||||
assertEquals("myhost",results.get(i++));
|
||||
assertEquals("8888",results.get(i++));
|
||||
|
||||
|
||||
|
||||
results.clear();
|
||||
response=_connector.getResponses(
|
||||
|
@ -492,7 +492,7 @@ public class RequestTest
|
|||
assertEquals("0.0.0.0",results.get(i++));
|
||||
assertEquals("myhost",results.get(i++));
|
||||
assertEquals("8888",results.get(i++));
|
||||
|
||||
|
||||
results.clear();
|
||||
response=_connector.getResponses(
|
||||
"GET http://myhost:8888/ HTTP/1.1\n"+
|
||||
|
@ -718,7 +718,7 @@ public class RequestTest
|
|||
String response = _connector.getResponses(request);
|
||||
assertThat(response,Matchers.containsString(" 200 OK"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test8859EncodedForm() throws Exception
|
||||
{
|
||||
|
@ -746,7 +746,7 @@ public class RequestTest
|
|||
String response = _connector.getResponses(request);
|
||||
assertThat(response,Matchers.containsString(" 200 OK"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUTF8EncodedForm() throws Exception
|
||||
{
|
||||
|
@ -774,8 +774,8 @@ public class RequestTest
|
|||
String response = _connector.getResponses(request);
|
||||
assertThat(response,Matchers.containsString(" 200 OK"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testPartialRead() throws Exception
|
||||
{
|
||||
|
@ -859,10 +859,10 @@ public class RequestTest
|
|||
assertTrue(responses.indexOf("read='param=wrong' param=right")>0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSessionAfterRedirect() throws Exception
|
||||
{
|
||||
{
|
||||
Handler handler = new AbstractHandler()
|
||||
{
|
||||
@Override
|
||||
|
@ -1345,7 +1345,7 @@ public class RequestTest
|
|||
((StdErrLog)Log.getLogger(HttpChannel.class)).setHideStacks(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testHashDOSSize() throws Exception
|
||||
{
|
||||
|
@ -1399,6 +1399,40 @@ public class RequestTest
|
|||
request.setCharacterEncoding("doesNotExist");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetterSafeFromNullPointerException()
|
||||
{
|
||||
Request request = new Request(null, null);
|
||||
|
||||
assertNull(request.getAuthType());
|
||||
assertNull(request.getAuthentication());
|
||||
|
||||
assertNull(request.getContentType());
|
||||
|
||||
assertNull(request.getCookies());
|
||||
assertNull(request.getContext());
|
||||
assertNull(request.getContextPath());
|
||||
|
||||
assertNull(request.getHttpFields());
|
||||
assertNull(request.getHttpURI());
|
||||
|
||||
assertNotNull(request.getScheme());
|
||||
assertNotNull(request.getServerName());
|
||||
assertNotNull(request.getServerPort());
|
||||
|
||||
assertNotNull(request.getAttributeNames());
|
||||
assertFalse(request.getAttributeNames().hasMoreElements());
|
||||
|
||||
request.extractParameters();
|
||||
assertNull(request.getQueryString());
|
||||
assertNotNull(request.getQueryParameters());
|
||||
assertEquals(0,request.getQueryParameters().size());
|
||||
assertNotNull(request.getParameterMap());
|
||||
assertEquals(0,request.getParameterMap().size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
interface RequestTester
|
||||
{
|
||||
boolean check(HttpServletRequest request,HttpServletResponse response) throws IOException;
|
||||
|
@ -1467,7 +1501,7 @@ public class RequestTest
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class BadMultiPartRequestHandler extends AbstractHandler
|
||||
{
|
||||
File tmpDir;
|
||||
|
@ -1496,9 +1530,9 @@ public class RequestTest
|
|||
{
|
||||
response.sendError(500);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue