CopyResourceHandler { CopyTask createCopyTask(final HttpServerRequest request, final Buffer buffer) { JsonObject task = new JsonObject(buffer.toString()); HeaderFunction headerFunction = HeaderFunctions.DO_NOTHING; JsonObject staticHeaders = task.getJsonObject("staticHeaders"); if (staticHeaders != null) { headerFunction = HeaderFunctions.parseStaticHeadersFromJson(staticHeaders); } else { final JsonArray dynamicHeaders = task.getJsonArray("headers"); if (dynamicHeaders != null) { headerFunction = HeaderFunctions.parseFromJson(dynamicHeaders); } } MultiMap headers = StatusCodeTranslator.getTranslateFreeHeaders(request.headers()); final HeaderFunctions.EvalScope evalScope = headerFunction.apply(headers); if (evalScope.getErrorMessage() != null) { log.warn("Problem invoking Header functions: {}", evalScope.getErrorMessage()); return null; } HttpHeaderUtil.removeNonForwardHeaders(headers); headers.remove("content-length"); return new CopyTask(task.getString("source"), task.getString("destination"), headers); } CopyResourceHandler(HttpClient selfClient, String copyPath); boolean handle(final HttpServerRequest request);  }
@Test public void testBackwardCompatibility() { String json = " { 'staticHeaders': {" + " 'x-expire-after': 900," + " 'Content-Type': 'application/json'" + "} }"; json = json.replace('\'', '"'); Buffer jsonBuffer = Buffer.buffer(json); CopyTask copyTask = copyResourceHandler.createCopyTask(httpServerRequestMock, jsonBuffer); Assert.assertEquals("900", copyTask.getHeaders().get("x-expire-after")); Assert.assertEquals("application/json", copyTask.getHeaders().get("Content-Type")); }
@Test public void testDynamicHeaderWithNoFunction() { String json = "{ 'headers' : [" + " {'header': 'x-expire-after', 'value': '800'}," + " {'header': 'Content-Type', 'value': 'application/json;charset=UTF-8'}" + "] }"; json = json.replace('\'', '"'); Buffer jsonBuffer = Buffer.buffer(json); CopyTask copyTask = copyResourceHandler.createCopyTask(httpServerRequestMock, jsonBuffer); Assert.assertEquals("800", copyTask.getHeaders().get("x-expire-after")); Assert.assertEquals("application/json;charset=UTF-8", copyTask.getHeaders().get("Content-Type")); }
@Test public void testDynamicHeaderWithFunction() { String json = "{ 'headers' : [" + " { 'header': 'x-bar', 'value': 'I am bar'}," + " { 'header': 'x-foo', 'value': 'bar-{x-bar}'}" + "] }"; json = json.replace('\'', '"'); Buffer jsonBuffer = Buffer.buffer(json); CopyTask copyTask = copyResourceHandler.createCopyTask(httpServerRequestMock, jsonBuffer); Assert.assertEquals("I am bar", copyTask.getHeaders().get("x-bar")); Assert.assertEquals("bar-I am bar", copyTask.getHeaders().get("x-foo")); }
@Test public void testApplyHeadersFailed() { String json = "{ 'headers' : [" + " { 'header': 'x-foo', 'value': 'bar-{x-bar}'}" + "] }"; json = json.replace('\'', '"'); Buffer jsonBuffer = Buffer.buffer(json); CopyTask copyTask = copyResourceHandler.createCopyTask(httpServerRequestMock, jsonBuffer); Assert.assertNull(copyTask); }