@Test public void testAuthorizationHeaderWithNonMatchingUrl() throws Exception { final String expectedBody = setupStub(); final String headerValue = "foobar"; String authorization = "Authorization"; final AuthorizationValue authorizationValue = new AuthorizationValue(authorization, headerValue, "header", u -> false); final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue)); assertEquals(actualBody, expectedBody); List<LoggedRequest> requests = WireMock.findAll(getRequestedFor(urlEqualTo("/v2/pet/1"))); assertEquals(1, requests.size()); assertFalse(requests.get(0).containsHeader(authorization)); }
public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception { InputStream is = null; BufferedReader br = null; try { URLConnection conn; do { final URL inUrl = new URL(cleanUrl(url)); final List<AuthorizationValue> query = new ArrayList<>(); final List<AuthorizationValue> header = new ArrayList<>(); if (auths != null && auths.size() > 0) { for (AuthorizationValue auth : auths) { if (auth.getUrlMatcher()!= null) { if (auth.getUrlMatcher().test(inUrl)) { if ("query".equals(auth.getType())) { appendValue(inUrl, auth, query); } else if ("header".equals(auth.getType())) { appendValue(inUrl, auth, header); } } } } } if (!query.isEmpty()) { final URI inUri = inUrl.toURI(); final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery()); for (AuthorizationValue item : query) { if (newQuery.length() > 0) { newQuery.append("&"); } newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=") .append(URLEncoder.encode(item.getValue(), UTF_8.name())); } conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection(); } else { conn = inUrl.openConnection(); } CONNECTION_CONFIGURATOR.process(conn); for (AuthorizationValue item : header) { conn.setRequestProperty(item.getKeyName(), item.getValue()); } conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE); conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE); conn.connect(); url = ((HttpURLConnection) conn).getHeaderField("Location"); } while ((301 == ((HttpURLConnection) conn).getResponseCode())||(302 == ((HttpURLConnection) conn).getResponseCode()) || (307 == ((HttpURLConnection) conn).getResponseCode())||(308 == ((HttpURLConnection) conn).getResponseCode())); InputStream in = conn.getInputStream(); StringBuilder contents = new StringBuilder(); BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8)); for (int i = 0; i != -1; i = input.read()) { char c = (char) i; if (!Character.isISOControl(c)) { contents.append((char) i); } if (c == '\n') { contents.append('\n'); } } in.close(); return contents.toString(); } catch (javax.net.ssl.SSLProtocolException e) { LOGGER.warn("there is a problem with the target SSL certificate"); LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n"); LOGGER.error("unable to read", e); throw e; } catch (Exception e) { LOGGER.error("unable to read", e); throw e; } finally { if (is != null) { is.close(); } if (br != null) { br.close(); } } }
RemoteUrl { public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception { InputStream is = null; BufferedReader br = null; try { URLConnection conn; do { final URL inUrl = new URL(cleanUrl(url)); final List<AuthorizationValue> query = new ArrayList<>(); final List<AuthorizationValue> header = new ArrayList<>(); if (auths != null && auths.size() > 0) { for (AuthorizationValue auth : auths) { if (auth.getUrlMatcher()!= null) { if (auth.getUrlMatcher().test(inUrl)) { if ("query".equals(auth.getType())) { appendValue(inUrl, auth, query); } else if ("header".equals(auth.getType())) { appendValue(inUrl, auth, header); } } } } } if (!query.isEmpty()) { final URI inUri = inUrl.toURI(); final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery()); for (AuthorizationValue item : query) { if (newQuery.length() > 0) { newQuery.append("&"); } newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=") .append(URLEncoder.encode(item.getValue(), UTF_8.name())); } conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection(); } else { conn = inUrl.openConnection(); } CONNECTION_CONFIGURATOR.process(conn); for (AuthorizationValue item : header) { conn.setRequestProperty(item.getKeyName(), item.getValue()); } conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE); conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE); conn.connect(); url = ((HttpURLConnection) conn).getHeaderField("Location"); } while ((301 == ((HttpURLConnection) conn).getResponseCode())||(302 == ((HttpURLConnection) conn).getResponseCode()) || (307 == ((HttpURLConnection) conn).getResponseCode())||(308 == ((HttpURLConnection) conn).getResponseCode())); InputStream in = conn.getInputStream(); StringBuilder contents = new StringBuilder(); BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8)); for (int i = 0; i != -1; i = input.read()) { char c = (char) i; if (!Character.isISOControl(c)) { contents.append((char) i); } if (c == '\n') { contents.append('\n'); } } in.close(); return contents.toString(); } catch (javax.net.ssl.SSLProtocolException e) { LOGGER.warn("there is a problem with the target SSL certificate"); LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n"); LOGGER.error("unable to read", e); throw e; } catch (Exception e) { LOGGER.error("unable to read", e); throw e; } finally { if (is != null) { is.close(); } if (br != null) { br.close(); } } } }
RemoteUrl { public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception { InputStream is = null; BufferedReader br = null; try { URLConnection conn; do { final URL inUrl = new URL(cleanUrl(url)); final List<AuthorizationValue> query = new ArrayList<>(); final List<AuthorizationValue> header = new ArrayList<>(); if (auths != null && auths.size() > 0) { for (AuthorizationValue auth : auths) { if (auth.getUrlMatcher()!= null) { if (auth.getUrlMatcher().test(inUrl)) { if ("query".equals(auth.getType())) { appendValue(inUrl, auth, query); } else if ("header".equals(auth.getType())) { appendValue(inUrl, auth, header); } } } } } if (!query.isEmpty()) { final URI inUri = inUrl.toURI(); final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery()); for (AuthorizationValue item : query) { if (newQuery.length() > 0) { newQuery.append("&"); } newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=") .append(URLEncoder.encode(item.getValue(), UTF_8.name())); } conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection(); } else { conn = inUrl.openConnection(); } CONNECTION_CONFIGURATOR.process(conn); for (AuthorizationValue item : header) { conn.setRequestProperty(item.getKeyName(), item.getValue()); } conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE); conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE); conn.connect(); url = ((HttpURLConnection) conn).getHeaderField("Location"); } while ((301 == ((HttpURLConnection) conn).getResponseCode())||(302 == ((HttpURLConnection) conn).getResponseCode()) || (307 == ((HttpURLConnection) conn).getResponseCode())||(308 == ((HttpURLConnection) conn).getResponseCode())); InputStream in = conn.getInputStream(); StringBuilder contents = new StringBuilder(); BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8)); for (int i = 0; i != -1; i = input.read()) { char c = (char) i; if (!Character.isISOControl(c)) { contents.append((char) i); } if (c == '\n') { contents.append('\n'); } } in.close(); return contents.toString(); } catch (javax.net.ssl.SSLProtocolException e) { LOGGER.warn("there is a problem with the target SSL certificate"); LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n"); LOGGER.error("unable to read", e); throw e; } catch (Exception e) { LOGGER.error("unable to read", e); throw e; } finally { if (is != null) { is.close(); } if (br != null) { br.close(); } } }  }
RemoteUrl { public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception { InputStream is = null; BufferedReader br = null; try { URLConnection conn; do { final URL inUrl = new URL(cleanUrl(url)); final List<AuthorizationValue> query = new ArrayList<>(); final List<AuthorizationValue> header = new ArrayList<>(); if (auths != null && auths.size() > 0) { for (AuthorizationValue auth : auths) { if (auth.getUrlMatcher()!= null) { if (auth.getUrlMatcher().test(inUrl)) { if ("query".equals(auth.getType())) { appendValue(inUrl, auth, query); } else if ("header".equals(auth.getType())) { appendValue(inUrl, auth, header); } } } } } if (!query.isEmpty()) { final URI inUri = inUrl.toURI(); final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery()); for (AuthorizationValue item : query) { if (newQuery.length() > 0) { newQuery.append("&"); } newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=") .append(URLEncoder.encode(item.getValue(), UTF_8.name())); } conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection(); } else { conn = inUrl.openConnection(); } CONNECTION_CONFIGURATOR.process(conn); for (AuthorizationValue item : header) { conn.setRequestProperty(item.getKeyName(), item.getValue()); } conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE); conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE); conn.connect(); url = ((HttpURLConnection) conn).getHeaderField("Location"); } while ((301 == ((HttpURLConnection) conn).getResponseCode())||(302 == ((HttpURLConnection) conn).getResponseCode()) || (307 == ((HttpURLConnection) conn).getResponseCode())||(308 == ((HttpURLConnection) conn).getResponseCode())); InputStream in = conn.getInputStream(); StringBuilder contents = new StringBuilder(); BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8)); for (int i = 0; i != -1; i = input.read()) { char c = (char) i; if (!Character.isISOControl(c)) { contents.append((char) i); } if (c == '\n') { contents.append('\n'); } } in.close(); return contents.toString(); } catch (javax.net.ssl.SSLProtocolException e) { LOGGER.warn("there is a problem with the target SSL certificate"); LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n"); LOGGER.error("unable to read", e); throw e; } catch (Exception e) { LOGGER.error("unable to read", e); throw e; } finally { if (is != null) { is.close(); } if (br != null) { br.close(); } } }  static String cleanUrl(String url); static String urlToString(String url, List<AuthorizationValue> auths); }
RemoteUrl { public static String urlToString(String url, List<AuthorizationValue> auths) throws Exception { InputStream is = null; BufferedReader br = null; try { URLConnection conn; do { final URL inUrl = new URL(cleanUrl(url)); final List<AuthorizationValue> query = new ArrayList<>(); final List<AuthorizationValue> header = new ArrayList<>(); if (auths != null && auths.size() > 0) { for (AuthorizationValue auth : auths) { if (auth.getUrlMatcher()!= null) { if (auth.getUrlMatcher().test(inUrl)) { if ("query".equals(auth.getType())) { appendValue(inUrl, auth, query); } else if ("header".equals(auth.getType())) { appendValue(inUrl, auth, header); } } } } } if (!query.isEmpty()) { final URI inUri = inUrl.toURI(); final StringBuilder newQuery = new StringBuilder(inUri.getQuery() == null ? "" : inUri.getQuery()); for (AuthorizationValue item : query) { if (newQuery.length() > 0) { newQuery.append("&"); } newQuery.append(URLEncoder.encode(item.getKeyName(), UTF_8.name())).append("=") .append(URLEncoder.encode(item.getValue(), UTF_8.name())); } conn = new URI(inUri.getScheme(), inUri.getAuthority(), inUri.getPath(), newQuery.toString(), inUri.getFragment()).toURL().openConnection(); } else { conn = inUrl.openConnection(); } CONNECTION_CONFIGURATOR.process(conn); for (AuthorizationValue item : header) { conn.setRequestProperty(item.getKeyName(), item.getValue()); } conn.setRequestProperty("Accept", ACCEPT_HEADER_VALUE); conn.setRequestProperty("User-Agent", USER_AGENT_HEADER_VALUE); conn.connect(); url = ((HttpURLConnection) conn).getHeaderField("Location"); } while ((301 == ((HttpURLConnection) conn).getResponseCode())||(302 == ((HttpURLConnection) conn).getResponseCode()) || (307 == ((HttpURLConnection) conn).getResponseCode())||(308 == ((HttpURLConnection) conn).getResponseCode())); InputStream in = conn.getInputStream(); StringBuilder contents = new StringBuilder(); BufferedReader input = new BufferedReader(new InputStreamReader(in, UTF_8)); for (int i = 0; i != -1; i = input.read()) { char c = (char) i; if (!Character.isISOControl(c)) { contents.append((char) i); } if (c == '\n') { contents.append('\n'); } } in.close(); return contents.toString(); } catch (javax.net.ssl.SSLProtocolException e) { LOGGER.warn("there is a problem with the target SSL certificate"); LOGGER.warn("**** you may want to run with -Djsse.enableSNIExtension=false\n\n"); LOGGER.error("unable to read", e); throw e; } catch (Exception e) { LOGGER.error("unable to read", e); throw e; } finally { if (is != null) { is.close(); } if (br != null) { br.close(); } } }  static String cleanUrl(String url); static String urlToString(String url, List<AuthorizationValue> auths);  }