Index: /applications/editors/josm/plugins/http2/.classpath
===================================================================
--- /applications/editors/josm/plugins/http2/.classpath	(revision 35789)
+++ /applications/editors/josm/plugins/http2/.classpath	(revision 35790)
@@ -2,5 +2,10 @@
 <classpath>
 	<classpathentry kind="src" path="src"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-11.0.3.7-hotspot">
+	<classpathentry kind="src" output="bintest" path="test/unit">
+		<attributes>
+			<attribute name="test" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
 		<attributes>
 			<attribute name="module" value="true"/>
Index: /applications/editors/josm/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Client.java
===================================================================
--- /applications/editors/josm/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Client.java	(revision 35789)
+++ /applications/editors/josm/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Client.java	(revision 35790)
@@ -46,4 +46,14 @@
     @Override
     protected void setupConnection(ProgressMonitor progressMonitor) throws IOException {
+        request = createRequest();
+
+        notifyConnect(progressMonitor);
+        
+        if (requiresBody()) {
+            logRequestBody();
+        }
+    }
+
+    protected HttpRequest createRequest() throws IOException {
         HttpRequest.Builder requestBuilder;
         try {
@@ -77,11 +87,5 @@
             }
         }
-        request = requestBuilder.build();
-
-        notifyConnect(progressMonitor);
-        
-        if (requiresBody()) {
-            logRequestBody();
-        }
+        return requestBuilder.build();
     }
 
Index: /applications/editors/josm/plugins/http2/test/unit/org/openstreetmap/josm/plugins/http2/Http2ClientTest.java
===================================================================
--- /applications/editors/josm/plugins/http2/test/unit/org/openstreetmap/josm/plugins/http2/Http2ClientTest.java	(revision 35789)
+++ /applications/editors/josm/plugins/http2/test/unit/org/openstreetmap/josm/plugins/http2/Http2ClientTest.java	(revision 35790)
@@ -3,12 +3,25 @@
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import org.junit.Test;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URL;
+import java.net.http.HttpRequest;
+import java.time.Duration;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import org.junit.jupiter.api.Test;
 import org.openstreetmap.josm.plugins.http2.Http2Client.Http2Response;
+import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
 
 /**
  * Unit test of {@link Http2Client}
  */
-public class Http2ClientTest {
+@BasicPreferences
+class Http2ClientTest {
 
     /**
@@ -16,5 +29,5 @@
      */
     @Test
-    public void testParseDate() {
+    void testParseDate() {
         assertEquals(786297600000L, Http2Response.parseDate("Thu, 01 Dec 1994 16:00:00 GMT"));
         assertEquals(783459811000L, Http2Response.parseDate("Sat, 29 Oct 1994 19:43:31 GMT"));
@@ -30,3 +43,31 @@
         assertEquals(0L, Http2Response.parseDate("foo-bar"));
     }
+
+    @Test
+    void testCreateRequest() throws Exception {
+        HttpRequest request = new Http2Client(new URL("https://foo.bar"), "GET").createRequest();
+        assertEquals("GET", request.method());
+        assertEquals(Duration.ofSeconds(30), request.timeout().get());
+        assertEquals(new URI("https://foo.bar"), request.uri());
+        assertEquals(Optional.empty(), request.version());
+        Map<String, List<String>> headers = request.headers().map();
+        assertEquals(2, headers.size());
+        List<String> encodings = headers.get("Accept-Encoding");
+        assertEquals(1, encodings.size());
+        assertEquals("gzip, deflate", encodings.get(0));
+        List<String> userAgents = headers.get("User-Agent");
+        assertEquals(1, userAgents.size());
+        assertTrue(userAgents.get(0).startsWith("JOSM/1.5 ("), userAgents.get(0));
+        assertEquals("https://foo.bar GET", request.toString());
+    }
+
+    @Test
+    void testCreateRequest_invalidURI() throws Exception {
+        // From https://josm.openstreetmap.de/ticket/21126
+        // URISyntaxException for URL not formatted strictly according to RFC2396
+        // See chapter "2.4.3. Excluded US-ASCII Characters"
+        assertTrue(assertThrows(IOException.class, () -> new Http2Client(
+                new URL("https://commons.wikimedia.org/w/api.php?format=xml&action=query&list=geosearch&gsnamespace=6&gslimit=500&gsprop=type|name&gsbbox=52.2804692|38.1772755|52.269721|38.2045051"), "GET")
+                .createRequest()).getCause().getMessage().startsWith("Illegal character in query at index 116:"));
+    }
 }
