Skip to content

Commit

Permalink
Better error message for missing parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ato committed Feb 14, 2017
1 parent 778b7b7 commit 26f62da
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
21 changes: 10 additions & 11 deletions src/tinycdxserver/Web.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,22 @@ static Response notFound() {
return new Response(NOT_FOUND, "text/plain", "Not found\n");
}

static Response badRequest(String message) {
return new Response(BAD_REQUEST, "text/plain", message);
}

static class Router implements Handler {
private final List<Handler> routes = new ArrayList<>();

@Override
public Response handle(IHTTPSession request) {
try {
for (Handler route : routes) {
Response result = route.handle(request);
if (result != null) {
return result;
}
public Response handle(IHTTPSession request) throws Exception {
for (Handler route : routes) {
Response result = route.handle(request);
if (result != null) {
return result;
}
return Web.notFound();
} catch (Exception e) {
e.printStackTrace();
return new Response(INTERNAL_ERROR, "text/plain", e.toString() + "\n");
}
return Web.notFound();
}

public Router on(Method method, String pathPattern, Handler handler) {
Expand Down
14 changes: 11 additions & 3 deletions src/tinycdxserver/Webapp.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,25 @@ private Response listAccessRules(IHTTPSession request) throws IOException, Web.R

Response checkAccess(IHTTPSession request) throws IOException, ResponseException {
String accesspoint = request.getParms().get("accesspoint");
String url = request.getParms().get("url");
String timestamp = request.getParms().get("timestamp");
String url = getMandatoryParam(request, "url");
String timestamp = getMandatoryParam(request, "timestamp");

Date captureTime = Date.from(LocalDateTime.parse(timestamp, Capture.arcTimeFormat).toInstant(ZoneOffset.UTC));
Date accessTime = new Date();

return jsonResponse(getIndex(request).accessControl.checkAccess(accesspoint, url, captureTime, accessTime));
}

private String getMandatoryParam(IHTTPSession request, String name) throws ResponseException {
String value = request.getParms().get(name);
if (value == null) {
throw new Web.ResponseException(badRequest("missing mandatory parameter: " + name));
}
return value;
}

@Override
public Response handle(IHTTPSession session) throws IOException, Web.ResponseException {
public Response handle(IHTTPSession session) throws Exception {
Response response = router.handle(session);
if (response != null) {
response.addHeader("Access-Control-Allow-Origin", "*");
Expand Down
12 changes: 6 additions & 6 deletions test/tinycdxserver/WebappTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ List<String> cdxUrls(String cdx) {
return urls;
}

private long createRule(long policyId, String... surts) throws IOException, Web.ResponseException {
private long createRule(long policyId, String... surts) throws Exception {
AccessRule rule = new AccessRule();
rule.policyId = policyId;
rule.urlPatterns.addAll(asList(surts));
String response = POST("/testap/access/rules", GSON.toJson(rule), CREATED);
return GSON.fromJson(response, Id.class).id;
}

private long createPolicy(String name, String... accessPoints) throws IOException, Web.ResponseException {
private long createPolicy(String name, String... accessPoints) throws Exception {
AccessPolicy publicPolicy = new AccessPolicy();
publicPolicy.name = name;
publicPolicy.accessPoints.addAll(asList(accessPoints));
Expand All @@ -162,18 +162,18 @@ public static class Id {
public long id;
}

private String POST(String url, String data) throws IOException, Web.ResponseException {
private String POST(String url, String data) throws Exception {
return POST(url, data, OK);
}
private String POST(String url, String data, NanoHTTPD.Response.Status expectedStatus) throws IOException, Web.ResponseException {
private String POST(String url, String data, NanoHTTPD.Response.Status expectedStatus) throws Exception {
DummySession session = new DummySession(POST, url);
session.data(data);
NanoHTTPD.Response response = webapp.handle(session);
assertEquals(expectedStatus, response.getStatus());
return slurp(response);
}

private String GET(String url, String... parmKeysAndValues) throws IOException, Web.ResponseException {
private String GET(String url, String... parmKeysAndValues) throws Exception {
DummySession session = new DummySession(GET, url);
for (int i = 0; i < parmKeysAndValues.length; i += 2) {
session.parm(parmKeysAndValues[i], parmKeysAndValues[i + 1]);
Expand All @@ -183,7 +183,7 @@ private String GET(String url, String... parmKeysAndValues) throws IOException,
return slurp(response);
}

private String DELETE(String url) throws IOException, Web.ResponseException {
private String DELETE(String url) throws Exception {
DummySession session = new DummySession(DELETE, url);
NanoHTTPD.Response response = webapp.handle(session);
assertEquals(OK, response.getStatus());
Expand Down

0 comments on commit 26f62da

Please sign in to comment.