### Question:
CsvAction extends Action<CsvAction> { @Override public void run(Request req, Response res) throws ApiException { if (!"GET".equals(req.getMethod()) || 200 != res.getStatusCode() || res.getJson() == null || res.getText() != null) { return; } if (!"csv".equalsIgnoreCase(req.getUrl().getParam("format")) && !"csv".equalsIgnoreCase(req.getChain().getConfig("format", null))) { return; } JSNode arr = res.getJson(); if (!(arr instanceof JSArray)) { if (res.getJson().hasProperty("data")) { arr = res.getJson().getArray("data"); } else { arr = new JSArray(arr); } } byte[] bytes = toCsv((JSArray) arr).getBytes(); res.withHeader("Content-Length", bytes.length + ""); res.debug("Content-Length " + bytes.length + ""); res.withText(new String(bytes)); }  @Override void run(Request req, Response res); String toCsv(JSArray arr);  }### Answer:
@Test public void run_shouldReturnCsv_when_singleObject_in_response() throws Exception { Response res = new Response(); JSNode singleObject = new JSNode(); singleObject.put("key1", "value1"); singleObject.put("key2", "value2"); res.withJson(singleObject); action.run(buildSuccessfulRequest(), res); assertNull(res.getJson()); assertEquals("key1,key2\r\nvalue1,value2\r\n", res.getText()); }
@Test public void run_shouldReturnCsv_when_ArrayObject_in_response() throws Exception { Response res = new Response(); JSNode singleObject = new JSNode(); singleObject.put("key1", "value1"); singleObject.put("key2", "value2"); JSArray array = new JSArray(); array.put(0, singleObject); array.put(1, singleObject.copy()); res.withJson(array); action.run(buildSuccessfulRequest(), res); assertNull(res.getJson()); assertEquals("key1,key2\r\nvalue1,value2\r\nvalue1,value2\r\n", res.getText()); }
@Test public void run_shouldReturnCsv_when_InversionWrapper_in_response() throws Exception { Response res = new Response(); JSNode singleObject = new JSNode(); singleObject.put("key1", "value1"); singleObject.put("key2", "value2"); JSArray array = new JSArray(); array.put(0, singleObject); array.put(1, singleObject.copy()); res.withData(array); res.withMeta("blah", "blahvalue"); action.run(buildSuccessfulRequest(), res); assertNull(res.getJson()); assertEquals("key1,key2\r\nvalue1,value2\r\nvalue1,value2\r\n", res.getText()); }
@Test public void run_should_doNothing_when_notGetRequest() throws Exception { Response res = new Response(); JSNode singleObject = new JSNode(); singleObject.put("key1", "value1"); singleObject.put("key2", "value2"); res.withJson(singleObject); Map<String, String> params = new HashMap<>(); params.put("format", "csv"); action.run(new Request("POST", "/", null, params, null), res); assertEquals(singleObject, res.getJson()); }