source: josm/trunk/test/unit/org/openstreetmap/josm/io/remotecontrol/RemoteControlTest.java@ 15469

Last change on this file since 15469 was 15469, checked in by Don-vip, 5 years ago

fix #10033, fix #15748, fix #17097 - drop remote control https support

Rationale: all modern browsers (including next version of Safari) allow mixed-content to localhost.

Cross-platform / cross-browser HTTPS support is a pain to maintain, was never completed, and is no longer needed.

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol;
3
4import static org.junit.Assert.assertEquals;
5
6import java.io.BufferedReader;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.InputStreamReader;
10import java.net.HttpURLConnection;
11import java.net.URL;
12import java.nio.charset.StandardCharsets;
13import java.security.GeneralSecurityException;
14import java.security.KeyStore.TrustedCertificateEntry;
15
16import org.junit.After;
17import org.junit.Before;
18import org.junit.Rule;
19import org.junit.Test;
20import org.openstreetmap.josm.TestUtils;
21import org.openstreetmap.josm.spi.preferences.Config;
22import org.openstreetmap.josm.testutils.JOSMTestRules;
23import org.openstreetmap.josm.tools.PlatformHookWindows;
24import org.openstreetmap.josm.tools.PlatformManager;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27import mockit.Mock;
28import mockit.MockUp;
29
30/**
31 * Unit tests for Remote Control
32 */
33public class RemoteControlTest {
34
35 private String httpBase;
36
37 private static class PlatformHookWindowsMock extends MockUp<PlatformHookWindows> {
38 @Mock
39 public boolean setupHttpsCertificate(String entryAlias, TrustedCertificateEntry trustedCert) {
40 return true;
41 }
42 }
43
44 /**
45 * Setup test.
46 */
47 @Rule
48 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
49 public JOSMTestRules test = new JOSMTestRules().preferences().https().assertionsInEDT();
50
51 /**
52 * Starts Remote control before testing requests.
53 * @throws GeneralSecurityException if a security error occurs
54 */
55 @Before
56 public void setUp() throws GeneralSecurityException {
57 if (PlatformManager.isPlatformWindows() && "True".equals(System.getenv("APPVEYOR"))) {
58 // appveyor doesn't like us tinkering with the root keystore, so mock this out
59 TestUtils.assumeWorkingJMockit();
60 new PlatformHookWindowsMock();
61 }
62
63 RemoteControl.start();
64 httpBase = "http://127.0.0.1:"+Config.getPref().getInt("remote.control.port", 8111);
65 }
66
67 /**
68 * Stops Remote control after testing requests.
69 */
70 @After
71 public void tearDown() {
72 RemoteControl.stop();
73 }
74
75 /**
76 * Tests that sending an HTTP request without command results in HTTP 400, with all available commands in error message.
77 * @throws Exception if an error occurs
78 */
79 @Test
80 public void testHttpListOfCommands() throws Exception {
81 testListOfCommands(httpBase);
82 }
83
84 private void testListOfCommands(String url) throws IOException, ReflectiveOperationException {
85 HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
86 connection.connect();
87 assertEquals(connection.getResponseCode(), HttpURLConnection.HTTP_BAD_REQUEST);
88 try (InputStream is = connection.getErrorStream()) {
89 // TODO this code should be refactored somewhere in Utils as it is used in several JOSM classes
90 StringBuilder responseBody = new StringBuilder();
91 try (BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
92 String s;
93 while ((s = in.readLine()) != null) {
94 responseBody.append(s);
95 responseBody.append("\n");
96 }
97 }
98 assert responseBody.toString().contains(RequestProcessor.getUsageAsHtml());
99 }
100 }
101}
Note: See TracBrowser for help on using the repository browser.