Commit fa80fff4 authored by iwakeh's avatar iwakeh
Browse files

Avoid adding superfluous brackets in suggestion links.

Also add a test checking for superfluous brackets.
Fixes task-26032.
parent 5eaad992
......@@ -466,7 +466,7 @@ public class ExoneraTorServlet extends HttpServlet {
contactLink);
}
private void writeSummaryAddressesInSameNetwork(PrintWriter out,
void writeSummaryAddressesInSameNetwork(PrintWriter out,
ResourceBundle rb, String relayIp, String timestampStr, String langStr,
List<String> addressesInSameNetwork) throws IOException {
Object[][] panelItems = new Object[addressesInSameNetwork.size()][];
......@@ -475,10 +475,10 @@ public class ExoneraTorServlet extends HttpServlet {
String link;
String address;
if (addressInSameNetwork.contains(":")) {
address = addressInSameNetwork.replaceAll("[\\[\\]]", "");
link = String.format("/?ip=[%s]&timestamp=%s&lang=%s",
addressInSameNetwork.replaceAll(":", "%3A"), timestampStr,
langStr);
address = "[" + addressInSameNetwork + "]";
address.replaceAll(":", "%3A"), timestampStr, langStr);
address = "[" + address + "]";
} else {
link = String.format("/?ip=%s&timestamp=%s&lang=%s",
addressInSameNetwork, timestampStr, langStr);
......
......@@ -4,9 +4,17 @@
package org.torproject.metrics.exonerator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;
public class ExoneraTorServletTest {
private static final String[][] ipTestData
......@@ -32,5 +40,55 @@ public class ExoneraTorServletTest {
assertEquals(data[1], ExoneraTorServlet.parseIpParameter(data[0]));
}
}
@Test
public void testNearbyIpV6Response() throws Exception {
ExoneraTorServlet es = new ExoneraTorServlet();
ResourceBundle rb = ResourceBundle
.getBundle("ExoneraTor", Locale.forLanguageTag("en"));
for (QueryResponse qr : qrs) {
StringWriter sw = new StringWriter();
es.writeSummaryAddressesInSameNetwork(new PrintWriter(sw), rb,
qr.queryAddress, qr.queryDate, "en",
Arrays.asList(qr.nearbyAddresses));
String errorMsg = "Test data:" + QueryResponse.toJson(qr)
+ "\nresult:\n" + sw.toString();
assertTrue(errorMsg,
sw.toString().contains("Result is negative"));
assertTrue(errorMsg,
sw.toString().contains("ip=[2a06%3Ae80%3A1%3A%3A10]&"));
assertTrue(errorMsg,
sw.toString().contains("ip=[2a06%3Ae80%3A1%3A%3A15]&"));
}
}
private QueryResponse[] qrs = new QueryResponse[]{
QueryResponse.fromJson(new StringReader(
"{\"version\":\"1.0\","
+ "\"query_address\":\"2a06:e80:1::11\","
+ "\"query_date\":\"2016-12-12\","
+ "\"first_date_in_database\":\"2016-01-01\","
+ "\"last_date_in_database\":\"2016-12-31\","
+ "\"relevant_statuses\":false,"
+ "\"nearby_addresses\":[\"2a06:e80:1::10\","
+ "\"2a06:e80:1::15\"]}")),
QueryResponse.fromJson(new StringReader(
"{\"version\":\"1.0\","
+ "\"query_address\":\"2a06:e80:1::11\","
+ "\"query_date\":\"2016-12-12\","
+ "\"first_date_in_database\":\"2016-01-01\","
+ "\"last_date_in_database\":\"2016-12-31\","
+ "\"relevant_statuses\":false,"
+ "\"nearby_addresses\":[\"[2a06:e80:1::10]\","
+ "\"2a06:e80:1::15\"]}")),
QueryResponse.fromJson(new StringReader(
"{\"version\":\"1.0\","
+ "\"query_address\":\"2a06:e80:1::11\","
+ "\"query_date\":\"2016-12-12\","
+ "\"first_date_in_database\":\"2016-01-01\","
+ "\"last_date_in_database\":\"2016-12-31\","
+ "\"relevant_statuses\":false,"
+ "\"nearby_addresses\":[\"[2a06:e80:1::10]\","
+ "\"[2a06:e80:1::15]\"]}"))};
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment