From 9b1d403b69a3a78e25e3577d700dfca6e3ae4df4 Mon Sep 17 00:00:00 2001 From: Sarah Bedn Date: Wed, 2 Oct 2024 19:19:59 +0100 Subject: [PATCH] tests for CookieUtil and RequestAuthenticator helpers --- .../java/org/jsoup/helper/CookieUtilTest.java | 47 +++++++++++++++++++ .../helper/RequestAuthenticatorTest.java | 42 +++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/test/java/org/jsoup/helper/CookieUtilTest.java create mode 100644 src/test/java/org/jsoup/helper/RequestAuthenticatorTest.java diff --git a/src/test/java/org/jsoup/helper/CookieUtilTest.java b/src/test/java/org/jsoup/helper/CookieUtilTest.java new file mode 100644 index 0000000000..6d7a32f5de --- /dev/null +++ b/src/test/java/org/jsoup/helper/CookieUtilTest.java @@ -0,0 +1,47 @@ +package org.jsoup.helper; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * This class tests the functionality of the CookieUtil class. + * It includes tests for applying cookies to a request, converting URLs to URIs, and storing cookies. + */ +public class CookieUtilTest { + + @Test + public void testApplyCookiesToRequest() throws IOException { + HttpConnection.Request req = new HttpConnection.Request(); + HttpURLConnection con = (HttpURLConnection) new URL("http://test.com").openConnection(); + req.cookie("name", "value"); + + CookieUtil.applyCookiesToRequest(req, con); + + assertEquals("name=value", con.getRequestProperty("Cookie")); + } + + @Test + public void testAsUri() throws IOException, URISyntaxException { + URL url = new URL("http://test.com"); + assertEquals(url.toURI(), CookieUtil.asUri(url)); + } + + @Test + public void testStoreCookies() throws IOException { + HttpConnection.Request req = new HttpConnection.Request(); + URL url = new URL("http://test.com"); + Map> resHeaders = new HashMap<>(); + resHeaders.put("Set-Cookie", Collections.singletonList("name=value")); + + CookieUtil.storeCookies(req, url, resHeaders); + + assertEquals("value", req.cookie("name")); + } +} \ No newline at end of file diff --git a/src/test/java/org/jsoup/helper/RequestAuthenticatorTest.java b/src/test/java/org/jsoup/helper/RequestAuthenticatorTest.java new file mode 100644 index 0000000000..37d4e276a8 --- /dev/null +++ b/src/test/java/org/jsoup/helper/RequestAuthenticatorTest.java @@ -0,0 +1,42 @@ +package org.jsoup.helper; + +import org.junit.jupiter.api.Test; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; +import java.net.URL; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test class for RequestAuthenticator. + * This class contains unit tests to verify the functionality of the RequestAuthenticator interface and its Context class. + */ +public class RequestAuthenticatorTest { + + @Test public void testAuthenticate() throws Exception { + RequestAuthenticator auth = new RequestAuthenticator() { + @Override + public PasswordAuthentication authenticate(Context auth) { + if (auth.isServer() && "Realm".equals(auth.realm())) { + return auth.credentials("user", "pass"); + } + return null; + } + }; + + RequestAuthenticator.Context context = new RequestAuthenticator.Context( + new URL("http://test.com"), Authenticator.RequestorType.SERVER, "Realm"); + + PasswordAuthentication credentials = auth.authenticate(context); + assertNotNull(credentials); + assertEquals("user", credentials.getUserName()); + assertEquals("pass", new String(credentials.getPassword())); + + RequestAuthenticator.Context wrongContext = new RequestAuthenticator.Context( + new URL("http://test.com"), Authenticator.RequestorType.PROXY, "WrongRealm"); + + PasswordAuthentication wrongCredentials = auth.authenticate(wrongContext); + assertNull(wrongCredentials); + } +} \ No newline at end of file