ProxyHelper { public String getProxyUrl(String baseUrl, String endPoint, MultivaluedMap<String, String> queryParameters, String authType) { Set<String> keySet = queryParameters.keySet(); URIBuilder builder; try { builder = new URIBuilder(baseUrl + "/" + endPoint); for(String key: keySet) { builder.addParameter(key, queryParameters.getFirst(key)); } if(authType == null || authType.equalsIgnoreCase("simple")) { builder.addParameter("user.name", viewContext.getUsername()); } return builder.build().toString(); } catch (URISyntaxException e) { LOG.error("Failed to build a URL from the baseUrl: {} and endPoint: {}. Exception: {}", baseUrl, endPoint, e); throw new TezWebAppException("Failed to build a URL from the baseUrl:" + baseUrl + "and endPoint: " + endPoint, e); } } @Inject  ProxyHelper(ViewContext viewContext); String getResponse(String url, Map<String, String> headers, String authType); String getProxyUrl(String baseUrl, String endPoint, MultivaluedMap<String, String> queryParameters, String authType);  }
@Test public void shouldBuildURLWithNoQueryParameters() throws Exception { ViewContext context = createNiceMock(ViewContext.class); ProxyHelper helper = new ProxyHelper(context); assertEquals("http: assertEquals("http: }
@Test public void shouldBuildURLWithQueryParametersHavingSpecialCharacters() throws Exception { ViewContext context = createNiceMock(ViewContext.class); ProxyHelper helper = new ProxyHelper(context); MultivaluedMap<String, String> map = new MultivaluedHashMap<>(); map.putSingle("data", "abcd/efgh"); assertEquals("http: map.putSingle("data", "abcd efgh"); assertEquals("http: }
@Test(expected = TezWebAppException.class) public void shouldThrowExceptionIfWrongUrl() throws Exception { ViewContext context = createNiceMock(ViewContext.class); ProxyHelper helper = new ProxyHelper(context); helper.getProxyUrl("####", "", new MultivaluedHashMap<String, String>(), "kerberos"); }
@Test public void shouldAddUserIfAuthTypeIsSimple() throws Exception { ViewContext context = createNiceMock(ViewContext.class); expect(context.getUsername()).andReturn("admin").anyTimes(); ProxyHelper helper = new ProxyHelper(context); MultivaluedMap<String, String> map = new MultivaluedHashMap<>(); replay(context); assertEquals("http: assertEquals("http: }