Changeset 35790 in osm


Ignore:
Timestamp:
2021-07-17T14:14:21+02:00 (3 years ago)
Author:
Don-vip
Message:

see #21126 - add unit test

Location:
applications/editors/josm/plugins/http2
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/http2

    • Property svn:ignore
      •  

        old new  
        22build
        33javadoc
         4bintest
  • applications/editors/josm/plugins/http2/.classpath

    r35053 r35790  
    22<classpath>
    33        <classpathentry kind="src" path="src"/>
    4         <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-11.0.3.7-hotspot">
     4        <classpathentry kind="src" output="bintest" path="test/unit">
     5                <attributes>
     6                        <attribute name="test" value="true"/>
     7                </attributes>
     8        </classpathentry>
     9        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
    510                <attributes>
    611                        <attribute name="module" value="true"/>
  • applications/editors/josm/plugins/http2/src/org/openstreetmap/josm/plugins/http2/Http2Client.java

    r35427 r35790  
    4646    @Override
    4747    protected void setupConnection(ProgressMonitor progressMonitor) throws IOException {
     48        request = createRequest();
     49
     50        notifyConnect(progressMonitor);
     51       
     52        if (requiresBody()) {
     53            logRequestBody();
     54        }
     55    }
     56
     57    protected HttpRequest createRequest() throws IOException {
    4858        HttpRequest.Builder requestBuilder;
    4959        try {
     
    7787            }
    7888        }
    79         request = requestBuilder.build();
    80 
    81         notifyConnect(progressMonitor);
    82        
    83         if (requiresBody()) {
    84             logRequestBody();
    85         }
     89        return requestBuilder.build();
    8690    }
    8791
  • applications/editors/josm/plugins/http2/test/unit/org/openstreetmap/josm/plugins/http2/Http2ClientTest.java

    r35427 r35790  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertThrows;
     6import static org.junit.jupiter.api.Assertions.assertTrue;
    57
    6 import org.junit.Test;
     8import java.io.IOException;
     9import java.net.URI;
     10import java.net.URL;
     11import java.net.http.HttpRequest;
     12import java.time.Duration;
     13import java.util.List;
     14import java.util.Map;
     15import java.util.Optional;
     16
     17import org.junit.jupiter.api.Test;
    718import org.openstreetmap.josm.plugins.http2.Http2Client.Http2Response;
     19import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    820
    921/**
    1022 * Unit test of {@link Http2Client}
    1123 */
    12 public class Http2ClientTest {
     24@BasicPreferences
     25class Http2ClientTest {
    1326
    1427    /**
     
    1629     */
    1730    @Test
    18     public void testParseDate() {
     31    void testParseDate() {
    1932        assertEquals(786297600000L, Http2Response.parseDate("Thu, 01 Dec 1994 16:00:00 GMT"));
    2033        assertEquals(783459811000L, Http2Response.parseDate("Sat, 29 Oct 1994 19:43:31 GMT"));
     
    3043        assertEquals(0L, Http2Response.parseDate("foo-bar"));
    3144    }
     45
     46    @Test
     47    void testCreateRequest() throws Exception {
     48        HttpRequest request = new Http2Client(new URL("https://foo.bar"), "GET").createRequest();
     49        assertEquals("GET", request.method());
     50        assertEquals(Duration.ofSeconds(30), request.timeout().get());
     51        assertEquals(new URI("https://foo.bar"), request.uri());
     52        assertEquals(Optional.empty(), request.version());
     53        Map<String, List<String>> headers = request.headers().map();
     54        assertEquals(2, headers.size());
     55        List<String> encodings = headers.get("Accept-Encoding");
     56        assertEquals(1, encodings.size());
     57        assertEquals("gzip, deflate", encodings.get(0));
     58        List<String> userAgents = headers.get("User-Agent");
     59        assertEquals(1, userAgents.size());
     60        assertTrue(userAgents.get(0).startsWith("JOSM/1.5 ("), userAgents.get(0));
     61        assertEquals("https://foo.bar GET", request.toString());
     62    }
     63
     64    @Test
     65    void testCreateRequest_invalidURI() throws Exception {
     66        // From https://josm.openstreetmap.de/ticket/21126
     67        // URISyntaxException for URL not formatted strictly according to RFC2396
     68        // See chapter "2.4.3. Excluded US-ASCII Characters"
     69        assertTrue(assertThrows(IOException.class, () -> new Http2Client(
     70                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")
     71                .createRequest()).getCause().getMessage().startsWith("Illegal character in query at index 116:"));
     72    }
    3273}
Note: See TracChangeset for help on using the changeset viewer.