1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.net.HttpURLConnection;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.nio.charset.StandardCharsets;
|
---|
11 | import java.security.GeneralSecurityException;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.AfterEach;
|
---|
14 | import org.junit.jupiter.api.BeforeEach;
|
---|
15 | import org.junit.jupiter.api.Test;
|
---|
16 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
17 | import org.openstreetmap.josm.testutils.annotations.AssertionsInEDT;
|
---|
18 | import org.openstreetmap.josm.testutils.annotations.HTTPS;
|
---|
19 | import org.openstreetmap.josm.tools.Utils;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Unit tests for Remote Control
|
---|
23 | */
|
---|
24 | @AssertionsInEDT
|
---|
25 | @HTTPS
|
---|
26 | class RemoteControlTest {
|
---|
27 |
|
---|
28 | private String httpBase;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Starts Remote control before testing requests.
|
---|
32 | * @throws GeneralSecurityException if a security error occurs
|
---|
33 | */
|
---|
34 | @BeforeEach
|
---|
35 | public void setUp() throws GeneralSecurityException {
|
---|
36 | RemoteControl.start();
|
---|
37 | httpBase = "http://127.0.0.1:"+Config.getPref().getInt("remote.control.port", 8111);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Stops Remote control after testing requests.
|
---|
42 | */
|
---|
43 | @AfterEach
|
---|
44 | public void tearDown() {
|
---|
45 | RemoteControl.stop();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Tests that sending an HTTP request without command results in HTTP 400, with all available commands in error message.
|
---|
50 | * @throws Exception if an error occurs
|
---|
51 | */
|
---|
52 | @Test
|
---|
53 | void testHttpListOfCommands() throws Exception {
|
---|
54 | testListOfCommands(httpBase);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void testListOfCommands(String url) throws IOException, ReflectiveOperationException {
|
---|
58 | HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
---|
59 | connection.connect();
|
---|
60 | assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, connection.getResponseCode());
|
---|
61 | try (InputStream is = connection.getErrorStream()) {
|
---|
62 | String responseBody = new String(Utils.readBytesFromStream(is), StandardCharsets.UTF_8);
|
---|
63 | assert responseBody.contains(RequestProcessor.getUsageAsHtml());
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|