| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.plugins.http2;
|
|---|
| 3 |
|
|---|
| 4 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|---|
| 5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
|---|
| 6 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
|---|
| 7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
|---|
| 8 |
|
|---|
| 9 | import java.io.IOException;
|
|---|
| 10 | import java.net.URI;
|
|---|
| 11 | import java.net.URL;
|
|---|
| 12 | import java.net.http.HttpRequest;
|
|---|
| 13 | import java.time.Duration;
|
|---|
| 14 | import java.util.List;
|
|---|
| 15 | import java.util.Map;
|
|---|
| 16 | import java.util.Optional;
|
|---|
| 17 |
|
|---|
| 18 | import org.junit.jupiter.api.Test;
|
|---|
| 19 | import org.openstreetmap.josm.plugins.http2.Http2Client.Http2Response;
|
|---|
| 20 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Unit test of {@link Http2Client}
|
|---|
| 24 | */
|
|---|
| 25 | @BasicPreferences
|
|---|
| 26 | class Http2ClientTest {
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Unit test of {@link Http2Response#parseDate}
|
|---|
| 30 | */
|
|---|
| 31 | @Test
|
|---|
| 32 | void testParseDate() {
|
|---|
| 33 | assertEquals(786297600000L, Http2Response.parseDate("Thu, 01 Dec 1994 16:00:00 GMT"));
|
|---|
| 34 | assertEquals(783459811000L, Http2Response.parseDate("Sat, 29 Oct 1994 19:43:31 GMT"));
|
|---|
| 35 | assertEquals(784903526000L, Http2Response.parseDate("Tue, 15 Nov 1994 12:45:26 GMT"));
|
|---|
| 36 | // == RFC 2616 ==
|
|---|
| 37 | // All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception.
|
|---|
| 38 | assertEquals(0L, Http2Response.parseDate("Thu, 01 Jan 1970 01:00:00 CET"));
|
|---|
| 39 | // == RFC 2616 ==
|
|---|
| 40 | // HTTP/1.1 clients and caches MUST treat other invalid date formats,
|
|---|
| 41 | // especially including the value "0", as in the past (i.e., "already
|
|---|
| 42 | // expired").
|
|---|
| 43 | assertEquals(0L, Http2Response.parseDate("0"));
|
|---|
| 44 | assertEquals(0L, Http2Response.parseDate("foo-bar"));
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | @Test
|
|---|
| 48 | void testCreateRequest() throws Exception {
|
|---|
| 49 | HttpRequest request = new Http2Client(new URL("https://foo.bar"), "GET").createRequest();
|
|---|
| 50 | assertEquals("GET", request.method());
|
|---|
| 51 | assertEquals(Duration.ofSeconds(30), request.timeout().get());
|
|---|
| 52 | assertEquals(new URI("https://foo.bar"), request.uri());
|
|---|
| 53 | assertEquals(Optional.empty(), request.version());
|
|---|
| 54 | Map<String, List<String>> headers = request.headers().map();
|
|---|
| 55 | assertEquals(2, headers.size());
|
|---|
| 56 | List<String> encodings = headers.get("Accept-Encoding");
|
|---|
| 57 | assertEquals(1, encodings.size());
|
|---|
| 58 | assertEquals("gzip, deflate", encodings.get(0));
|
|---|
| 59 | List<String> userAgents = headers.get("User-Agent");
|
|---|
| 60 | assertEquals(1, userAgents.size());
|
|---|
| 61 | assertTrue(userAgents.get(0).startsWith("JOSM/1.5 ("), userAgents.get(0));
|
|---|
| 62 | assertEquals("https://foo.bar GET", request.toString());
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | @Test
|
|---|
| 66 | void testCreateRequestInvalidURI() {
|
|---|
| 67 | // From https://josm.openstreetmap.de/ticket/21126
|
|---|
| 68 | // URISyntaxException for URL not formatted strictly according to RFC2396
|
|---|
| 69 | // See chapter "2.4.3. Excluded US-ASCII Characters"
|
|---|
| 70 | final URL url = assertDoesNotThrow(() -> 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"));
|
|---|
| 71 | assertTrue(assertThrows(IOException.class, () -> new Http2Client(url, "GET")
|
|---|
| 72 | .createRequest()).getCause().getMessage().startsWith("Illegal character in query at index 116:"));
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|