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

Last change on this file since 17360 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • 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.jupiter.api.Assertions.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;
15import java.util.stream.Collectors;
16
17import org.junit.jupiter.api.AfterEach;
18import org.junit.jupiter.api.BeforeEach;
19import org.junit.jupiter.api.Test;
20import org.junit.jupiter.api.extension.RegisterExtension;
21import org.openstreetmap.josm.TestUtils;
22import org.openstreetmap.josm.spi.preferences.Config;
23import org.openstreetmap.josm.testutils.JOSMTestRules;
24import org.openstreetmap.josm.tools.PlatformHookWindows;
25import org.openstreetmap.josm.tools.PlatformManager;
26
27import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
28import mockit.Mock;
29import mockit.MockUp;
30
31/**
32 * Unit tests for Remote Control
33 */
34class RemoteControlTest {
35
36 private String httpBase;
37
38 private static class PlatformHookWindowsMock extends MockUp<PlatformHookWindows> {
39 @Mock
40 public boolean setupHttpsCertificate(String entryAlias, TrustedCertificateEntry trustedCert) {
41 return true;
42 }
43 }
44
45 /**
46 * Setup test.
47 */
48 @RegisterExtension
49 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
50 public JOSMTestRules test = new JOSMTestRules().preferences().https().assertionsInEDT();
51
52 /**
53 * Starts Remote control before testing requests.
54 * @throws GeneralSecurityException if a security error occurs
55 */
56 @BeforeEach
57 public void setUp() throws GeneralSecurityException {
58 if (PlatformManager.isPlatformWindows() && "True".equals(System.getenv("APPVEYOR"))) {
59 // appveyor doesn't like us tinkering with the root keystore, so mock this out
60 TestUtils.assumeWorkingJMockit();
61 new PlatformHookWindowsMock();
62 }
63
64 RemoteControl.start();
65 httpBase = "http://127.0.0.1:"+Config.getPref().getInt("remote.control.port", 8111);
66 }
67
68 /**
69 * Stops Remote control after testing requests.
70 */
71 @AfterEach
72 public void tearDown() {
73 RemoteControl.stop();
74 }
75
76 /**
77 * Tests that sending an HTTP request without command results in HTTP 400, with all available commands in error message.
78 * @throws Exception if an error occurs
79 */
80 @Test
81 void testHttpListOfCommands() throws Exception {
82 testListOfCommands(httpBase);
83 }
84
85 private void testListOfCommands(String url) throws IOException, ReflectiveOperationException {
86 HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
87 connection.connect();
88 assertEquals(connection.getResponseCode(), HttpURLConnection.HTTP_BAD_REQUEST);
89 try (InputStream is = connection.getErrorStream()) {
90 // TODO this code should be refactored somewhere in Utils as it is used in several JOSM classes
91 String responseBody;
92 try (BufferedReader in = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
93 responseBody = in.lines().collect(Collectors.joining("\n"));
94 }
95 assert responseBody.contains(RequestProcessor.getUsageAsHtml());
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.