private Result process(HttpServletRequest request, HttpServletResponse response, String body) { try { JSONObject req = new JSONObject(body); JSONObject resp = jsonHandler.process(req); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/json; charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=rpc.txt"); return new Result(resp.toString(), true); } catch (JSONException e) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return new Result("Malformed JSON request.", false); } catch (RpcException e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); LOG.log(Level.INFO, e.getMessage(), e); return new Result(e.getMessage(), false); } }
processor.submit(createNewJob(context));
private Result process(HttpServletRequest request, HttpServletResponse response, String body) { try { JSONObject req = new JSONObject(body); JSONObject resp = jsonHandler.process(req); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/json; charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=rpc.txt"); return new Result(resp.toString(), true); } catch (JSONException e) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return new Result("Malformed JSON request.", false); } catch (RpcException e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); LOG.log(Level.INFO, e.getMessage(), e); return new Result(e.getMessage(), false); } }
processor.submit(createNewJob(context));
private Result process(HttpServletRequest request, HttpServletResponse response, String body) { try { JSONObject req = new JSONObject(body); JSONObject resp = jsonHandler.process(req); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("application/json; charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=rpc.txt"); return new Result(resp.toString(), true); } catch (JSONException e) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); return new Result("Malformed JSON request.", false); } catch (RpcException e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); LOG.log(Level.INFO, e.getMessage(), e); return new Result(e.getMessage(), false); } }
processor.submit(createNewJob(context));
@Test public void testUnexpectedError() throws Exception { JSONArray gadgets = new JSONArray() .put(createGadget(FakeProcessor.SPEC_URL.toString(), 0, null)); JSONObject input = new JSONObject() .put("context", createContext("en", "US")) .put("gadgets", gadgets); urlGenerator.throwRandomFault = true; JSONObject resp = jsonRpcHandler.process(input); String actual = resp.getJSONArray("gadgets").getJSONObject(0).getJSONArray("errors").getString(0); assertEquals("BROKEN", actual); }
@Test public void testUnexpectedError() throws Exception { JSONArray gadgets = new JSONArray() .put(createGadget(FakeProcessor.SPEC_URL.toString(), 0, null)); JSONObject input = new JSONObject() .put("context", createContext("en", "US")) .put("gadgets", gadgets); urlGenerator.throwRandomFault = true; JSONObject resp = jsonRpcHandler.process(input); String actual = resp.getJSONArray("gadgets").getJSONObject(0).getJSONArray("errors").getString(0); assertEquals("BROKEN", actual); }
@Test public void testUnexpectedError() throws Exception { JSONArray gadgets = new JSONArray() .put(createGadget(FakeProcessor.SPEC_URL.toString(), 0, null)); JSONObject input = new JSONObject() .put("context", createContext("en", "US")) .put("gadgets", gadgets); urlGenerator.throwRandomFault = true; JSONObject resp = jsonRpcHandler.process(input); String actual = resp.getJSONArray("gadgets").getJSONObject(0).getJSONArray("errors").getString(0); assertEquals("BROKEN", actual); }
@Test public void testDoGetNormal() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._"); HttpServletResponse response = createHttpResponse("Content-Disposition", "attachment;filename=rpc.txt", "application/json; charset=utf-8", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._({\"GADGETS\":[]})", HttpServletResponse.SC_OK); JSONObject handlerResponse = new JSONObject("{\"GADGETS\":[]}"); expect(handler.process(isA(JSONObject.class))).andReturn(handlerResponse); replay(handler); servlet.doGet(request, response); verify(response); }
@Test public void testDoGetNormal() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._"); HttpServletResponse response = createHttpResponse("Content-Disposition", "attachment;filename=rpc.txt", "application/json; charset=utf-8", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._({\"GADGETS\":[]})", HttpServletResponse.SC_OK); JSONObject handlerResponse = new JSONObject("{\"GADGETS\":[]}"); expect(handler.process(isA(JSONObject.class))).andReturn(handlerResponse); replay(handler); servlet.doGet(request, response); verify(response); }
@Test public void testDoGetNormal() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._"); HttpServletResponse response = createHttpResponse("Content-Disposition", "attachment;filename=rpc.txt", "application/json; charset=utf-8", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._({\"GADGETS\":[]})", HttpServletResponse.SC_OK); JSONObject handlerResponse = new JSONObject("{\"GADGETS\":[]}"); expect(handler.process(isA(JSONObject.class))).andReturn(handlerResponse); replay(handler); servlet.doGet(request, response); verify(response); }
@Test public void testDisallowJSONP() throws Exception { servlet.setJSONPAllowed(false); HttpServletRequest request = createGetRequest("{\"gadgets\":[]}",null); HttpServletResponse response = createHttpResponse("Content-Disposition", "attachment;filename=rpc.txt", "application/json; charset=utf-8", "{\"GADGETS\":[]}", HttpServletResponse.SC_OK); JSONObject handlerResponse = new JSONObject("{\"GADGETS\":[]}"); expect(handler.process(isA(JSONObject.class))).andReturn(handlerResponse); replay(handler); servlet.doGet(request, response); verify(response); servlet.setJSONPAllowed(true); }
@Test public void testDisallowJSONP() throws Exception { servlet.setJSONPAllowed(false); HttpServletRequest request = createGetRequest("{\"gadgets\":[]}",null); HttpServletResponse response = createHttpResponse("Content-Disposition", "attachment;filename=rpc.txt", "application/json; charset=utf-8", "{\"GADGETS\":[]}", HttpServletResponse.SC_OK); JSONObject handlerResponse = new JSONObject("{\"GADGETS\":[]}"); expect(handler.process(isA(JSONObject.class))).andReturn(handlerResponse); replay(handler); servlet.doGet(request, response); verify(response); servlet.setJSONPAllowed(true); }
@Test public void testDoGetWithHandlerJsonException() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "function"); HttpServletResponse response = createHttpResponse("Malformed JSON request.", HttpServletResponse.SC_BAD_REQUEST); expect(handler.process(isA(JSONObject.class))).andThrow(new JSONException("json")); replay(handler); servlet.doGet(request, response); verify(response); }
@Test public void testDoGetWithHandlerRpcException() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "function"); HttpServletResponse response = createHttpResponse("rpcExceptionMessage", HttpServletResponse.SC_INTERNAL_SERVER_ERROR); expect(handler.process(isA(JSONObject.class))).andThrow( new RpcException("rpcExceptionMessage")); replay(handler); servlet.doGet(request, response); verify(response); }
@Test public void testDoGetWithHandlerRpcException() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "function"); HttpServletResponse response = createHttpResponse("rpcExceptionMessage", HttpServletResponse.SC_INTERNAL_SERVER_ERROR); expect(handler.process(isA(JSONObject.class))).andThrow( new RpcException("rpcExceptionMessage")); replay(handler); servlet.doGet(request, response); verify(response); }
@Test public void testDoGetWithHandlerJsonException() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "function"); HttpServletResponse response = createHttpResponse("Malformed JSON request.", HttpServletResponse.SC_BAD_REQUEST); expect(handler.process(isA(JSONObject.class))).andThrow(new JSONException("json")); replay(handler); servlet.doGet(request, response); verify(response); }
@Test public void testDoGetWithHandlerJsonException() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "function"); HttpServletResponse response = createHttpResponse("Malformed JSON request.", HttpServletResponse.SC_BAD_REQUEST); expect(handler.process(isA(JSONObject.class))).andThrow(new JSONException("json")); replay(handler); servlet.doGet(request, response); verify(response); }
@Test public void testDoGetWithHandlerRpcException() throws Exception { HttpServletRequest request = createGetRequest("{\"gadgets\":[]}", "function"); HttpServletResponse response = createHttpResponse("rpcExceptionMessage", HttpServletResponse.SC_INTERNAL_SERVER_ERROR); expect(handler.process(isA(JSONObject.class))).andThrow( new RpcException("rpcExceptionMessage")); replay(handler); servlet.doGet(request, response); verify(response); }