@Inject public void setLockedDomainService(LockedDomainService lockedDomainService) { checkInitialized(); this.lockedDomainService = lockedDomainService; }
@Before public void setUp() throws Exception { servlet.setProxyHandler(proxyHandler); servlet.setProxyUriManager(proxyUriManager); servlet.setLockedDomainService(lockedDomainService); }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse servletResponse) throws IOException { processRequest(request, servletResponse); }
@Test public void testDoGetWithOAuth2() throws Exception { Map<String, String> options = new HashMap<String, String>(); options.put("OAUTH_SERVICE_NAME", "example"); ProxyUriManager.ProxyUri proxyUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); proxyUri.setAuthType(AuthType.OAUTH2); Uri uri = Uri.parse(BASIC_SYNTAX_URL + "&authz=oauth2&OAUTH_SERVICE_NAME=example&container=default&gadget=http://example.org/gadget.xml"); expect(proxyUriManager.process(uri)).andReturn(proxyUri); expect(request.getScheme()).andReturn(uri.getScheme()); expect(request.getServerName()).andReturn(uri.getAuthority()); expect(request.getServerPort()).andReturn(80); expect(request.getRequestURI()).andReturn(uri.getPath()); expect(request.getQueryString()).andReturn(uri.getQuery()); expect(request.getHeader("Host")).andReturn(uri.getAuthority()); expect(request.getParameter("OAUTH_SERVICE_NAME")).andReturn("example"); expect(request.getParameterNames()).andReturn(Collections.enumeration(options.keySet())); expect(lockedDomainService.isSafeForOpenProxy(uri.getAuthority())).andReturn(true); ProxyUriManager.ProxyUri pUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); pUri.setAuthType(AuthType.OAUTH2); pUri.setOAuth2Arguments(new OAuth2Arguments(AuthType.OAUTH2, options)); expect(proxyHandler.fetch(pUri)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doGet(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); }
@Test public void testDoPostNormal() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(request.getInputStream()).andReturn(postContentStream); expect(request.getMethod()).andReturn(POST_METHOD); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doPost(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); }
StringBuffer buffer = getPOSTContent(request); response = proxyHandler.fetch(proxyUri, buffer.toString()); } else {
@Test public void testDoGetWithOAuth() throws Exception { Map<String, String> options = new HashMap<String, String>(); options.put("OAUTH_SERVICE_NAME", "example"); ProxyUriManager.ProxyUri proxyUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); proxyUri.setAuthType(AuthType.OAUTH); Uri uri = Uri.parse(BASIC_SYNTAX_URL + "&authz=oauth&OAUTH_SERVICE_NAME=example&container=default&gadget=http://example.org/gadget.xml"); expect(proxyUriManager.process(uri)).andReturn(proxyUri); expect(request.getScheme()).andReturn(uri.getScheme()); expect(request.getServerName()).andReturn(uri.getAuthority()); expect(request.getServerPort()).andReturn(80); expect(request.getRequestURI()).andReturn(uri.getPath()); expect(request.getQueryString()).andReturn(uri.getQuery()); expect(request.getHeader("Host")).andReturn(uri.getAuthority()); expect(request.getParameter("OAUTH_SERVICE_NAME")).andReturn("example"); expect(request.getParameterNames()).andReturn(Collections.enumeration(options.keySet())); expect(lockedDomainService.isSafeForOpenProxy(uri.getAuthority())).andReturn(true); ProxyUriManager.ProxyUri pUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); pUri.setAuthType(AuthType.OAUTH); pUri.setOAuthArguments(new OAuthArguments(AuthType.OAUTH, options)); expect(proxyHandler.fetch(pUri)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doGet(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); } }
@Test public void testDoPostNormal() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(request.getInputStream()).andReturn(postContentStream); expect(request.getMethod()).andReturn(POST_METHOD); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doPost(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); }
@Before public void setUp() throws Exception { servlet.setProxyHandler(proxyHandler); servlet.setProxyUriManager(proxyUriManager); servlet.setLockedDomainService(lockedDomainService); }
@Test public void testIfModifiedSinceAlwaysReturnsEarly() throws Exception { expect(request.getHeader("If-Modified-Since")).andReturn("Yes, this is an invalid header"); replay(); servlet.doGet(request, recorder); verify(); assertEquals(HttpServletResponse.SC_NOT_MODIFIED, recorder.getHttpStatusCode()); assertFalse(rewriter.responseWasRewritten()); }
@Inject public void setProxyHandler(ProxyHandler proxyHandler) { checkInitialized(); this.proxyHandler = proxyHandler; }
@Test public void testDoPostHttpError() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andReturn(HttpResponse.notFound()); expect(request.getMethod()).andReturn(POST_METHOD); expect(request.getInputStream()).andReturn(postContentStream); replay(); servlet.doPost(request, recorder); verify(); assertResponseOk(HttpResponse.SC_NOT_FOUND, ""); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse servletResponse) throws IOException { processRequest(request, servletResponse); }
@Before public void setUp() throws Exception { servlet.setProxyHandler(proxyHandler); servlet.setProxyUriManager(proxyUriManager); servlet.setLockedDomainService(lockedDomainService); }
@Test public void testDoGetWithOAuth() throws Exception { Map<String, String> options = new HashMap<String, String>(); options.put("OAUTH_SERVICE_NAME", "example"); ProxyUriManager.ProxyUri proxyUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); proxyUri.setAuthType(AuthType.OAUTH); Uri uri = Uri.parse(BASIC_SYNTAX_URL + "&authz=oauth&OAUTH_SERVICE_NAME=example&container=default&gadget=http://example.org/gadget.xml"); expect(proxyUriManager.process(uri)).andReturn(proxyUri); expect(request.getScheme()).andReturn(uri.getScheme()); expect(request.getServerName()).andReturn(uri.getAuthority()); expect(request.getServerPort()).andReturn(80); expect(request.getRequestURI()).andReturn(uri.getPath()); expect(request.getQueryString()).andReturn(uri.getQuery()); expect(request.getHeader("Host")).andReturn(uri.getAuthority()); expect(request.getParameter("OAUTH_SERVICE_NAME")).andReturn("example"); expect(request.getParameterNames()).andReturn(Collections.enumeration(options.keySet())); expect(lockedDomainService.isSafeForOpenProxy(uri.getAuthority())).andReturn(true); ProxyUriManager.ProxyUri pUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); pUri.setAuthType(AuthType.OAUTH); pUri.setOAuthArguments(new OAuthArguments(AuthType.OAUTH, options)); expect(proxyHandler.fetch(pUri)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doGet(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); } }
@Inject public void setProxyUriManager(ProxyUriManager proxyUriManager) { checkInitialized(); this.proxyUriManager = proxyUriManager; }
@Test public void testDoPostHttpError() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andReturn(HttpResponse.notFound()); expect(request.getMethod()).andReturn(POST_METHOD); expect(request.getInputStream()).andReturn(postContentStream); replay(); servlet.doPost(request, recorder); verify(); assertResponseOk(HttpResponse.SC_NOT_FOUND, ""); }
@Test public void testDoGetWithOAuth2() throws Exception { Map<String, String> options = new HashMap<String, String>(); options.put("OAUTH_SERVICE_NAME", "example"); ProxyUriManager.ProxyUri proxyUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); proxyUri.setAuthType(AuthType.OAUTH2); Uri uri = Uri.parse(BASIC_SYNTAX_URL + "&authz=oauth2&OAUTH_SERVICE_NAME=example&container=default&gadget=http://example.org/gadget.xml"); expect(proxyUriManager.process(uri)).andReturn(proxyUri); expect(request.getScheme()).andReturn(uri.getScheme()); expect(request.getServerName()).andReturn(uri.getAuthority()); expect(request.getServerPort()).andReturn(80); expect(request.getRequestURI()).andReturn(uri.getPath()); expect(request.getQueryString()).andReturn(uri.getQuery()); expect(request.getHeader("Host")).andReturn(uri.getAuthority()); expect(request.getParameter("OAUTH_SERVICE_NAME")).andReturn("example"); expect(request.getParameterNames()).andReturn(Collections.enumeration(options.keySet())); expect(lockedDomainService.isSafeForOpenProxy(uri.getAuthority())).andReturn(true); ProxyUriManager.ProxyUri pUri = new ProxyUri(-1, false, true, "default", "http://example.org/gadget.xml", REQUEST_URL); pUri.setAuthType(AuthType.OAUTH2); pUri.setOAuth2Arguments(new OAuth2Arguments(AuthType.OAUTH2, options)); expect(proxyHandler.fetch(pUri)).andReturn(new HttpResponse(RESPONSE_BODY)); replay(); servlet.doGet(request, recorder); verify(); assertResponseOk(HttpResponse.SC_OK, RESPONSE_BODY); }
@Inject public void setProxyUriManager(ProxyUriManager proxyUriManager) { checkInitialized(); this.proxyUriManager = proxyUriManager; }
@Test public void testDoPostException() throws Exception { setupRequest(BASIC_SYNTAX_URL); expect(request.getInputStream()).andReturn(postContentStream); expect(request.getMethod()).andReturn(POST_METHOD); expect(proxyHandler.fetch(proxyUri, POST_CONTENT)).andThrow( new GadgetException(GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT, ERROR_MESSAGE)); replay(); servlet.doPost(request, recorder); verify(); assertEquals(HttpServletResponse.SC_BAD_REQUEST, recorder.getHttpStatusCode()); assertContains(ERROR_MESSAGE, recorder.getResponseAsString()); }