Browse files

Allow optional trailing slash in URLs

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...
1 parent 7134187 commit 42e2f774fb008249615954f87870985440a20096 @avsej avsej committed with trondn Dec 28, 2011
View
8 src/main/java/org/couchbase/mock/http/PoolsHandler.java
@@ -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()) {
@@ -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]);
View
15 src/test/java/org/couchbase/mock/JMembaseTest.java
@@ -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");

0 comments on commit 42e2f77

Please sign in to comment.