HttpRequestHandler { public Map<String, String> handleCorsPreflightRequest(String pOrigin, String pRequestHeaders) { Map<String,String> ret = new HashMap<String, String>(); if (backendManager.isOriginAllowed(pOrigin,false)) { ret.put("Access-Control-Allow-Origin",pOrigin == null || "null".equals(pOrigin) ? "*" : pOrigin); if (pRequestHeaders != null) { ret.put("Access-Control-Allow-Headers",pRequestHeaders); } ret.put("Access-Control-Allow-Credentials","true"); ret.put("Access-Control-Max-Age","" + 3600 * 24 * 365); } return ret; } HttpRequestHandler(Configuration pConfig, BackendManager pBackendManager, LogHandler pLogHandler); JSONAware handleGetRequest(String pUri, String pPathInfo, Map<String, String[]> pParameterMap); JSONAware handlePostRequest(String pUri, InputStream pInputStream, String pEncoding, Map<String, String[]>  pParameterMap); Map<String, String> handleCorsPreflightRequest(String pOrigin, String pRequestHeaders); JSONObject handleThrowable(Throwable pThrowable); JSONObject getErrorJSON(int pErrorCode, Throwable pExp, JmxRequest pJmxReq); void checkAccess(String pHost, String pAddress, String pOrigin); String extractCorsOrigin(String pOrigin);  }
@Test public void preflightCheck() { String origin = "http: String headers ="X-Data: Test"; expect(backend.isOriginAllowed(origin,false)).andReturn(true); replay(backend); Map<String,String> ret = handler.handleCorsPreflightRequest(origin, headers); assertEquals(ret.get("Access-Control-Allow-Origin"),origin); }
@Test public void preflightCheckNegative() { String origin = "http: String headers ="X-Data: Test"; expect(backend.isOriginAllowed(origin,false)).andReturn(false); replay(backend); Map<String,String> ret = handler.handleCorsPreflightRequest(origin, headers); assertNull(ret.get("Access-Control-Allow-Origin")); }