Skip to content

Commit

Permalink
Allow optional trailing slash in URLs
Browse files Browse the repository at this point in the history
Change-Id: I8f457c93227973e17c37df801f0d5a2d564f930b
Reviewed-on: http://review.couchbase.org/11926
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
  • Loading branch information
avsej authored and trondn committed Dec 28, 2011
1 parent 7134187 commit 42e2f77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/couchbase/mock/http/PoolsHandler.java
Expand Up @@ -85,14 +85,14 @@ public void handle(HttpExchange exchange) throws IOException {
payload = getPoolsJSON().getBytes();
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, payload.length);
body.write(payload);
} else if (path.matches("^/pools/" + mock.getPoolName() + "$")) {
} else if (path.matches("^/pools/" + mock.getPoolName() + "$/?")) {
// GET /pools/:poolName
StringWriter sw = new StringWriter();
sw.append("{\"buckets\":{\"uri\":\"/pools/" + mock.getPoolName() + "/buckets/default\"}}");
payload = sw.toString().getBytes();
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, payload.length);
body.write(payload);
} else if (path.matches("^/pools/" + mock.getPoolName() + "/buckets$")) {
} else if (path.matches("^/pools/" + mock.getPoolName() + "/buckets/?$")) {
// GET /pools/:poolName/buckets
JSONArray buckets = new JSONArray();
for (Bucket bucket : mock.getBuckets().values()) {
Expand All @@ -105,14 +105,14 @@ public void handle(HttpExchange exchange) throws IOException {
payload = buckets.toString().getBytes();
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, payload.length);
body.write(payload);
} else if (path.matches("^/pools/" + mock.getPoolName() + "/buckets/[^/]+$")) {
} else if (path.matches("^/pools/" + mock.getPoolName() + "/buckets/[^/]+/?$")) {
// GET /pools/:poolName/buckets/:bucketName
String[] tokens = path.split("/");
Bucket bucket = mock.getBuckets().get(tokens[tokens.length - 1]);
payload = bucket.getJSON().getBytes();
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, payload.length);
body.write(payload);
} else if (path.matches("^/pools/" + mock.getPoolName() + "/bucketsStreaming/[^/]+$")) {
} else if (path.matches("^/pools/" + mock.getPoolName() + "/bucketsStreaming/[^/]+/?$")) {
// GET /pools/:poolName/bucketsStreaming/:bucketName
String[] tokens = path.split("/");
Bucket bucket = mock.getBuckets().get(tokens[tokens.length - 1]);
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/couchbase/mock/JMembaseTest.java
Expand Up @@ -100,6 +100,21 @@ public void testHandleHttpRequest() throws IOException {
}
}

public void testHandleHttpRequestWithTrailingSlash() throws IOException {
System.out.println("testHandleHttpRequestWithTrailingSlash");
URL url = new URL("http://localhost:" + instance.getHttpPort() + "/pools/default/buckets/protected/");
HttpURLConnection conn = null;

try {
conn = (HttpURLConnection) url.openConnection();
assertNotNull(conn);
conn.addRequestProperty("Authorization", "Basic " + Base64.encode("protected:secret"));
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
} catch (Exception ex) {
fail(ex.getMessage());
}
}

public void testAdministratorCouldAccessProtectedBuckets() throws IOException {
System.out.println("testAdministratorCouldAccessProtectedBuckets");
URL url = new URL("http://localhost:" + instance.getHttpPort() + "/pools/default/buckets/protected");
Expand Down

0 comments on commit 42e2f77

Please sign in to comment.